JavaScript

How to Check if String Contains Only Numbers and Special Characters in JavaScript

A string is created with multiple characters, some of which can be special characters, numbers, or letters. In JavaScript, you can check whether a string contains only numbers and special characters, or it contains letters also. To do so, utilize the JavaScript built-in methods with regular expressions that can validate the strings based on specific patterns.

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:

regexPattern.test(string);

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:

var str = "71#8*6%0!72(8)-5%";

Now, create a regex pattern for searching numbers and special characters in a string and store it in a variable “pattern”:

var pattern = /^[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~\d]*$/g;

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”:

var res = pattern.test(str);

Finally, we will print the resultant value on the console:

console.log(res);

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:

string.match(regexPattern);

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:

var res = str.match(pattern) ? "true" : "false";

Finally, print the resultant value on the console using the “console.log()” method:

console.log(res);

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.