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 t
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