Navigate back to the homepage

How to check if a string is alphanumeric in JavaScript?

Infinitbility
JavaScript
June 6th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To check string is alphanumeric, use this regex /^[a-z0-9]+$/i if it gets true means the string contains only alphabets and numbers.

Today, I’m going to show How do I check if a string is alphanumeric in javascript, here I will use the javascript test() method and regex as mentioned above to validate whether the string is alphanumeric or not.

Let’s start the today’s tutorial How do you check if a string is alphanumeric in javascript?

In the following example, we are going to do

  1. create function isAlphanumeric() and take the input string as a parameter.
  2. Use regex to validate alphanumeric string
  3. return boolean value based on string

let’s write the code.

1function isAlphanumeric(str) {
2 return /^[a-z0-9]+$/i.test(str)
3}
4
5isAlphanumeric("infinitbility"); // ✅ true
6isAlphanumeric("23"); // ✅ true
7isAlphanumeric("23$"); // ⛔️ false

In the above program, we have created a custom function isAlphanumeric() and passed a difference string example to validate the function.

let’s check the output.

javascript, check if a string contains only alphabets and numbers example
javascript, check if a string contains only alphabets and numbers or not example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a number is divisible by another number in javascript?

Example of check if a number is divisible by another number in javascript with step by step guide.

June 5th, 2022 · 1 min read

How to check if a number is between two values in javascript?

Example of check if a number is between two values in javascript with step by step guide.

June 4th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]