Allow only numeric values or digits in a TextBox using JavaScript or jQuery

The example, allow only numeric values or digits in a textbox using javascript or jquery, shows how to accept only numbers or digits in a textbox using JavaScript or jQuery. Sometimes we need to accept only numbers for a textbox instead of validation then it comes handy. In this type of validation user will not be able to type a non-numeric values.

This tutorial shows both validation and accepting only numbers in a textbox. This example also shows inline JavaScript function call with the input textbox. We will see each example one by one.

Example – using JavaScript

The javascript function checks for charcode when a key is pressed in the keyboard and it check if it does not fall between 48 and 57 then it is other than digit. So we show the error message to the user.

We call the javascript function onkeypress javascript event. You can also call this function onkeyup javascript function. Continue reading “Allow only numeric values or digits in a TextBox using JavaScript or jQuery”