I want input field accept numbers and decimal only.
Input field should not accept negative, letters, symbols other than single decimal. Number 1 works absolutely fine with input type "text". However, number 2 doesn't work and the only difference is type as number.
I want numbers and decimals only. No negative, no letter and no symbol.
All the keyboard keys have different ASCII code just creating a script which will except only number and decimals by recognising there acsii codes rest will be ignored
HTML
<input type="text" class="inputNumberDot">
JS
$(document).ready(function() {
$('.inputNumberDot').keypress(function(event) {
var charCode = (event.which) ? event.which : event.keyCode
if (
(charCode != 45 || $(this).val().indexOf('-') != -1) && // “-” CHECK MINUS, AND ONLY ONE.
(charCode != 46 || $(this).val().indexOf('.') != -1) && // “.” CHECK DOT, AND ONLY ONE.
(charCode < 48 || charCode > 57))
return false;
return true;
});
});
Working example : https://jsfiddle.net/Luaq2f65/2/
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