Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Text box to accept only number

, 27 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Here is a really simple code for a numeric text box and it will work in all browsers. Only works for web applications. function CheckNumeric(e) { if (window.event) //...

Here is a really simple code for a numeric text box and it will work in all browsers. Only works for web applications.

<head  runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function CheckNumeric(e) {
 
            if (window.event) // IE 
            {
                if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
                    event.returnValue = false;
                    return false;
 
                }
            }
            else { // Fire Fox
                if ((e.which < 48 || e.which > 57) & e.which != 8) {
                    e.preventDefault();
                    return false;
 
                }
            }
        }
     
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:textbox id="TextBox1" runat="server" onkeypress="CheckNumeric(event);" 
             xmlns:asp="#unknown"></asp:textbox>
    </form>
</body></head>

License

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

About the Author

Nigam Patel
Software Developer
India India
I am working as software engineer since last 3.5 year.
i am working in .net technologies.

Comments and Discussions

 
BugTab and Enter key won't work too PinprofessionalAnkur\m/5-Apr-13 0:05 
GeneralMy vote of 5 PinmemberV!jAy pAnT28-Jun-12 23:20 
GeneralRe: My vote of 5 PinmemberKManishS2-Feb-13 0:05 
GeneralReason for my vote of 5 Good PinmemberVinod Satapara27-Dec-11 18:18 

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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 27 Dec 2011
Article Copyright 2011 by Nigam Patel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid