In the previous post, we showed you 3 ways to check whether the String contains only alphabetic characters. Today we will show you solutions for how to check if String contains only Letters and Numbers in JavaScript. Read on to learn more.
Check if String contains only Letters and Numbers in JavaScript
Like the previous article, we will also show you 3 simple ways this time.
Using test()
method
You can read more syntax of test()
here.
We will use the regex string: /^[A-Za-z0-9]+$/
to call test()
and execute the test on the given String. Test()
will return true if String exists nothing except alphanumeric characters. Otherwise, if there is a non-alphanumeric character, it will return false.
Let us explain the regex string: /^[A-Za-z0-9]+$/
to you.
^
: marks the start of the regex string.[ ]
: matches the characters inside it.+
: occur 1 or more times.A-Z
: represents the letters from A to Z.a-z
: represents the letters from a to z.0-9
: represent digits from 0 to 9.$
: marks the end of the regex string.
See the example below for how to use test()
:
const account = 'btwr2022';
const email = '[email protected]';
function checkLettersAndNumbers(str) {
// Use test() to check the str
let isAlphanumeric = /^[a-zA-Z0-9]+$/.test(str);
if (isAlphanumeric) {
console.log(`${str} contains only Letters and Numbers!`);
return true;
} else {
console.log(`${str} does not contain only Letters and Numbers!`);
return false;
}
}
checkLettersAndNumbers(account); // true
checkLettersAndNumbers(email); // false
Output:
btwr2022 contains only Letters and Numbers!
[email protected] does not contain only Letters and Numbers!
Using search()
method
The search()
method will search for a match of the regex string in the given String and return the position of the first match in the given String. If it returns -1, it means no value matches the regex string.
You can learn the syntax of search()
here.
First, we will pass the regex mentioned above into the search()
method to perform the search.
Next, we use the if else statement to check the return. If the return is -1, then our String contains non-alphanumeric characters. In contrast, our String exists nothing except alphanumeric characters.
const account = 'btwr2022';
const email = '[email protected]';
function checkLettersAndNumbers(str) {
// Use search() to check the str
let position = str.search(/^[a-zA-Z0-9]+$/);
if (position === -1) {
// Position = -1 -> A non-alphanumeric character exists
console.log(position); // -1
console.log(`${str} does not contain only Letters and Numbers!`);
return false;
} else {
// Position != -1 -> Contains only letters and numbers
console.log(position); // 0
console.log(`${str} contains only Letters and Numbers!`);
return true;
}
}
checkLettersAndNumbers(account); // true
checkLettersAndNumbers(email); // false
Output:
0
btwr2022 contains only Letters and Numbers!
-1
[email protected] does not contain only Letters and Numbers!
Using match()
method
Before reading on, you can read more about the match()
method syntax here.
Similar to the search()
method, we have to pass the regex: /^[a-zA-Z0-9]+$/
to the match()
method to start matching. The match()
method will return null if there are no matches in the String or return an array containing the characters that match the regex string.
const account = 'btwr2022';
const email = '[email protected]';
function checkLettersAndNumbers(str) {
// Use match() to check the str
const matches = str.match(/^[a-zA-Z0-9]+$/);
if (matches === null) {
// Matches = null -> A non-alphanumeric character exists
console.log(matches); // null
console.log(`${str} does not contain only Letters and Numbers!`);
return false;
} else {
// Matches != null -> Contains only letters and numbers
console.log(matches); // An Array
console.log(`${str} contains only Letters and Numbers!`);
return true;
}
}
checkLettersAndNumbers(account); // true
checkLettersAndNumbers(email); // false
Output:
["btwr2022"]
btwr2022 contains only Letters and Numbers!
null
[email protected] does not contain only Letters and Numbers!
Summary
Through this article about how to check if String contains only Letters and Numbers in JavaScript, we hope to make it easier for you to work with String. If you have any questions, please leave a comment below. Thank you for reading!

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java