Answer the question
In order to leave comments, you need to log in
How to correctly check a string for validity using regular expressions?
I'm trying to validate a string using regular expressions.
Examples found on the Internet are based on match()
or test()
But this alone is not enough - if you just do str.match(/[0-9]/)
it, the result will be non-empty if str
there is at least one number in it. And in this example, it is necessary that all characters in str
be numbers
. In general, for now, I do this:
var str = '123';
var strIsValid = !!str.match(/[0-9]/) && str.match(/[0-9]/).input === str;
Answer the question
In order to leave comments, you need to log in
/^[0-9]$/.test(str)
or option for numbers not exceeding 9007199254740991parseInt(str, 10).toString() === str
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question