Cambia Research - Supporting the Microsoft .NET Developer Community Supporting the Microsoft .NET Developer Community  

     | Home  | Articles  | Categories  | Coders  | Search  | Submit  | Contact Us    
Microsoft's only factory asset is the human imagination. --Bill Gates

Share Your Knowledge! -- Create and submit your articles the easy way with WebWriter.

Updated:02:16 AM CT Jan 11, 2007
Posted:02:01 AM CT Jan 11, 2007

How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?

Author: Steve Lautenschlager

JavascriptHTMLBrowsersClient SideWeb

 Summary

Here I show how to use Javascript to allow only numbers to be entered in a textbox. This is useful for phone numbers, IDs, ZipCodes, and, well, that's about it.

This solution works in both IE and Netscape/Mozilla.

Try it here! Just type in the text box below. Note that digits are allowed and alphabetic characters are not allowed. Observe, too, that arrow keys and backspace _are_ allowed so that you can still edit what you type.
Try It!

Example: Allow only numbers/digits in TextBox

<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
      //-->
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
   </BODY>
</HTML>

Add New Comment
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
birju shah08 Jun 07, 4:29Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
kejal18 Oct 07, 6:10Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Jerod13 Jun 07, 11:34Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Josef20 Jun 07, 17:16Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
malfem29 Jun 07, 3:49Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
JamRoll23 Jul 07, 21:50Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
JamRoll23 Jul 07, 22:02Reply 
Too check for other symbols...
David C229 Oct 07, 14:17Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Matt25 Jul 07, 1:05Reply 
How Can I Use Javascript to Allow Only Characters from A to Z to Be Entered in a
Narayanan06 Sep 07, 11:33Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Dimmai12 Sep 07, 2:43Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
eboyjr16 Sep 07, 13:34Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
moron02 Oct 07, 11:34Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
chuck12 Oct 07, 20:20Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
chuck13 Oct 07, 1:56Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Satish25 Oct 07, 4:36Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Sri Amma Bhagawan25 Oct 07, 4:38Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Tuomas25 Oct 07, 6:38Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Tuomas25 Oct 07, 6:51Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Alex26 Oct 07, 11:13Reply 
It works above
Steve26 Oct 07, 22:02Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
przylepa.pl31 Oct 07, 10:34Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Steve31 Oct 07, 19:55Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
.net27 Nov 07, 20:05Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
sony27 Nov 07, 20:43Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Jason28 Dec 07, 6:13Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Roy27 Jan 08, 8:29Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Roy27 Jan 08, 8:35Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Anonymous26 May 08, 5:22Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Anonymous13 Feb 08, 1:00Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Tibox25 Apr 08, 8:52Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Sudhakar Janne14 May 08, 15:06Reply 
CR Comments by Cambia Research
advertisement
 
Steve Lautenschlager (steve)
Steve is the founder and creator of Cambia Research. Developing and maintaining the site combines his passions for technology, writing and education.
Steve holds a Ph.D. in particle physics from Duke University, has worked at CERN, the European center for particle physics (where the web was born) and in Microsoft's web division with microsoft.com, msnbc.com and other web properties. Steve is a web consultant specializing in Microsoft.NET technologies. Read more here.


 
Copyright © Cambia Research 2002-2007. All Rights Reserved. steve [ at ] cambiaresearch.com