Click here to Skip to main content
15,667,355 members
Articles / Web Development / HTML
Posted 18 Apr 2011


26 bookmarked

Text box to accept only number

Rate me:
Please Sign up or sign in to vote.
4.13/5 (21 votes)
23 Apr 2011CPOL
Text box to accept only number
   <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;
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">

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) {
            keychar = String.fromCharCode(key);

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


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

Written By
Software Developer India
India India
Total DotNet/Programming Solution

I am Prasanta Banerjee. I am an Asp.Net Developer. My site:
If any body wants to prepare for interview is the good site.

Comments and Discussions

QuestionGood Solution Pin
Alireza_136220-Mar-13 22:24
Alireza_136220-Mar-13 22:24 
GeneralMy vote of 2 Pin
niubay11-Sep-12 3:31
niubay11-Sep-12 3:31 
GeneralRe: by small digits I mean something like: 1.2345, 6.78 etc I ... Pin
Huisheng Chen24-Apr-11 12:14
Huisheng Chen24-Apr-11 12:14 
GeneralRe: Agreed. Something like this regular expression /^-?[0-9]+(\.... Pin
danlobo29-Dec-11 23:32
danlobo29-Dec-11 23:32 
GeneralRe: Negative values you just have to program to allow the minus ... Pin
Ed Nutting24-Apr-11 3:08
Ed Nutting24-Apr-11 3:08 
GeneralRe: The keyboard remapping just assigns different characters to ... Pin
AspDotNetDev24-Apr-11 0:44
protectorAspDotNetDev24-Apr-11 0:44 
GeneralRe: For starters you can remap your keyboard but also, the world... Pin
Ed Nutting23-Apr-11 22:57
Ed Nutting23-Apr-11 22:57 
GeneralReason for my vote of 5 exclent Pin
lovepreet_shota18-Jun-11 17:15
lovepreet_shota18-Jun-11 17:15 
GeneralThis is all well and good unless of course my keyboards numb... Pin
Ed Nutting23-Apr-11 2:40
Ed Nutting23-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... Pin
AspDotNetDev23-Apr-11 18:51
protectorAspDotNetDev23-Apr-11 18:51 
GeneralRe: what if small digits or negative values? I think using regex... Pin
Huisheng Chen24-Apr-11 3:05
Huisheng Chen24-Apr-11 3:05 
GeneralExplanation needed Pin
Indivara18-Apr-11 12:05
professionalIndivara18-Apr-11 12:05 
GeneralRe: Explanation needed Pin
Prasad_Kulkarni27-Dec-11 18:52
Prasad_Kulkarni27-Dec-11 18:52 

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

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