Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer. --Fred Brooks, Jr.
NinjaTrader Kickstart - Use your dev skills to trade the markets. This is my latest free course. I hope you'll try it out - Steve @ CambiaResearch

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

Missouri Sycamores. Copyright © 2001 Steve Lautenschlager

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.

To filter out different keystrokes besides numbers check out my javascript keycode page.

Try It!
Example: Allow only numbers/digits in TextBox
   <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;
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">

Version: 5.1.20191022.2156