JavaScript - check if string contains only numbers

14 points
Created by:

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

Example expressions:

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

As example, we can select first one, and do some test with match() function:

// ONLINE-RUNNER:browser;

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

if (text.match(expression)) {
    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?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!

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.

❤️💻 🙂