467,876 Members | 1,224 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

How To Restrict TextBox To Only Accept Numeric Values

64KB
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
  • viewed: 55012
Share:
3 Comments
Frinavale
Expert Mod 8TB
Your article sounds a lot like the article titled How to check if a TextBox contains a number
Aug 2 '12 #2
64KB
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
hi

thank u
it works
Nov 20 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Qwert | last post: by
3 posts views Thread by Krish | last post: by
1 post views Thread by Ronald S. Cook | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.