Click here to Skip to main content
15,400,390 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick
Posted 11 Feb 2012

Tagged as

Stats

10.6K views
1 bookmarked

Allow only Numeric values in ASP Text box control using JavaScript

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Feb 2012CPOL
$(document).re...
JavaScript
$(document).ready(function () {
    $('#TextboxID').keydown(function (event) {
        if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9) {
            // let it happen, don't do anything
        }
        else {
            // Ensure that it is a number and stop the keypress
            if ((event.keyCode >= 48 && event.keyCode <= 57) || 
                  (event.keyCode >= 96 && event.keyCode <= 105) || 
                   event.keyCode == 190) {

            }
            else {
                event.preventDefault();
            }
        }
    });
});

License

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

Share

About the Author

Emad Mokhtar
Software Developer
Egypt Egypt
No Biography provided

Comments and Discussions

 
Generali tried this code. But this has an error If i enter 1.2.1.1 ... Pin
Member 847608323-Feb-12 1:37
MemberMember 847608323-Feb-12 1:37 

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.