JavaScript - How To Check if String Contains Only Digits?
Last Updated :
06 Dec, 2024
A string with only digits means it consists solely of numeric characters (0-9) and contains no other characters or symbols. Here are the different methods to check if the string contains only digits.
1. Using Regular Expression (RegExp) with test() Method
The most efficient way to check if a string contains only digits is by using a regular expression with the test() method. This method checks if the string matches the pattern for digits.
JavaScript
let s = "123456";
let regex = /^\d+$/;
let res = regex.test(s);
console.log(res);
- ^ asserts the start of the string.
- \d+ matches one or more digits.
- $ asserts the end of the string.
- test() method returns true if the string contains only digits and false otherwise.
2. Using isNaN() Method
The isNaN() function can be used to check if a value is NaN (Not-a-Number). If a string is entirely composed of digits, it will convert to a valid number, and isNaN() will return false. If it contains anything other than digits, it will return true.
JavaScript
let s = "123456";
let res = !isNaN(s);
console.log(res);
- isNaN(s) returns true if s is not a number and false if it is a valid number.
- The ! negates the result to check if the string is a valid number.
3. Using Array.prototype.every() with split()
You can use the every() method in combination with split() to check if every character in the string is a digit. This method checks each character in the string and ensures all characters are digits.
JavaScript
let s = "123456";
let res = s.split('').every(char =>
char >= '0' && char <= '9');
console.log(res);
- split('') splits the string into an array of individual characters.
- every() checks that every character in the array is a digit between '0' and '9'.
4. Using parseInt() Method
You can also use the parseInt() function to convert the string to a number and check if the result matches the original string. If the string contains anything other than digits, the conversion will not match.
JavaScript
let s = "123456";
let res = parseInt(s) == s;
console.log(res);
- parseInt(s) converts the string to an integer.
- If the conversion result is the same as the origihnal string, it means the string contained only digits.
5. Using Number() Method
The Number() method can also be used to convert a string to a number. If the string contains only digits, it will return a valid number; otherwise, it will return NaN.
JavaScript
let s = "123456";
let res = !isNaN(Number(s));
console.log(res);
- Number(s) attempts to convert the string to a number.
- isNaN() checks if the result is NaN or a valid number.
Conclusion
- Regular Expressions (^\d+$) are the most simple and efficient way to check for digit-only strings.
- isNaN() and Number() methods are good for general number validation.
- split() and every() provide a more manual approach but are still effective for checking individual characters.
- For most use cases, the regular expression method is preferred due to its simplicity and readability.