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

     | Home  | Articles  | Categories  | Coders  | Search  | Submit  | Contact Us    
Simplicity is prerequisite for reliability. --Edsger W. Dijkstra

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 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
govind07 Nov 08, 4:59Reply 
This script has problems
Ahern14 Nov 08, 15:17Reply 
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 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 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Anonymous26 Feb 09, 3:49Reply 
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 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
JP03 Aug 08, 17:37Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Robert07 Aug 08, 9:48Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Denny05 May 09, 5:37Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Admir12 Aug 08, 2:20Reply 
Good
Rose14 Aug 08, 6:08Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Katsuke18 Aug 08, 22:59Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
kishore24 Apr 09, 3:25Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Aarthi21 Oct 08, 1:53Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
anilreddy17 Dec 08, 3:48Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
AHern14 Nov 08, 15:18Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Surbhi16 Dec 08, 4:21Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
anilreddy17 Dec 08, 3:46Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
Hemant17 Dec 08, 6:25Reply 
DOUBT!..How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBo
Hemant17 Dec 08, 6:31Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
swetha18 Dec 08, 1:15Reply 
How Can I check the emailid expression by using javascript?<br/>my mail id
swetha18 Dec 08, 1:19Reply 
re: How Can I check the emailid expression by using javascript?<br/>my mai
Hemant20 Dec 08, 9:46Reply 
In Fire Fox ,t will not allow ctrl+v and ctrl+c , how can v achieve this ?
Raj07 Jan 09, 4:12Reply 
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
samy30 Jun 09, 9:53Reply 
re: How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
samy30 Jun 09, 10:08Reply 
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