Guides
Last updated
November 17, 2023

How to Validate Multiple Email Addresses

Elizabeth (Lizzie) Shipton

Table of Contents:

Get your free
Email Validation
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

Validating email addresses is a critical part of modern app development and email marketing. From user authentication to email marketing, there are numerous ways in which email validation improves the experience of using websites and applications.

In this article, we’ll look at how to validate multiple email addresses using plain Javascript, using the React framework, using Regex, and using an email validation API.

Why Validate Multiple Email Addresses?

The primary reason to validate multiple email addresses at one time has to do with email marketing. In order to ensure that your marketing campaigns are reaching the people you want to target, you must make sure that the addresses on your email list are valid and deliverable. Bulk email validation is the easiest way to do this.

Invalid email addresses take up precious space on your email list (there is no point in paying to keep an address on your list if it isn’t a real address, or if the user at the address isn’t interested in your services.) Furthermore, invalid email addresses increase bounce rates and spam complaints, which can ultimately harm your sender reputation and prevent email addresses to valid addresses from being delivered.

Let’s send your first free
Email Validation
call
See why the best developers build on Abstract
Get your free api

JavaScript Validation

Since the majority of frontend web apps and websites today are built with JavaScript, it’s worthwhile understanding how to do basic email validation using JavaScript. The most common way of validating emails in JavaScript is to use Regex.

Regex Validation

Regex, or regular expressions, are sequences of characters used for pattern matching in software development. A regular expression can be created and then compared against an input string to find irregularities or mismatches in the string.

A possible regular expression that could be used for basic email validation might look like this:


/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g

Let’s break down this regular expression to see how it validates emails.

^ this symbol indicates the beginning of a line

[a-za-z0-9] this indicates a character set - aka, will match any character in the set. This character set looks for characters found in words, and the special characters “-” and “.” .

+ matches more than one occurrence of the preceding set.

@ matches the “@” symbol

[a-za-z] another character set

{2,4} matches between 2 and 4 occurrences of the preceding set

$ indicates the end of the line

Using Regex

To use this Regex in your validation, you would create a Regex object using this pattern, and then call the test function provided by the object to compare an input string:


const re = new RegExp(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g);
const tested = re.test("test@gmail.com");
console.log(tested);

// true

The result true tells us that the input string test@gmail.com is a valid pattern match for the provided regular expression.

To see how to actually use this logic in your app, take a look at the next section, where we’ll plug it into a React component.

JQuery Validation

You can use the regex pattern we just established to validate emails in JQuery quite easily. Simply create a function called validateEmail that accepts an email as an argument and matches the email to the regex pattern.

Once you have the function, you can hook it up to a button or onClick handler to validate an email from an input form before you send it to a server or do anything else with it.


$(document).ready(function () {

$('#submitButton').click(function () {

var email = $('#email').val();

if (validateEmail(email) == false) {

// show an error

return false;

}

$.post("", $("#emailForm").serialize(),

function (response) {

// show a success message

});

});

return false;

});

});

function validateEmail(email) {

var regex = new RegExp(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g); if (!regex.test(email)) {

return false;

}

else {

return true;

}

}

React Validation

React is the most popular frontend JavaScript framework being used today. To use Regex in your React application, all you need to do is pass it as the validator for an input component. You can do this using React Hook Forms, if you’re using that particular library to assist you with form-building, or you can just call your validation function in the onClick or onSubmit handler.

Related:

Let’s take a look at what that could look like.


const EmailInputComponent = () => {
const [email, setEmail] = useState("");

const handleSubmit = () => {
const re = new RegExp(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g);

const validated = re.test(email);

if (validated) {
// send the email to our server
} else {
// raise an error to the user
}
}

return (
setEmail(e.target.value)}/>

This is a very basic component that holds the value of the email string in state, updates that string with the user’s input when they enter it into the HTML text box, and then calls a handleSubmit function when the user clicks a button. The handleSubmit function compares the email held in state against a regular expression and if the email string passes the Regex match, the email is sent to the server.

Problems With Regex

The main problem with using Regex for email validation is that the regex for matching email addresses can get very complicated. There is no "fool-proof" regular expression that will match 100% of valid email addresses.

There is an official standard known as RFC 5322 that dictates what valid email addresses should look like. That regular expression looks like this:


This is a very basic component that holds the value of the email string in state, updates that string with the user’s input when they enter it into the HTML text box, and then calls a handleSubmit function when the user clicks a button. The handleSubmit function compares the email held in state against a regular expression and if the email string passes the Regex match, the email is sent to the server.

Problems With Regex
The main problem with using Regex for email validation is that the regex for matching email addresses can get very complicated. There is no "fool-proof" regular expression that will match 100% of valid email addresses.

There is an official standard known as RFC 5322 that dictates what valid email addresses should look like. That regular expression looks like this:

\A(?:[a-z0-9!#$%&'*+/=?^_‘{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_‘{|}~-]+)*

| "(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]

| \\[\x01-\x09\x0b\x0c\x0e-\x7f])*")

@ (?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

| \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}

(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:

(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]

| \\[\x01-\x09\x0b\x0c\x0e-\x7f])+)

\])\z

As you can see, it’s very complicated.

The other issue with using Regex to validate emails is that Regex only checks syntax. It doesn’t allow you to check the validity of the domain name, MX records, whether the address is a known spam trap or on a blacklist, whether it is a role-based email or free email account.

In order to check those things, you need to use a dedicated email validation API.

Using an API

Let’s take a look at how to use the AbstractAPI Free Email Validation API to validate a single email address.

Get Started With the API

Navigate to the API home page and click "Get Started"

Once you sign up for a free account you’ll land on the API dashboard, where you’ll see an API key and links to documentation, pricing, etc.

Copy the API key.

Send an Email Validation Request to the API

In your JavaScript code, create a function called sendEmailValidationRequest.

This function will send an AJAX request to the API endpoint containing the email for validation. It will then receive and analyze the response from the API and return a boolean value for us to use to determine if the email is valid.

The response from the API will look something like this:


{
"email": "eric@abstractapi.com",
"autocorrect": "",
"deliverability": "DELIVERABLE",
"quality_score": "0.80",
"is_valid_format": {
"value": true,
"text": "TRUE"
},
"is_free_email": {
"value": false,
"text": "FALSE"
},
"is_disposable_email": {
"value": false,
"text": "FALSE"
},
"is_role_email": {
"value": false,
"text": "FALSE"
},
"is_catchall_email": {
"value": true,
"text": "TRUE"
},
"is_mx_found": {
"value": true,
"text": "TRUE"
},
"is_smtp_valid": {
"value": true,
"text": "TRUE"
}
}

For now, we can just look at the is_valid_format field. Pull out the value from this field and return it as the result of our function.


const API_KEY = "YOUR API KEY";
const API_URL =
"https://emailvalidation.abstractapi.com/v1/?api_key=" + API_KEY;

const sendValidationRequest = async (emailAddress) => {
const fullURL = API_URL + "&email=" + emailAddress;
const apiResponse = await fetch(fullUrl);
const data = await apiResponse.json();
const isValid = data.is_valid_format.value;
return isValid;
}

This returns false if the email address provided is an invalid email address. If the email validation is successful, it returns true.

The function is an async function, because we are sending a network request. We can improve the user experience by handing any errors that arise from invalid emails or failed network requests.


const sendValidationRequest = async (emailAddress) => {
try {
const fullURL = API_URL + "&email=" + emailAddress;
const apiResponse = await fetch(fullUrl);
const data = await apiResponse.json();
const isValid = data.is_valid_format.value;
return isValid;
} catch(error) {
throw new Error("Unable to validate email");
}
}

The AbstractAPI endpoint checks a lot of things when you send it an email string. It runs a sophisticated regular expression check on the email, checks for syntax errors and typos in the address (for example john@agmil.com), and also does real-time SMTP and MX record checks against the email's domain.

Multiple Email Addresses

What about validating multiple emails? AbstractAPI provides a bulk upload option where you can upload a CSV file containing a list of comma separated email addresses to validate. The API runbus the same validation process against each email on the list and sends you a result file to the email address they have on record.

This is a good way of doing regular email list cleaning once every six months or so. You should be using both real-time validation and regular bulk validation to ensure that your list is free from invalid email addresses.

Common Pitfalls

The primary mistake people use when validating mail-addresses in Javascript is using only one validation method. If you solely rely on Regex to validate incoming mail-addresses, you will not capture all possibly valid emails, and you could prevent some valid addresses from getting through.

Regex is a great first step in validation - as is using the HTML attributes provided by HTML5 components. These can do a lot in raising basic errors to your user for you. However, your validation should be backed by a dedicated API that checks MX records, SMTP, and domain name validity.

IMAGE: email-validation-error

Conclusion

Validating email addresses in Javascript is a simple and effective way of preventing invalid email addresses from making it onto your email list. Make sure your email marketing campaigns are targeted and effective, and that you’re not sending emails to people who can’t receive them or don’t want them.

FAQs

How to validate multiple emails in Javascript?

The best way to validate multiple emails in Javascript is to use a dedicated email validation API such as AbstractAPI’s Free Email Validation API, or a service like Neverbounce or Zerobounce. These APIs can be easily integrated into your application via a simple network request.

How do you check whether email is valid or not in bulk?

The fastest way to bulk validate emails is using a bulk email validator that allows you to upload a CSV file full of email addresses. Most email marketing services providers give you a simple way to download a complete list of your emails - once you have this, simply upload it to your validating software of choice (AbstractAPI, Neverbounce and Zerobounce are good options.) The service will run checks on all the addresses in the list and provide you with output about which emails are valid and which are not.

Elizabeth (Lizzie) Shipton

Lizzie Shipton is an adept Full Stack Developer, skilled in JavaScript, React, Node.js, and GraphQL, with a talent for creating scalable, seamless web applications. Her expertise spans both frontend and backend development, ensuring innovative and efficient solutions.

Get your free
Email Validation
key now
See why the best developers build on Abstract
get started for free

Related Articles

Get your free
Email Validation
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required