Check if string contains only numbers and letters javascript

Check if String contains only Letters and Numbers #

Use the test() method on the following regular expression to check if a string contains only letters and numbers - /^[A-Za-z0-9]*$/. The test method will return true if the regular expression is matched in the string and false otherwise.

Copied!

const str1 = 'hello42'; const str2 = 'contains spaces'; const str3 = 'with symbols !@#$%^&'; function onlyLettersAndNumbers(str) { return /^[A-Za-z0-9]*$/.test(str); } console.log(onlyLettersAndNumbers(str1)); // 👉️ true console.log(onlyLettersAndNumbers(str2)); // 👉️ false console.log(onlyLettersAndNumbers(str3)); // 👉️ false

If you need to also allow spaces, hyphens, underscores, or other characters, scroll down to the next code snippet.

We called the RegExp.test method to check if the string contains only letters and numbers.

The forward slashes / / mark the beginning and end of the regular expression.

The caret ^ matches the beginning of the input, whereas the dollar $ matches the end of the input.

The square brackets [] are called a character class. We match 3 ranges in our character class:

  • all uppercase letters A-Z
  • all lowercase letters a-z
  • all numbers 0-9

The asterisk * matches the preceding item (our character class) zero or more times.

If you need a regex cheatsheet, check out this one from MDN.

If you also need to match spaces, dashes, underscores, or any other character, add the character between the square brackets [].

Copied!

const str1 = 'hello42'; const str2 = 'contains spaces'; const str3 = 'with symbols !@#$%^&'; function lettersNumbersSpacesDashes(str) { return /^[A-Za-z0-9 -]*$/.test(str); } console.log(lettersNumbersSpacesDashes(str1)); // 👉️ true console.log(lettersNumbersSpacesDashes(str2)); // 👉️ true console.log(lettersNumbersSpacesDashes(str3)); // 👉️ false

In this example we match all letters, numbers, spaces and dashes. You could extend this regex according to your use case.

If instead of checking whether the string contains only letters and numbers, you want to remove all characters that are not letters or numbers, use the replace method.

Copied!

const str = 'WITH symbols !@#$%^&'; const replaced = str.replace(/[^a-z0-9]/gi, ''); console.log(replaced); // 👉️ WITHsymbols

We used the String.replace method to get a new string with all non-letters and non-numbers replaced by an empty string.

When used inside of the square brackets [], a caret ^ symbol means "not the following". It's basically a negated match.

In the example, we replace all non-letters and non-numbers with an empty string.

We used the g (global) flag because we want to match all occurrences of non letters and non-numbers in the string and not just the first occurrence.

The i flag allows us to match all letters in a case-insensitive manner.

Further Reading #

  • Remove all non-alphanumeric Characters from String in JS
  • Check if String contains only Letters and Spaces in JS

How do you check if a string contains numbers and letters?

Check if a string is numeric, alphabetic, alphanumeric, or ASCII.
Check if a string contains only decimal: str.isdecimal().
Check if a string contains only digits: str.isdigit().
Check if a string contains only numeric: str.isnumeric().
Check if a string contains only alphabetic: str.isalpha().

How can you check if string contains only digits JavaScript?

Use the test() method to check if a string contains only digits, e.g. /^[0-9]+$/. test(str) . The test method will return true if the string contains only digits and false otherwise.

How do you check if a string is a letter or number JavaScript?

To check if a character is a letter, call the test() method on the following regular expression - /^[a-zA-Z]+$/ . If the character is a letter, the test method will return true , otherwise false will be returned.

How do you check if a string contains only alphabets and numbers in Java?

To check whether a String contains only unicode letters or digits in Java, we use the isLetterOrDigit() method and charAt() method with decision-making statements. The isLetterOrDigit(char ch) method determines whether the specific character (Unicode ch) is either a letter or a digit.