This tutorial will describe the methods to determine whether the string contains only numbers and special characters.
How to Check if String Contains Only Numbers and Special Characters in JavaScript?
To ensure that the string only contains special characters and numbers, use the below-listed methods:
Let’s see how these methods will work!
Method 1: Check if String Contains Only Numbers and Special Characters Using test() Method
If a string contains any number or special characters, it can be checked using a regular expression, also known as a regex pattern, passed in the “test()”. If a single special character and a number are found in a string, it returns “true” or “false”. Also, note that the test() method is a case-sensitive method.
Syntax
Follow the below-mentioned syntax for using the test() method:
Here, the “regexPattern” is a regular expression for special characters and numbers that will be checked in a string with the help of the test() method.
Example
In this example, we will first create a variable named “str” that stores a string containing numbers and special characters:
Now, create a regex pattern for searching numbers and special characters in a string and store it in a variable “pattern”:
Here, the pattern contains all the special characters and for numbers, where “\d” indicates all digits 0 to 9.
Then, call the test() method by passing a string as an argument and store its returned result in the variable “res”:
Finally, we will print the resultant value on the console:
As you can see, the output returns “true” which means the string contains numbers and special characters:
Let’s see another method!
Method 2: Check if String Contains Only Numbers and Special Characters Using match() Method
Use the “match()” method to see if the string only contains special characters and numbers. It compares a string to a regular expression or regex pattern. If a match occurs, it returns an array of all matched occurrences; else, it returns null. The ternary operator or conditional statement is also used with the match() method that returns a boolean value, depending on the evaluated condition.
Syntax
To use the match() method, follow the given provided syntax:
Here, the “regexPattern” is the regular expression for special characters and numbers that will be matched in a string.
Example
Here, we will use the same string and pattern created in the above example. Then, invoke the match() method by passing the regex pattern as an argument, which matches the string against it and outputs “true” if the match is found; else, it returns “false” using the ternary operator:
Finally, print the resultant value on the console using the “console.log()” method:
Output
We have provided the best ways for checking whether the string contains only numbers and special characters in JavaScript.
Conclusion
For verifying if the string contains only numbers and special characters, use the JavaScript built-in provided methods such as the test() or the match() method. Both methods compare the string to the pattern; the test() method returns a boolean value, true or false, while match() returns an array of matched occurrences. In this tutorial, we described the methods for verifying whether the string contains only numbers and special characters with the help of detailed examples.