Thursday, January 19, 2012

Javascript validate text box to enter only number

Javascript  validate text box to enter only number

<script type="text/javascript">
function validate(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]|\x08/;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
</script>


<input type="text" name="afroz" value=""  onkeypress='validate(event)' >


//When you enter any text , decimal or special character in the text box it will not be entered. Only numbers
and backspace are allowed. Even decimal is restricted tto enter.

3 comments: