Sunday, 15 November 2015

Allow only numbers in a text box


In most of web applications, we get requirement to allow only number in a textbox.

Here is the JavaScript for it, just call this method onKeyPressv Event.

   <SCRIPT language=Javascript>
       function isNumberPressed(evt) {
           var charCode = (evt.which) ? evt.which : event.keyCode
           if (charCode > 31 && (charCode < 48 || charCode > 57))
               return false;
           return true;
      <INPUT id="txtChar" onkeypress="return isNumberPressed(event)" type="text" name="txtChar">
Hope this helps.

Happy Coding

No comments:

Post a Comment