How To Do Alphanumeric Validation In Javascript 2021

Alphanumeric Validation In Javascript
Table of Contents

Today we will see how to do Alphanumeric validation in javascript. If you are looking for a solution to this problem then you are at the right. I will tell you how it is done and provide you with all the code.

If you are looking for coding projects then we have created many coding projects which you can check here – View coding projects.

What Is Alphanumeric Validation In Javascript?

First let’s see what is alphanumeric. Alphanumeric is any alphabet from (A to Z or a to z) or any number from (0 to 9) so alphanumeric validation makes sure that all the characters entered in an input are alphanumeric.

How Do We Validate Alphanumeric In Javascript?

To validate we will use a regular expression you may call it regex and we will see if the entered input matches the regular expression. So now let’s see this in code. It is very simple and easy to do. Just follow me.

This is the Regular Expression which we are going to use to validate:

/^[0-9a-zA-Z]+$/

Alphanumeric Validation In Javascript

I have explained to you what is alphanumeric and how do we validate it. Now we will see how to do alphanumeric validation in javascript. If you want to find coding solutions you can check this website here – Visit here 

ADD this HTML code

				
					<form id="form">
    <input type="text" id="text" placeholder="Enter your username">
    <button type="submit">Submit</button>
</form>
				
			

Add this Javascript code

				
					const form = document.getElementById('form')
const text = document.getElementById('text')

form.addEventListener('submit', function validateAlphanumeric(e) {
    e.preventDefault()
    const regEx = /^[0-9a-zA-Z]+$/;
    if(text.value.match(regEx))
        {
            return true;
        }
    else
        {
        alert("Please enter letters and numbers only.");
        return false;
        }
})
				
			

This was the alphanumeric validation in javascript. It was very simple to test this code. You can run it in our HTML, CSS AND JS COMPILER and get a live preview of this code.

You can watch this tutorial video on youtube on Alphanumeric validation in javascript if you want a video tutorial you can watch this

This was for today if you want coding guides in HINDI then you can visit our Hindi coding blog here –Hindi coding blog.

Thank you for reading, have a nice day 🙂

Share This Post
Share on facebook
Share on twitter
Share on email
Share on whatsapp

Leave a Reply

Subscribe To Our Newsletter

Get updates and learn from the best

Latest Guides & Articles