Skip to main content

How to validate email addresses

There are usually two parts to determining whether an email address actually belongs to a real user:

  1. Validation: the email address is structured correctly, the domain exists, and the address is not a spam or disposable email
  2. Verification: the email address can receive emails from you and is owned by the user

This guide will focus on validation. While email address validation is important in ensuring the email belongs to a real user, it is also a vital part of a seamless user experience. By reporting errors to users in real-time, you can help them quickly correct typos and mistakes, resulting in a smoother onboarding experience and better conversion rate.

What makes an email address "valid"?

Some good things to check include:

  • Address is structured correctly: correct format, length, characters etc
  • Address adheres to domain-specific patterns
  • Domain is not from a known disposable or spam domain
  • A Mail Exchange (MX) record for the domain exists

This is where Upollo can help: we provide a free client-side API - checkEmail - that validates emails using the above checks and more, and returns useful information about them, all in real-time and with only a few lines of code. Also want to validate passwords and phone numbers? We have APIs for that too.

Prerequisites

Calling the checkEmail API

The snippet below shows how to call checkEmail and parse the response to extract useful information. See Upollo/userwatch-proto for precise details about each field in CheckEmailResponse, but in general it contains:

  • Whether the email is valid, and if not, a reason why
  • The type of email, such as consumer, company, educational, disposable etc
  • Information about the company or institution (if applicable), such as the name, industry, and number of people
import { EmailInvalidReason, EmailType } from "@upollo/web";

...

// Returns an Object containing whether the email is valid,
// a reason if it’s invalid, and whether we should treat
// the user as a priority customer.
function validateEmail(email) {
const response = await upollo.checkEmail(email);

// Check if the email is part of an organization, and if so,
// whether that org is large.
const priority = response.company &&
response.company.companySize.employeesMin > 1000;

return {
valid: response.valid,
reason: response.reason,
priority: priority};
}

The checkEmail API is optimized for speed, and so can be called when the email address input changes (we recommend adding debouncing). Because the API works in real-time, you can display any error information to the user before they click the register button:

In your HTML:

<input name="email" type="text" onChange="handleEmailChange(this.value);" />

Then in your Javascript:

function handleEmailChange(email) {
emailDetails = validateEmail(email);
if (emailDetails.valid) {
// Enable register button.
if (emailDetails.priorityCustomer) {
// Alert sales team that a priority customer has registered.
}
} else {
// Show error message, disable register button.
}
}

Try out the demo below with different email address errors to see validation in real-time:

Next steps

Congratulations, you are now validating email addresses in real-time to help users fix input issues, as well as filtering out fake and disposable addresses. To really boost your registration and login flows, you can use Upollo’s assess API to detect and convert users trying to get a repeat trial, or sharing their account with others.

Upollo
Upollo logoUpollo
Copyright 2022