Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Allow only Numeric values in ASP Text box control using JavaScript

0.00/5 (No votes)
11 Feb 2012CPOL 11.7K  
$(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)