Languages
[Edit]
EN

JavaScript - check if string contains only numbers

14 points
Created by:
Savannah
319

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 

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