Languages
[Edit]
EN

JavaScript - check if string contains only numbers

14 points
Created by:
Savannah
559

In this article, we want to show how in JavaScript using simple regular expressions, check if the string contains only numbers.

Example expressions:

  1. /^[0-9]+$/
  2. /^[0-9]{1,}$/
  3. /^\d+$/
  4. /^\d{1,}$/ 

For example, we can select the first one, and do some tests with match() function:

// ONLINE-RUNNER:browser;

var expression = /^[0-9]+$/;
var text = '1000500100900';

if (text.match(expression)) {
    console.log('I am number!');
}

 

Alternative solution

// ONLINE-RUNNER:browser;

var expression = /^[0-9]+$/;
var text = '1000500100900';

if (expression.test(text)) {
    console.log('I am number!');
}

 

See also

  1. JavaScript - integer regular expression 

Alternative titles

  1. JavaScript - how to check if a string contains only numbers?
  2. JavaScript - check if string is number
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - String (popular problems)

JavaScript - check if a string only contains numbers
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join