Click here to Skip to main content
12,399,362 members (58,587 online)
Click here to Skip to main content
Add your own
alternative version

Stats

64.1K views
26 bookmarked
Posted

Text box to accept only number

, 23 Apr 2011 CPOL
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.

You may also be interested in...

Comments and Discussions

 
GeneralRe: The keyboard remapping just assigns different characters to ... Pin
AspDotNetDev24-Apr-11 0:44
protectorAspDotNetDev24-Apr-11 0:44 

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.

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