Email Validation Essentials: What It Is and How to Implement It

On February 04, 2024
7min read
Ivan Djuric, an author at Mailtrap
Ivan Djuric Technical Content Writer @Mailtrap

A clean email list is often half the battle won when it comes to marketing campaigns. But how do you keep it clean from bad email when people are bound to enter typos or change their email accounts?

The answer is email validation. 

In this article, I’ll show you how email validation works and tell you all about it so you can keep your list as clean as a whistle.

What is email validation?

Put simply, email validation is a procedure that identifies whether an email is free from typos. It detects and prevents typos and invalid email addresses from being entered into any of your forms. 

You can imagine email validation as a mailman who checks the address format on each letter before delivering it to the recipient.

Email validation typically consists of validating an email address’s format and existence, checking the domain’s validity, ensuring the mailbox can receive messages, and detecting disposable or risky addresses. It takes care of:

  • Disposable emails
  • Invalid emails
  • Misspelled emails
  • Spam trap emails
  • Catch-all emails
  • Emails from new domains
  • Undeliverable email addresses

Note that you shouldn’t confuse email validation with email verification as the two terms are often used interchangeably. Validation can be part of the verification process, which is far more complex as it involves both the backend and the frontend, whereas validation is usually done at the frontend.

Email verification not only checks whether the email address is correct but also makes sure that someone is actually behind that address. So, a mailman who, on top of making sure the address is right, makes sure that someone is home to receive the letter

A graphic explaining how email validation and verification work.

Why must you validate emails?

Email validation is used to prevent users from making typos and to limit the registration procedure to a certain group of users (i.e. you can’t sign up for Cambridge University library with your personal email domain, instead, you need an “edu” domain account).

This email validation process, in turn, decreases the number of requests sent to the server and lowers the server load, which is especially useful if you’re running a large business.

On top of that, email validation also:

  • Eliminates hard bounces 
  • Keeps bounce rate low
  • Decreases spam complaints 
  • Reduces the chance of blacklists 
  • Protects your sender’s reputation
  • Maintains your email sender score 
  • Increases your conversion rates

Lastly, you should also check out the new deliverability updates for 2024 released by Google and Yahoo, which include new requirements for email senders.

When do you need to validate email addresses?

Besides validating email addresses at the time of user input, it’s also important to do it in the following cases:

  • Before email marketing campaigns – Validating your entire list before sending out an email campaign is advisable if you want to improve your email deliverability
  • During regular database maintenance – As a rule of thumb, you should clean your mail list quarterly or bi-annually to get rid of inactive or invalid addresses, as over time, some can become obsolete as people change addresses or close their accounts.
  • After importing or merging lists – If you acquire a new email list or merge lists from different sources, you should validate these addresses to avoid introducing low-quality contacts.
  • When reactivating old contacts – Planning to reach out to old contacts? If so, you should probably validate them first to ensure they are still active.
  • If email engagement drops – If you’re noticing lower-than-usual open rates or engagement, you might be having issues with email validity. In this case, validating your list can help you identify and remove the inactive addresses.
  • When changing the email service provider (ESP) – Validating your email list as part of the migration process when changing your ESP or moving to a different email sending platform can go a long way.

How to validate emails?

  • Syntax check 
  • DNS lookup
  • Check for disposable email addresses

Syntax check

Let’s take a regular email address: example@mailtrap.io. It consists of local (example) and domain (mailtrap.io) parts.

The local part can contain:

  • Alphanumeric characters – A to Z (both upper and lower case) and 0 to 9
  • Printable characters !#$%&’*+-/=?^_`{|}~
  • A dot . (the local part cannot start and end with a dot, and you can’t use the dot consecutively like example..first@mailtrap.io)

The domain part can contain alphanumeric characters (both upper and lower case). It can also contain a hyphen if it is not the first or last character. The hyphen cannot be used consecutively. 

These email address validation rules can be implemented in a regular expression or RegEx to validate the email address syntax. However, do not limit the validation to a RegEx rule only. 

You should also consider IETF standards, ISP-specific syntax checking, quoted words, domain literals, non-ASCII domains, and so on. If you’re building your app using Angular, React, or React Native, check our respective blog posts dedicated to email validation:

Check for disposable email addresses

A disposable email address is a temporary address that is valid for some time. You should clean your mail of any disposable emails generated by Nada, Mailinator, and similar services. You can make use of a third-party API, such as InboxHit. Those can reliably detect disposable email addresses. 

Also, you may search for a list of domains used for temporary email addresses and use them in your RegEx.

DNS lookup

A DNS lookup is the process of requesting a DNS record from a DNS server. In our case, we’re interested in the MX record. It is a DNS entry that specifies an email server for accepting emails for the domain name. Here is an example of a DNS lookup for mailtrap.io:

  • Open your console app and run the following command:
nslookup –q=mx mailtrap.io
  • You’ll see a number of MX records for the domain “mailtrap.io” and their priority values:
mailtrap.io     MX preference = 5, mail exchanger = alt2.aspmx.l.google.com

mailtrap.io     MX preference = 1, mail exchanger = aspmx.l.google.com

mailtrap.io     MX preference = 10, mail exchanger = aspmx2.googlemail.com

mailtrap.io     MX preference = 10, mail exchanger = aspmx3.googlemail.com

mailtrap.io     MX preference = 5, mail exchanger = alt1.aspmx.l.google.com
  • Pick the email server with the highest priority. The smaller the preference value, the higher the priority. In our case, this is “aspmx.l.google.com” You’ll need this input for the next step of the checklist.
A screenshot depicting the use of nslookup command for requesting a DNS record from a DNS server.

Email Validation Tools

Here are some tools you can use for email validation:

  • RegEx
  • Validator.js
  • Apache Commons Validator
  • Email-Validator on PyPI

RegEx

Earlier in the article, we’ve mentioned that you can use RegEx for syntax check — and here’s how you do it:

  • Construct a RegEx pattern that matches the structure of an email address. For example: ^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$
  • Implement the RegEx in your code. This step depends on the programming environment you use — we’ll use Python just as an example:
import re

def validate_email(email):

    regex = r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$"

    return re.match(regex, email) is not None

In our Mailtrap tutorial, we also cover how to use regex and Vue.js for email validation:

Validator.js

Validator.js, a popular JavaScript library for string validation, can also be used for validating email addresses. Here’s how to use it:

  • Install Validator.js by using npm (Node Package Manager) and run the following command in your project directory:
npm install validator
  • Import the validator module by using:
const validator = require('validator');
  • To check if a string is an email address, just copy the following code and replace ‘user@example.com’ with the address you want to validate:
const email = "user@example.com";
if (validator.isEmail(email)) {
    console.log("Valid email address.");
} else {
    console.log("Invalid email address.");
}

Apache Commons Validator

Apache Commons Validator is a Java library that has a series of utilities for validating data, including email addresses. To use it for email validation, simply follow these steps:

  • Download the Apache Commons Validator from the official website
  • Import the ‘EmailValidator’ class in your Java code where you wish to perform the email validation like this:
import org.apache.commons.validator.routines.EmailValidator;
  • Validate an email address by creating an instance of ‘EmailValidator’ and using it to check if a string is valid or not. You can do this in a method or directly in your application logic. It should look like this:
public class EmailValidationExample {

public static void main(String[] args) {
        // Get instance of EmailValidator
        EmailValidator validator = EmailValidator.getInstance();

        // Example email addresses for validation
        String validEmail = "example@example.com";
        String invalidEmail = "example@.com";

        // Validate emails
        System.out.println("Is '" + validEmail + "' valid? " + validator.isValid(validEmail));
        System.out.println("Is '" + invalidEmail + "' valid? " + validator.isValid(invalidEmail));
    }
}

Email-validator on PyPI

Using the ‘email-validator’ library in Python is quite a straightforward process:

  • Install email-validator. If you’re using pip, you can do this easily by running the following command in your terminal or command prompt:
pip install email-validator
  • Import the ‘validate_email’ function from the ‘email_validator’ module with:
from email_validator import validate_email, EmailNotValidError
  • Use the ‘validate_email’ function to validate an email address. If the email is invalid, it should give you:
EmailNotValidError

Bulk email validation

I know that it might seem like validation can be quite time-consuming, but, luckily, there are email validation services that are also called “email verification tools”. You can use these “verifiers” to either validate single addresses in a few clicks or automate the whole process with API integration for real-time email validation.

The market is full of these, so choosing one can be hard.

Again, luck is on our side, because Jenny, our email infrastructure specialist, has tested numerous validation, or verification tools, analyzed web feedback, and come up with a list based on the following criteria:

  • Domain / MX record check
  • Single email validation
  • Bulk email validation
  • Syntax check
  • Mail server validation
  • Validation API
  • Disposable email address detection
  • GDPR compliance

Here is the list of winners:

  1. NeverBounce
  2. Emailable
  3. Hunter
  4. Bouncer
  5. ZeroBounce
  6. Kickbox

Email validation with tools

If you’re wondering how one of these so-called email verification services works, we’ve randomly picked Hunter, and performed a validation check on the following:

  • support@mailtrap.io – a valid email address
  • firebird@mailtrap.io – an invalid email address
  • example@gmail.ccom. – an email address with a typo

Here are the results:

Valid email address

An example of trying to validate a valid email at Hunter.io

Invalid email address

An example of trying to validate an email that doesn't exist at Hunter.io

Misspelled email address

An example of trying to validate an email with a typo on Hunter.io

Source: https://hunter.io/verify

Wrapping up

And that concludes our guide on email validation!

Remember that while you have numerous validating options, it’s best to combine several methods. 

For example, you can integrate real-time validation into your automation workflow and combine it with whole list validation to get a much better result than when using one of these methods on their own.

If you want to further improve your email deliverability, then use an email testing solution on top of validation services to ensure your messages don’t end up in spam folders, and, instead, reach the intended recipients’s inboxes.

I personally recommend Mailtrap Email Testing, as it allows me to test my emails in a safe SMTP server environment where I can spot and fix any HTML/CSS code errors, and more.

Check out how it works!

Ivan Djuric, an author at Mailtrap
Article by Ivan Djuric Technical Content Writer @Mailtrap

I’m a Technical Content Writer with 5 years of background covering email-related topics in tight collaboration with software engineers and email marketers. I just love to research and share actionable insights with you about email sending, testing, deliverability improvements, and more. Happy to be your guide in the world of emails!