Click here to Skip to main content
Click here to Skip to main content

Text box to accept only number

By , 23 Apr 2011
 
<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>
 
Just paste the Script Section in the Head section and in on key press call the function.
For C#, just paste the following code:
 
function allownumbers(e) {
        var key = window.event ? e.keyCode : e.which;
        var keychar = String.fromCharCode(key);
        var reg = new RegExp("[0-9.]")
        if (key == 8) {
            //alert(key);
            keychar = String.fromCharCode(key);
        }

And in PageLoad:
txtNum.Attributes.Add("onkeypress", "javascript:return allownumbers(event);");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Prasanta_Prince
Software Developer India
India India
Member
http://devcorners.com/
Total DotNet/Programming Solution
 
I am Prasanta Banerjee. I am an Asp.Net Developer. My site: http://devcorners.com/
Email: prasanta.it@hotmail.com
If any body wants to prepare for interview http://guru-code.blogspot.com/ is the good site.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGood SolutionmemberAlireza_136220 Mar '13 - 22:24 
Thanks
GeneralMy vote of 2memberniubay11 Sep '12 - 3:31 
from "For C#, just paste the following code:" its messy, and "String.fromCharCode(key);" is redundant, please fix it.
GeneralRe: by small digits I mean something like: 1.2345, 6.78 etc I ...memberUnruled Boy24 Apr '11 - 12:14 
by small digits I mean something like:
 
1.2345, 6.78 etc
 
I once wrote such functions, but I wrote 3 separate functions, one for integers, one for small digits, one for negative small digits. Yet I believe that it would be much better to have all of them within one function.
GeneralRe: Agreed. Something like this regular expression /^-?[0-9]+(\....memberdanlobo29 Dec '11 - 23:32 
Agreed. Something like this regular expression
/^-?[0-9]+(\.[0-9]{2})?$/
is not so bad to use, and at the end, gets the job done.
GeneralRe: Negative values you just have to program to allow the minus ...memberEdMan19624 Apr '11 - 3:08 
Negative values you just have to program to allow the minus chracter once at the beggining - very simple. Small digits? I don't understand what you mean? Also, regexp is horribly messy and almost impossible to read/modify as soon as you do anything beyond the very basic. Even it's abreviated name is horrible.
GeneralRe: The keyboard remapping just assigns different characters to ...protectorAspDotNetDev24 Apr '11 - 0:44 
The keyboard remapping just assigns different characters to the physical keys... doesn't change the underlying code of the characters. And I'm gonna have to call BS on the different encodings. If it were in a different encoding, using the character in quotes wouldn't help... they'd still be interpreted as the same ASCII code. UTF-8, by the way, is compatible with ASCII... the first 128 characters or so in UTF-8 map to the same characters as ASCII. And the encoding doesn't matter by the time the Javascript is reached... encodings are just a way to store the same codes in different formats for transmission (such as over a network or in a file). I imagine by the time the characters reach Javascript, the same encoding is used in memory regardless of the encoding of the text in the input fields (as is done in C#). In short, using characters rather than numbers may help readability, but it would not make the code more portable.
GeneralRe: For starters you can remap your keyboard but also, the world...memberEdMan19623 Apr '11 - 22:57 
For starters you can remap your keyboard but also, the world isn't just ASCII you know, there's UTF-8 etc. Also, how about my sister's Russian keyboard that is completely different, I would doubt that that maps exactly the same. Perhaps the numbers might be the same but as a general rule, use chars, it's safer.
GeneralReason for my vote of 5 exclentmemberlovepreet_shota18 Jun '11 - 17:15 
Reason for my vote of 5
exclent
GeneralThis is all well and good unless of course my keyboards numb...memberEdMan19623 Apr '11 - 2:40 
This is all well and good unless of course my keyboards number keys don't match char codes 31 and 48 to 57.... You ought to be using '9' instead of 57 etc. to allow for different keyboards. Otherwise, this is a good example (though vars in C# are messy if you ask me. Why not use some proper types? :/ )
GeneralRe: What do you mean? How would your keyboard number keys use di...protectorAspDotNetDev23 Apr '11 - 18:51 
What do you mean? How would your keyboard number keys use different ASCII codes?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 23 Apr 2011
Article Copyright 2011 by Prasanta_Prince
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid