Hello @Raju Ahmed i check your code and figure that u are testing key's with regex
but the problem is it's only checking for number's and this is what u asked. But also u need to check for Delete Key(which is 46) and also check for Backspace(which is 8) and other keys what u want to use.
Also i test your code on Chrome build 55.0.2883.87 m (64-bit) and it's also not working for delete, backspace and others.
You might want to use like this if u use jQuery:
function onlyNumberInput(evt) {
var theEvent = evt || window.event;
var allowed = [8, 17, 46, 37, 38, 39, 40];
var key = theEvent.keyCode || theEvent.which;
var regex = /[0-9]/;
if($.inArray(allowed, key) !== -1) {
key = String.fromCharCode( key );
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault)
theEvent.preventDefault();
}
}
}
If you want to use other keys, find the keyCode the key and add into allowed.
If you don't use JQuery in your code please let me know. I can change it to normal version :)
One more thing this is allowed to use in JS if you have only one line job:
if(theEvent.preventDefault)
theEvent.preventDefault();
Edit II:
Well @Raju i have check the code again it's not working as u wish. So i found an example about it with a plugin if you want to use it.
Plugin : NumericInput,
Demo: Demo
You can check this link about number only input's: HTML Text Input allow only Numeric input