jquery - Html and javascript input validation -
i trying have input accept numbers , '.', working great doesn't allow number pad number keys. cant seem find exact answer online.
html
<input type="text" id="itemtotal#i#" name="itemtotal#i#" value="#qpriceact#" onkeypress="return isnumeric(event)" onkeydown="return keyispressed(event);">
javascript
//prevent , , $ being input function keyispressed(e){ var charval= string.fromcharcode(e.keycode); if(isnan(charval) && (e.which != 8 ) && (e.which != 190 )){ return false; } return true; } //is input numeric function isnumeric (evt) { var theevent = evt || window.event; var key = theevent.keycode || theevent.which; key = string.fromcharcode (key); var regex = /[0-9]|\./; if ( !regex.test(key) ) { theevent.returnvalue = false; if(theevent.preventdefault) theevent.preventdefault(); } }
thanks help!
the best way believe add class inputs allow numbers. can restrict input doesn't match pattern or number/decimal.
function numberverfication(value) { var pattern=/^[0-9]*(\.)?[0-9]*$/; if (value.match(pattern) != null){ return value } else { var p=/[0-9]*(\.)?[0-9]*/; return value.match(p)[0]; } } $('.numbersonly').keyup(function(e) { e.target.value = numberverfication(e.target.value); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class='numbersonly' type="text" id="itemtotal#i#" name="itemtotal#i#" value="#qpriceact#">
Comments
Post a Comment