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
$
: End
Regex Visualization:
Demo
If you want to allow only numbers, you can use input-number
and pattern
<input type="number" pattern="d+" />
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…