You can check if the user has entered only numbers using the change event on the input and in the regular expression.
$(document).ready(function() { $('#myText').on('change', function() { if (/^\d+$/.test($(this).val())) {
REGEX:
/ : regex delimiters^ : starts with\d : Any number+ : one or more previous characters$ : End
Regular Expression Visualization:

Demo
If you want to allow only numbers, you can use input-number and pattern
<input type="number" pattern="\d+" />
source share