Click here to Skip to main content
11,478,562 members (64,390 online)
Click here to Skip to main content

Text box to accept only number

, 23 Apr 2011 CPOL 56.3K 26
Rate this:
Please Sign up or sign in to vote.
Text box to accept only number
<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)

Share

About the Author

Prasanta_Prince
Software Developer India
India India
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.

Comments and Discussions

 
QuestionGood Solution Pin
Alireza_136220-Mar-13 23:24
memberAlireza_136220-Mar-13 23:24 
GeneralMy vote of 2 Pin
niubay11-Sep-12 4:31
memberniubay11-Sep-12 4:31 
GeneralRe: by small digits I mean something like: 1.2345, 6.78 etc I ... Pin
Unruled Boy24-Apr-11 13:14
memberUnruled Boy24-Apr-11 13:14 
GeneralRe: Agreed. Something like this regular expression /^-?[0-9]+(\.... Pin
danlobo30-Dec-11 0:32
memberdanlobo30-Dec-11 0:32 
GeneralRe: Negative values you just have to program to allow the minus ... Pin
EdMan19624-Apr-11 4:08
memberEdMan19624-Apr-11 4:08 
GeneralRe: The keyboard remapping just assigns different characters to ... Pin
AspDotNetDev24-Apr-11 1:44
protectorAspDotNetDev24-Apr-11 1: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... Pin
EdMan19623-Apr-11 23:57
memberEdMan19623-Apr-11 23:57 
GeneralReason for my vote of 5 exclent Pin
lovepreet_shota18-Jun-11 18:15
memberlovepreet_shota18-Jun-11 18:15 
GeneralThis is all well and good unless of course my keyboards numb... Pin
EdMan19623-Apr-11 3:40
memberEdMan19623-Apr-11 3:40 
GeneralRe: What do you mean? How would your keyboard number keys use di... Pin
AspDotNetDev23-Apr-11 19:51
protectorAspDotNetDev23-Apr-11 19:51 
GeneralRe: what if small digits or negative values? I think using regex... Pin
Unruled Boy24-Apr-11 4:05
memberUnruled Boy24-Apr-11 4:05 
GeneralExplanation needed Pin
Indivara18-Apr-11 13:05
subeditorIndivara18-Apr-11 13:05 
GeneralRe: Explanation needed Pin
pashad27-Dec-11 19:52
memberpashad27-Dec-11 19:52 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 23 Apr 2011
Article Copyright 2011 by Prasanta_Prince
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid