Add to collection
Expert User

Expert User

Verified

Number Validation | Only Numbers allowed 0-9

It will allow only the numbers to be typed.

Try Yourself in Code Editor    

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)">
<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;
 }

References and Credits

Comments 0

  • Sorry!! No comment posted yet. Become the first user to give comment.
Add a comment

Would you like to write for w3 sniff?

Become part of an amazing group of contributors and authors and start writing for us

Join Now
<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;
 }
Feedback submitted successfully.