Numeric validation with decimal point.
Numeric validation with decimal point.
HTML
<h4>Numeric validation</h4>
<input type="text" class="numbersonly">
<br>
<br>
<input type="text" class="numbersonly">
<br>
<br>
<input type="text" class="numbersonly">
<br>
<br>
<input type="text" class="numbersonly">
JavaScript
/*Numeric validation starts*/
$(".numbersonly").keydown(function (e) {
// Prevent shift key since its not needed
var regexp = /^\d+(\.\d+)?$/;
alert(regexp.test('10.55555') )
if (e.shiftKey == true) {
e.preventDefault();
}
// Allow Only: keyboard 0-9, numpad 0-9
if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)
//Allow Only: backspace, tab, left arrow, right arrow
||
e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 37 || e.keyCode == 39
//Allow Only: delete, home, end
||
e.keyCode == 46 || e.keyCode == 36 || e.keyCode == 35
//Allow Only: .(full stop [keyboar, numpad]) and check if there is more than one .(full stop)
||
((e.keyCode == 190 || e.keyCode == 110) && $(this).val().indexOf('.') < 0)) {
// Allow normal operation
} else {
// Prevent the rest
e.preventDefault();
}
});