• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Check if String Contains Numbers

Using JavaScript to Check if String Contains Numbers

September 13, 2022 Leave a Comment

There are many ways we can use JavaScript to check if a string contains numbers. We will go over two simple methods below to do this.

The first uses the JavaScript test() method and a regex expression.

/\d/.test(someString);

The regex expression /\d/ will check for any number from 0-9. The test() method will check the string you pass as a parameter to see if a number is contained in the string due to the regex expression. If it does find it, it returns true. Otherwise, it returns false.

We can wrap this code in a function to make it easy to reuse.

function stringContainsNumber(str){
  return /\d/.test(str);
};

Now let’s test this function with a couple of examples:

function stringContainsNumber(str){
  return /\d/.test(str);
};

var string1 = "This is a string with no numbers";
var string2 = "This is a string with numbers 1234567890";
var string3 = "This is a string a number 1";
var string4 = "123456";
var string5 = 12;

console.log(stringContainsNumber(string1));
console.log(stringContainsNumber(string2));
console.log(stringContainsNumber(string3));
console.log(stringContainsNumber(string4));
console.log(stringContainsNumber(string5));

#Output
false
true
true
true
true

Using JavaScript match() Method to Check if String Contains Numbers

We can also use the match() method to check if a string contains numbers along with a regex expression as we did with the test method.

someString.match(/\d/);

The match() method will search the string, someString, for whatever you pass in as the parameter, which in this case is our regex expression for a number. The match() method will return an array with all of the matches, otherwise if a match is not found, it will return null.

Let’s put this code in a function as we did with the test() method.

function stringContainsNumber(str){
  if( str.match(/\d/) != null ){
    return true;
  } else {
    return false;
  }
};

And finally, let’s test this function as we did with our first function.

Now let’s test this function with a couple of examples:

function stringContainsNumber(str){
  if( str.match(/\d/) != null ){
    return true;
  } else {
    return false;
  }
};

var string1 = "This is a string with no numbers";
var string2 = "This is a string with numbers 1234567890";
var string3 = "This is a string a number 1";
var string4 = "123456";

console.log(stringContainsNumber(string1));
console.log(stringContainsNumber(string2));
console.log(stringContainsNumber(string3));
console.log(stringContainsNumber(string4));

#Output
false
true
true
true

Hopefully this article has been useful for you to learn how to use JavaScript to check if a string contains numbers.

Other Articles You'll Also Like:

  • 1.  How to Hide a Div in JavaScript
  • 2.  Using JavaScript to Check if Number is Divisible by Another Number
  • 3.  Using JavaScript to Remove All Pipe Characters From String
  • 4.  JavaScript toLocaleString – Display the Date and Time Using JavaScript
  • 5.  Sum the Digits of a Number in JavaScript
  • 6.  Remove Commas From a String in JavaScript
  • 7.  How to Get the Cursor Position in JavaScript
  • 8.  Using JavaScript to Return String
  • 9.  JavaScript Map Size – Find Length of JavaScript Map
  • 10.  JavaScript Change Text Color of a Paragraph

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x