Check If A String Contains Only Digits In JavaScript

To check if a String contains only digits in JavaScript, use the RegExp.test() method. The RegExp.test() method accepts a regular expression as an argument and returns true or false depending on whether the given string matches the regular expression. Alternatively you can use a for Loop to iterate over the characters in the string and check if each character is a digit.

Check If A String Contains Only Digits In JavaScript

check if a string contains only digits in javascript

RegExp.test() Method In JavaScript

The RegExp.test() method in JavaScript returns true or false when a pattern match is found in a string. This method is used to check if a string contains a specified word or a substring.

The test() method is executed on a regular expression and the value is returned based on the result of the match. If no match is found, the value returned is false.

For example, the following code snippet uses the test() method to check if a string contains the word “cat”:

var regexp = /cat/;

console.log(regexp.test(“The cat in the hat”)); //returns true

Similarly, the following code snippet uses the test() method to check if a string contains the substring “hat”:

var regexp = /hat/;

console.log(regexp.test(“The cat in the hat”)); //returns true

On the other hand, if there is no match found, the test() method will return false, as shown in this example:

var regexp = /bat/;

console.log(regexp.test(“The cat in the hat”)); //returns false

Check If A String Contains Only Digits Using RegExp.test()

If you want to check if a string contains only digits in JavaScript, you can use the following regular expression:

var regex = /^\d+$/;

console.log(regex.test(“12345”)); // true

console.log(regex.test(“1234a5”)); // false

Alternatively, you can use the String.prototype.search() method with a regular expression containing the ^ and $ anchors to ensure that the whole string is matched:

console.log(“12345”.search(/^\d+$/) !== -1); // true

console.log(“1234a5”.search(/^\d+$/) !== -1); // false

Note that the \d character class matches any digit from 0 to 9, so the regular expression will also match strings like “0123”, “001234”, etc.

The ‘\’ in front of the ‘d’ is needed to escape it, as ‘\d’ is a special character in regular expressions.

The ‘+’ character after the ‘\d’ means that one or more digits must be present for the regular expression to match.

The ‘^’ anchor marks the beginning of the string, and the ‘$’ anchor marks the end of the string, so this regular expression will only match strings that contain nothing but digits.

You can even use [0-9] instead of \d if you want, it means the same thing.

var regex = /^[0-9]+$/;

console.log(regex.test(“12345”)); // true

console.log(regex.test(“1234a5”)); // false

If you want to allow whitespace characters before and after the digits as well, you can use the \s character class:

var regex = /^\s*\d+\s*$/;

console.log(regex.test(” 12345 “)); // true

console.log(regex.test(” 12a345 “)); // false

The \s* before and after the \d+ matches any number of whitespace characters, including zero.

You can also use the String.prototype.trim() method to remove whitespace from both ends of a string before testing it against a regular expression:

console.log(” 12345 “.trim().search(/^\d+$/) !== -1); // true

console.log(” 12a345 “.trim().search(/^\d+$/) !== -1); // false

If you want to check if a string contains only hexadecimal digits, you can use the following regular expression:

var regex = /^[0-9a-fA-F]+$/;

console.log(regex.test(“12345”)); // true

console.log(regex.test(“1234a5”)); // true

console.log(regex.test(“1234g5”)); // false

If you want to check if a string contains only octal digits, you can use the following regular expression:

var regex = /^[0-7]+$/;

console.log(regex.test(“12345”)); // true

console.log(regex.test(“1234a5”)); // false

If you want to check if a string contains only binary digits, you can use the following regular expression:

var regex = /^[01]+$/;

console.log(regex.test(“12345”)); // false

console.log(regex.test(“010101010”)); // true

If you want to check if a string contains only alphanumeric characters (letters and digits), you can use the following regular expression:

var regex = /^[0-9a-zA-Z]+$/;

console.log(regex.test(“12345”)); // true

console.log(regex.test(“abcde”)); // true

console.log(regex.test(“1234a5”)); // true

console.log(regex.test(“[email protected]#$%^&*()_+”)); // false

Check If A String Contains Only Digits Using for Loop

If you don’t want to use a regular expression, you can loop through the string and check each character if it’s a digit using the charCodeAt() method:

function isDigit(str) {

  for (var i = 0; i < str.length; i++) {

    if (str.charCodeAt(i) < 48 || str.charCodeAt(i) > 57) return false;

  }

  return true;

}

console.log(isDigit(“12345”)); // true

console.log(isDigit(“1234a5”)); // false

This approach is more verbose but has the advantage of being able to handle non-ASCII characters, like accented letters, that the regular expression approach above cannot.

Conclusion

In this article, you’ve learned how to check if a string contains only digits in JavaScript. You can use regular expressions or loop through the string character by character to achieve this.

I hope you found this article helpful. If you have any questions, please feel free to post them in the comments section below.

Cheers!

Leave a Comment