How can I validate an email address in JavaScript?

Validating Email Addresses in JavaScript

When you are writing a web application, validating email addresses is an essential feature. This article covers how to validate email addresses using JavaScript.

Steps for Validating an Email Address

  1. Create a Regular Expression (regex) to validate an email address.
  2. Create a function that uses the regex to check if a given email address matches the regex.
  3. Test the function to ensure that it works properly with valid email addresses.

Creating a Regex

A regex can be used to validate an email address by ensuring that it follows a specific set of rules. The following regex is one that can be used to validate any valid email address:


This regex ensures that the email address has a valid format, including a majority of characters found in the Latin alphabet, the numbers 0-9, and a set of special characters.

Creating a Function

Once you have created a regex, you can use it to create a function that takes a given email address and checks to see if it matches the required regex.

The following is an example of a function that can be used to validate an email address:

// Function to validate an email address
function validateEmail(email) {
var regex = // the regex discussed above
if(email.match(regex)) {
return true;
} else {
return false;

This function takes a given email address and uses the given regex to see if it matches. If it does, the function returns true, and if it does not, the function returns false.

Testing the Function

Once you have created the function, it is important to test it to make sure that it works properly. Numerous valid and invalid email addresses can be tested to ensure that the function returns true for valid email addresses and false for invalid email addresses.


Validating an email address can be an important part of most applications and can be accomplished relatively easily with JavaScript. With the given regex and function, you will be able to accurately validate email addresses to ensure that your application functions as expected.


Mostafa Saady, Egyptian Software Engineer, supersonic self-learner and teacher, fond of learning and exploring new technologies and science. As a self-taught professional I really know the hard parts and the difficult topics when learning new or improving on already-known languages. This background and experience enables me to focus on the most relevant key concepts and topics.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Back to top button