How to check if a textbox contains numbers only?
While googling I came across this. But I'm wondering if isNumeric can be used for this purpose or if there are more simpler ways of checking if a textbox has a numeric value.
var query = $('#myText').val();
if (parseFloat(query) == NaN) {
    alert("query is a string");
} else {
    alert("query is numeric");
}
                Switch to design view from markup view. Now click on design view and press Ctrl + Alt + X . From the toolbox that opens click on Validation and drag a compare validator near your TextBox . Right click on the compare validator and choose properties, now locate ErrorMessage and write "Alert: Type only Number".
The jQuery $. isNumeric() method is used to check whether the entered number is numeric or not. $. isNumeric() method: It is used to check whether the given argument is a numeric value or not.
You can check if the user has entered only numbers using change event on input and regex.
$(document).ready(function() {
    $('#myText').on('change', function() {
        if (/^\d+$/.test($(this).val())) {
            // Contain numbers only
        } else {
            // Contain other characters also
        }
    })
});
REGEX:
/: Delimiters of regex^: Starts with\d: Any digit+: One or more of the preceding characters$: EndRegex Visualization:

Demo
If you want to allow only numbers, you can use input-number and pattern
<input type="number" pattern="\d+" />
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With