I had a html field like below
<input type="text" name="price" class="validate_price" id="price"/>
So the field should accept only float, decimal, integer values from the input.
jQuery code
<script>
function isFloat(value)
{
return value!= "" && !isNaN(value) && Math.round(value) != value;
}
$(document).ready(function(){
$('.validate_price').focusout(function(){
var value = this.value;
if (value)
{
if (!isFloat(value) || !$.isNumeric(value))
alert('Value must be float or not');
}
});
});
</script>
So from the above code, what all my intention is, I should allow user to enter integer values(1,2,2, etc.,), float values(1.0, 2.0, 3.0, 3.2, 3.5, etc.) and decimal values(2.152, 4.56, 5.000, 6.3256, 2.00 etc.,)
How to check/implement the above functionality ?
If you notice from the jQuery.isNumeric() manual manual, it also validates floats, so there is no need to include any other arguments. To validate a number, all you have to do is use jQuery.isNumeric():
if (!$.isNumeric(value)) {
alert('Value must be numeric');
}
I made a quick jsFiddle for you: http://jsfiddle.net/guxz9/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With