How to prevent other number related characters other than unsigned integer
For example - e ...etc
Demo: http://codepen.io/anon/pen/jrEGrK
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<form action="#" class="container">
<br />
<br />
<br />
<input class="form-control" onkeyup="value=isNaN(parseFloat(value))?1000:value" type="number" value="0">
</form>
If you're ok with HTML5 why not use the pattern attribute and define a number only regex?
<input pattern="[1-9][0-9]*" ...>
function validate(e) {
var ev = e || window.event;
var key = ev.keyCode || ev.which;
key = String.fromCharCode( key );
var regex = /[0-9]/;
if( !regex.test(key) ) {
ev.returnValue = false;
if(ev.preventDefault) ev.preventDefault();
}
}
<input type='text' onkeypress='validate(event)' />
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