Click here to Skip to main content
15,880,905 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

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 11.3K   1   1
$(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)


Written By
Software Developer
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

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
Member 847608323-Feb-12 1:37 
i tried this code. But this has an error If i enter 1.2.1.1 or any thing similar it is accepting. which it should not

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.