Archive for October, 2013

simple JavaScript allow only digits/numbers in input field

Wednesday, October 23rd, 2013

<input onkeypress=”validate(event)” />

function validate(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();
}
}