Number Validation | Only Numbers allowed 0-9

Expert User Verified

It will allow only the numbers to be typed.

<input type="text" id="inputText" name="inputText" onkeypress="return isNumber(event)">
function isNumber(evt)
 {
         evt = (evt) ? evt : window.event;
          var charCode = (evt.which) ? evt.which : evt.keyCode;
           if (charCode > 31 && (charCode < 48 || charCode > 57))
           {
              return false;
             }
            return true;
 }

The main reason for this validation is to only allow the number to be entered in the textbox between 0-9. Apart from this user cannot enter the character. It will restrict in the beginning.

Call this on on the keypress event.

<input type="text" id="inputText" name="inputText" onkeypress="return isNumber(event)">

References and Credits

Comments

Leave a Comment