The number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).
Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:
<label for="totalAmt">Total Amount</label>
<input type="number" step="0.01" id="totalAmt">
(I'd also set min=0 if it can only be positive)
If you'd prefer to allow any number of decimal places, you can use step="any" (though for currencies, I'd recommend sticking to 0.01). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)
Here's a playground showing how various steps affect various input types:
<form>
<input type=number step=1 /> Step 1 (default)<br />
<input type=number step=0.01 /> Step 0.01<br />
<input type=number step=any /> Step any<br />
<input type=range step=20 /> Step 20<br />
<input type=datetime-local step=60 /> Step 60 (default)<br />
<input type=datetime-local step=1 /> Step 1<br />
<input type=datetime-local step=any /> Step any<br />
<input type=datetime-local step=0.001 /> Step 0.001<br />
<input type=datetime-local step=3600 /> Step 3600 (1 hour)<br />
<input type=datetime-local step=86400 /> Step 86400 (1 day)<br />
<input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br />
</form>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!
Answer from Dave on Stack OverflowThe number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).
Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:
<label for="totalAmt">Total Amount</label>
<input type="number" step="0.01" id="totalAmt">
(I'd also set min=0 if it can only be positive)
If you'd prefer to allow any number of decimal places, you can use step="any" (though for currencies, I'd recommend sticking to 0.01). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)
Here's a playground showing how various steps affect various input types:
<form>
<input type=number step=1 /> Step 1 (default)<br />
<input type=number step=0.01 /> Step 0.01<br />
<input type=number step=any /> Step any<br />
<input type=range step=20 /> Step 20<br />
<input type=datetime-local step=60 /> Step 60 (default)<br />
<input type=datetime-local step=1 /> Step 1<br />
<input type=datetime-local step=any /> Step any<br />
<input type=datetime-local step=0.001 /> Step 0.001<br />
<input type=datetime-local step=3600 /> Step 3600 (1 hour)<br />
<input type=datetime-local step=86400 /> Step 86400 (1 day)<br />
<input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br />
</form>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!
Via: http://blog.isotoma.com/2012/03/html5-input-typenumber-and-decimalsfloats-in-chrome/
But what if you want all the numbers to be valid, integers and decimals alike? In this case, set step to “any”
<input type="number" step="any" />
Works for me in Chrome, not tested in other browsers.
Double format problems when using <input type="number">
How to convert int to double using jquery or javascript when user enter input value - Stack Overflow
javascript - Validate for double type in textbox - Stack Overflow
html - javascript enable input on double click - Stack Overflow
Instead of step="any", which allows for any number of decimal places, use step=".01", which allows up to two decimal places.
More details in the spec: https://www.w3.org/TR/html/sec-forms.html#the-step-attribute
In case anyone is looking for a regex that allows only numbers with an optional 2 decimal places
^\d*(\.\d{0,2})?$
For an example, I have found solution below to be fairly reliable
HTML:
<input name="my_field" pattern="^\d*(\.\d{0,2})?$" />
JS / JQuery:
$(document).on('keydown', 'input[pattern]', function(e){
var input = $(this);
var oldVal = input.val();
var regex = new RegExp(input.attr('pattern'), 'g');
setTimeout(function(){
var newVal = input.val();
if(!regex.test(newVal)){
input.val(oldVal);
}
}, 1);
});
You have not closed the dom ready function. });
There is another problem as well, at the document.ready function, the value of the input will be blank. Which causes error.
$(document).ready(function () {
$("input").change(function () {
var decimalNumber = document.getElementById('TextBox1').value;
var integerNumber = parseFloat(decimalNumber);
alert(integerNumber);
});
});
Here I have binded a change event to the text box.
Fiddle
I am unclear of what you need ,but this should help.
$(document).ready(function () {
$("input").change(function () {
var d= document.getElementById('TextBox1').value;
d=parseFloat(d);
var i = d.toFixed(2);
alert(i);
});
});
Also close the document ready function.
You can use regex like this
> "222.22".match(/^\d+.?\d*$/)
["222.22"]
> "222.22.2".match(/^\d+.?\d*$/)
null
You can also just try to convert it to Number e.g.
> isNaN(Number("22.2.2"))
true
Only problem or advantage with checking by converting to Number is that it will allow number like 1e6
> isNaN(Number("1e6"))
false
If you are talking in terms of jQuery, there are many options out there. Most of them involves a regular expression check.
Matching against a valid number is easy enough with regular expressions, if your main concern is preventing further character insertions, read on.
All you need to do is the following.
- You need a character map of integers to 'allow' the inputs.
- Prevent all other inputs except keys that match the character map.
- Further prevent the 'dot' insertion if there is already one.
I will assume you allow patterns that starts with a dot, as long as javascript parses it as if there is a leading zero, and many developers treat this as a short-hand.
Also with the jQuery tag, assumed that you are working with jQuery.
$("#Foo").keydown(function(e) {
var c = e.keyCode
, value = $(this).val();
// Prevent insertion if the inserting character is
// 1. a 'dot' but there is already one in the text box, or
// 2. not numerics.
if ( (c == 190 && value.indexOf('.') > -1) || c < 48 || c > 57 ) {
e.preventDefault();
return;
}
});
ondblclick does not get fired on a disabled element, you should mark it as readonly:
<input type="text" value="asdf" readonly="true" ondblclick="this.readOnly='';">
http://jsfiddle.net/vhktx/4/
You will have to wrap the element in a container since the the event will not fire on disabled elements.
jsFiddle
<div ondblclick="document.getElementById('test').disabled=false;">
<input type="text" id="test" value="asdf" disabled="disabled"></div>
i know you can do <input type="number"> but it only does integer.
how can you do it for double/decimal number? thanks
<input class="test-input" type="number" maxlength="12" />
<script>
$('.test-input').unbind('keyup change input paste').bind('keyup change input paste',function(e){
var $this = $(this);
var val = $this.val();
var valLength = val.length;
var maxCount = $this.attr('maxlength');
if(valLength>maxCount){
$this.val($this.val().substring(0,maxCount));
}
});
</script>
Your example works if you remove the $(".js-number").numeric();
But you can edit the input.
maxlength attribute works on input type text, so I guess javascript validation is inevitable.
<input type="number" class="js-number" min="0" max="99" value="0">
<script>
$(".js-number").bind('keydown', function(e){
var targetValue = $(this).val();
if (e.which ===8 || e.which === 13 || e.which === 37 || e.which === 39 || e.which === 46) { return; }
if (e.which > 47 && e.which < 58 && targetValue.length < 2) {
var c = String.fromCharCode(e.which);
var val = parseInt(c);
var textVal = parseInt(targetValue || "0");
var result = textVal + val;
if (result < 0 || result > 99) {
e.preventDefault();
}
if (targetValue === "0") {
$(this).val(val);
e.preventDefault();
}
}
else {
e.preventDefault();
}
});
</script>