Site icon Mailtrap

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

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:

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

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:

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:

How to validate emails?

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:

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:

nslookup –q=mx mailtrap.io
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

Email Validation Tools

Here are some tools you can use for email validation:

RegEx

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

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:

npm install validator
const validator = require('validator');
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:

import org.apache.commons.validator.routines.EmailValidator;
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:

pip install email-validator
from email_validator import validate_email, EmailNotValidError
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:

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:

Here are the results:

Valid email address

Invalid email address

Misspelled email address

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!

Exit mobile version