Only accept quarterly floats - javascript

I would like an HTML input validated on integer # (hourly), #.5 (half hourly) and #.25 (quarter).
I tried using HTML input type='number', but this seems to be fixed integers only. Does anybody have any clue on how to make a system that restricts on quarters?

Guessing you haven't tried step attribute yet?
Also believe you want it to be a time input since you mention hourly
:invalid{
color: red
}
<input type="time" step="900"><br>
<input type="number" step="0.25">

Related

How to set maximum number of characters in number type textbox?

I have one textbox which is of number type. I want it to accept maximum of 16 digits, not more than that. For that I have tried with "maxlength" attribue and "max" attribute both. But its not working. Can anyone provide me solution?
<input type="number" id="dummy" max="16" />
Edit: I need to enter only whole number since it is used for account number of bank.
There are a few of these "how do I further control type="number" fields" questions here on SO. It boils down to:
You make it a type="number" and accept that you can't further mask the input vs. what the browser does to implement a numeric input, or
You make it type="text" and use pattern and/or maxlength to enforce the specific pattern you want, and accept that the browser won't add a specific UI for numbers.
Input type number doesn't support "maxlength" attribute. You can specify "max" attribute (HTML5), which is the highest number user can input. Something like this: <input type="number" max="999999999999" id="dummy" step="1" min="0">.
Edit: Also specify step attribute to 1.

AngularJs-Facing issue with ui-number-mask in Portuguese (Brazil) locale

My application locale is Portuguese (Brazil). In Brazil a dot (.) is used to separate thousands and comma(,) is used for decimal. Suppose user enters value 4.878 When user saves the data, it saves the correct value as entered in UI, but when next time page render it shows the round off value(i.e. 5 instead of 4.878). I am facing problem with numeric text box without decimal places,
<input class="inputbox" ng-model="value" ui-number-mask="0" />
If I set 2 decimal places for same textbox, then it will show/renders correct value always(i.e. 4.878,00):-
<input class="inputbox" ng-model="value" ui-number-mask="2" />
Any suggestion, why values are rounding off and how to fix this.
This sounds like a localization issue. Have you tried following this? https://docs.angularjs.org/guide/i18n

HTML Field Type Input Number Only or Letter Only Without JavaScript

I create a form, where one field should only be filled with numbers.
I want that field can be filled only with numbers since entering input.
Like this example :
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
I've tried using Regex, but when I try to input is still able to enter letters.
<input type="number" min="2" pattern="^[0-9]" class="andi_input required-entry" name="amount" id="amount" required title='Only Number' />
I want it when input to field and not after click the Submit button and the message appear and inform that the field can only be filled with numbers.
I also try to add validate-number, but the result is the same.
How, without javascript, so that the field can only be filled with numbers or letters?
Whether for this kind of case have to use JavaScript or is there another way without JavaScript?
HTML 5 uses the type="number" so make sure that the browser that you are using is compatible. Check it out in action here.
You should check browser compatibility.
You're right with <input type="number" pattern="^[0-9]" />.
Your regex rule is :
^[0-9] :
^ assert position at start of the string
[0-9] match a single character present in the list below
(0-9 a single character in the range between 0 and 9)
You can check your regex here.
I use to use HTML5 Validation and jQuery one because IE is capricious most of the time.
UPDATE :
Without Javascript it's not possible to check pattern on real time. I suggest you to use a jQuery library like : http://www.jqueryscript.net/form/jQuery-Plugin-For-Formatting-User-Input-with-Specified-Pattern-formatter-js.html.
Here is a OneLiner:
<input type="number" onkeypress="return /[0-9]/i.test(event.key)">
or
<input type="text" onkeypress="return /[a-z]/i.test(event.key)">

Regex replace from number type input

I have a number type input that I want to prevent people from entering anything but numbers.
The examples I found work well with input type text fields but don't work well with number fields.
Works well :)
<input type="text" onkeyup="this.value=this.value.replace(/[^\d]/,'')">
Doesn't works well :(
<input type="number" onkeyup="this.value=this.value.replace(/[^\d]/,'')">
Try it for yourself on JSFiddle
Please help...
I've just done some simple testing using JSFiddle and it would appear that if there is an invalid input on an <input type="number" /> element then the this.value property is returned blank.
The following line showed this result when using Chrome:
<input type="number" oninput="alert(this.value)">
JSFiddle Demo
In fact here's the reason why this happens:
The value attribute, if specified and not empty, must have a value
that is a valid floating-point number.
The value sanitization algorithm is as follows: If the value of the
element is not a valid floating-point number, then set it to the empty
string instead.
^ From the HTML5 Draft Paper section on the implementation of the number input type
This problem has taken my interest now and I've come up with a little workaround.
<input type="number" oninput="updateNum(this)">
function updateNum(e)
{
e.select();
e.value = getSelection().toString().replace(/[^\d]/g,'');
}
This has the potential to be buggy if the selection where to change between commands.
JSFiddle for the workaround
See this DEMO if you want to accept numbers with fractional part (3.14 for example)
Number: <input type="number" oninput="updateNum(this)" />
function updateNum(e) {
if (isNaN(e.value)) {
e.value = e.value.replace(/[^\d]/g,'');
}

Mobile Number Masking using Javascript?

I want to use masking for Mobile Number in javascript.In this masking, I want to fix (+91) then user can input mobile number(10 digit number)
<label>Phone Number</label>
<input type="button" value="Turn on Mask" id="maskon" />
<input id="phone" type="text" value="" />
$("#maskon").click(function(){
$("#phone").mask("(+91) 9999-999999");
});
If I am using the given masking then I am able to put any digit in place of 9 instead of it I want to have (+91) as fixed.
Is there any idea to solve this problem?
Thanks in Advance!!
I do not think that there is an option for making first few digits fixed for mask plugin.
However you could also write keyup event for textbox and write a regex that verifies that first three digits are +91. You can use this regex for that. /(\+\d{2})/

Categories

Resources