Check if String Contains Numbers in JavaScript (Easy Method!).

0

In this blog you’ll learn how to check if the string contains the numeric value or not using a simple JavaScript method.


check if string contain a numeric values


You can easily check the string contains a number or numeric value by JavaScript match method with regular expression.


We’ll use the match method with regular expressions for numbers ([0-9]). If a match is found we’ll show output that string contains a number, if a match is not found we show output that the string does not contain a number.


Take a look at the code below:


JavaScript


    // String does not contain a numeric value.
    let string = "hello welcome to maketechstuff.com";

    console.log("String: ", string);

    if (string.match(/[0-9]/g)) {
        console.log("Output: String contain a number");
    } else {
        console.log("Output: String does not contain a number.");
    }

  


Output:


string does not contain a numeric value (example).


Now let's add number inside a string an see the output.


JavaScript


    // String contain a numeric value.
    let string = "Thanks 4 visiting maketechstuff.com";

    console.log("String: ", string);

    if (string.match(/[0-9]/g)) {
        console.log("Output: String contain a number");
    } else {
        console.log("Output: String does not contain a number.");
    }

  


Output:


string contain a numeric value (example).


So that's how you can check if the string contains the numbers or not using JavaScript. Feel free to leave any questions or suggestions in the comments below.


You may like:

  1. How to extract the numbers present in a string using JavaScript.
  2. How to remove the numeric values from a string using JavaScript.
  3. How to check if string contain the special character using JavaScript.

Tags

Post a Comment

0Comments

Share your thoughts.

Post a Comment (0)
To Top