You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Problem

There is a text field only allow numerical values. Need to enforce keyboard entry such that users can only input numerical values.

Code

There are a number resources online that describes this, e.g. http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jqueryhttp://snipt.net/GerryEng/jquery-making-textfield-only-accept-numeric-values, etc

For example, if your textfield id is "numeric", you could use the sample script:

<script>
$(document).ready(function() {
    $("#numeric").keydown(function(event) {
        // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 ) {
            // 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.preventDefault();
            }
        }
    });
});
</script>
  • No labels