• 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 Only Numbers

Using JavaScript to Check if String Contains Only Numbers

September 13, 2022 Leave a Comment

We can use JavaScript to check if a string contains only numbers. One of the simplest ways to do this is to use the JavaScript test() method and a regex expression.

/^\d+$/.test(someString);

The regex expression \d will check for any number from 0-9. The rest of the regex expression checks that every character in the string is equal to a number 0-9. Here is a useful resource on regex expressions if you want to learn more about them. The test() method will check the string you pass as a parameter to see if only numbers are contained in the string due to the regex expression. If it does find all numbers, it returns true. Otherwise, it returns false.

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

function stringContainsOnlyNumbers(str){
  return /^\d+$/.test(str);
};

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

function stringContainsOnlyNumbers(str){
  return /^\d+$/.test(str);
};

var string1 = "This is a string with numbers 1234567890";
var string2 = "123456";
var string3 = " 123456";
var string4 = 12;
var string5 = "$1000";
var string6 = "10.234";


console.log(stringContainsOnlyNumbers(string1));
console.log(stringContainsOnlyNumbers(string2));
console.log(stringContainsOnlyNumbers(string3));
console.log(stringContainsOnlyNumbers(string4));
console.log(stringContainsOnlyNumbers(string5));
console.log(stringContainsOnlyNumbers(string6));

#Output
false
true
false
true
false
false

Notice in our last example the string “10.234” returns false because “.” is not a number. If we wanted to include this as being a number, we could just add “.” to our regex expression.

function stringContainsOnlyNumbers(str){
  return /^\d.+$/.test(str);
};

var string1 = "10.234";

console.log(stringContainsOnlyNumbers(string1));

#Output
true

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

Other Articles You'll Also Like:

  • 1.  Convert String to Float in JavaScript
  • 2.  JavaScript Trunc with Math.trunc() Method
  • 3.  JavaScript If Not – Check if a Condition is False
  • 4.  Using JavaScript to Remove Backslash From a String
  • 5.  Using JavaScript to Multiply All Elements in an Array
  • 6.  Using JavaScript to Check if Array is Empty
  • 7.  Using JavaScript to Get Date Format in yyyy mm dd
  • 8.  JavaScript if else Shorthand Statement
  • 9.  How to Clear an Input Field in JavaScript
  • 10.  Using JavaScript to Check if String Contains Letters

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