Javascript: Check if a string contains numbers

There is a general requirement while developing with javascript to check if a string contains numbers. This article will discuss checking if a string contains numbers in a javascript string using different methods and example illustrations.
We will be creating custom functions to check if a given string contains numbers and examples of their usage.

Table of Contents:-

Check if a string contains numbers in javascript using Regex

Regex: the regular expressions are patterns used to match character combinations in strings.

Example:-

Check if the strings -> “This is 123 online coaching” and “This is gold online coaching” contain numbers.

Function:-

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

Usage:-

 let input_string1 = "This is 123-online coaching"
 let input_string2 = "This is gold-online coaching"
 console.log(stringContainsNumber(input_string1)? "The input string contain numbers":"The input string does not contain numbers")
 console.log(stringContainsNumber(input_string2)? "The input string contain numbers":"The input string does not contain numbers")

Output:-

The input string contain numbers
The input string does not contain numbers

Another way to write and use the function for checking if a javascript string contains a number is:

Function:-

function stringContainsNumber(_string)
  {
  let matchPattern =_string.match(/\d+/g);
  if (matchPattern != null) {
    console.log('The input string contain numbers');
   }
  else{
  console.log('The input string does not contain numbers');
  }

Usage:-

let input_string1 = "This is 123-online coaching"
let input_string2 = "This is gold-online coaching"
stringContainsNumber(input_string1)
stringContainsNumber(input_string2)

Output:-

The input string contain numbers
The input string does not contain numbers

Check if a string contains numbers in javascript using for loop and isNaN()

The isNan() function in javascript determines if the input passed is a number or not. This function returns true if the parameter is Not a Number. Else returns false.

Example:-

Check if the strings -> “This is 123 online coaching” and “This is gold online coaching” contain numbers.

Function:-

function stringContainsNumber(_input){
  let string1 = String(_input);
  for( let i = 0; i < string1.length; i++){
      if(!isNaN(string1.charAt(i)) && !(string1.charAt(i) === " ") ){
        return true;
      }
  }
  return false;
}

Usage:-.;

let input_string1 = "This is 123 online coaching"
let input_string2 = "This is gold online coaching"
console.log(stringContainsNumber(input_string1)?"The input string contain numbers":"The input string does not contain numbers")
console.log(stringContainsNumber(input_string2)?"The input string contain numbers":"The input string does not contain numbers")

Output:-

The input string contain numbers
The input string does not contain numbers

Read More:

We hope this article helped you to check if a javascript string contains numbers. Good Luck !!!

Advertisements

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top