Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Objective:

To enforce keyboard entry so that users can only input numerical values for a text field

Code:

There are several online resources that describe 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 text field ID is "numeric", you can use this sample script:

Code Block
<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>