By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,402 Members | 1,679 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How To Restrict TextBox To Only Accept Numeric Values

P: 85
I have seen lot of people asking how they can restrict users to numeric input only, so i thought to post it here as well.
well for making a text box to accept only numeric input, we will write a very simple javascript function in the <head></head> section of our markup like this.
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  4.       function isNumberKey(evt)
  5.       {
  6.          var charCode = (evt.which) ? evt.which : event.keyCode
  7.          if (charCode > 31 && (charCode < 48 || charCode > 57)) {
  8.              alert("Please Enter Only Numeric Value:");
  9.              return false;
  10.          }
  12.          return true;
  13.       }
  15.    </script>
and on the TextBox which needs to be only numeric will have a markup like this

Expand|Select|Wrap|Line Numbers
  1.  <asp:TextBox ID ="txtNumericInput" onkeypress="return isNumberKey(event)" runat ="server" ></asp:TextBox>
now run your project and try typing values other than numbers, you will get an alert..
Aug 2 '12 #1
Share this Article
Share on Google+

Expert Mod 5K+
P: 9,731
Your article sounds a lot like the article titled How to check if a TextBox contains a number
Aug 2 '12 #2

P: 85
but i think my post seems to be more simpler.. :) no problem there can be a number of ways to implement a task in programming world..
Aug 3 '12 #3

P: 1

thank u
it works
Nov 20 '14 #4