Allow only numbers in a text box

Hi,

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.

<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
      <!–
       function isNumberPressed(evt) {
           var charCode = (evt.which) ? evt.which : event.keyCode
           if (charCode > 31 && (charCode 57))
               return false;
           return true;
       }
      //–>
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id=”txtChar”onkeypress=”return isNumberPressed(event)” type=”text” name=”txtChar”>
   </BODY>
</HTML>
 
Hope this helps.


Happy Coding
Gopinath

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s