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">
  2.  
  3.  
  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.          }
  11.  
  12.          return true;
  13.       }
  14.  
  15.    </script>
  16.  
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>
  2.  
now run your project and try typing values other than numbers, you will get an alert..
Addan
Aug 2 '12 #1
Share this Article
Share on Google+
3 Comments


Frinavale
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
hi

thank u
it works
Nov 20 '14 #4