EN
JavaScript - check if string contains only numbers
14 points
In this article, we want to show how in JavaScript using simple regular expressions, check if the string contains only numbers.
Example expressions:
/^[0-9]+$/
/^[0-9]{1,}$/
/^\d+$/
/^\d{1,}$/
For example, we can select the first one, and do some tests with match()
function:
xxxxxxxxxx
1
var expression = /^[0-9]+$/;
2
var text = '1000500100900';
3
4
if (text.match(expression)) {
5
console.log('I am number!');
6
}
xxxxxxxxxx
1
var expression = /^[0-9]+$/;
2
var text = '1000500100900';
3
4
if (expression.test(text)) {
5
console.log('I am number!');
6
}