Guides
Last updated
July 20, 2025

5 Ways to Implement Email Validation in JavaScript

Nicolas Rios
Nicolas Rios
Table of Contents:
ON THIS PAGE
Get your free
email validation
 API key now
stars rating
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

Ensuring email addresses are valid is fundamental for clean data and reliable communication. We'll walk through three JavaScript validation techniques, providing functional code for each. We will then analyze the shortcomings of these methods and demonstrate how Abstract API offers a more dependable solution to these common problems.

How to Implement Email Validation in JavaScript

Here are three common techniques to validate email addresses with JavaScript. Each approach has a different implementation, from native browser features to specialized libraries, with functional code for each.

HTML Constraint Validation API

This method leverages the browser’s built-in parser that supports the HTML input element with a "type" attribute set to "email". JavaScript's role is simply to trigger the validity check, which means you do not need to maintain a validation pattern.

The browser’s implementation applies the WHATWG email grammar. It automatically flags issues like "patternMismatch" or "typeMismatch". Developers retain full programmatic control through functions like "reportValidity()" or by the creation of custom messages.

<input id="mail" type="email" required>
<script>
function isValid() {
 const el = document.getElementById('mail');
 return el.checkValidity(); // boolean
}
</script>

A Single Regular Expression

A regular expression offers a pragmatic but aggressive way to validate emails. This pattern approximates the RFC 5322 standard and can cover quoted local parts, domain literals, and most ASCII special characters.

This single expression covers more than 99% of email addresses found in production environments. However, the pattern is intentionally complex and unreadable, so any modification creates a risk of regressions, a topic often discussed by developers.

const rfc5322 = /^(?:[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]\d|[01]?\d\d?)(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}\])$/i;

function isValid(email) {
 return rfc5322.test(email);
}

A Library Like Validator.js

You can use an NPM package like "validator" for email validation. This package provides a maintained "isEmail" function that includes options for IP literals, display names, and UTF-8 local parts.

It also allows for the creation of blacklist or whitelist top-level domains. The advantages include shared maintenance, unit tests, and incremental releases that track specification and TLD growth. It works in both browser and Node runtimes.

import validator from 'validator';
validator.isEmail(addr, {
 allow_ip_domain: true,
 allow_utf8_local_part: true,
 host_blacklist: ['example.com']
});

Challenges of Implementing Email Validation in JavaScript

JavaScript-based email validation faces significant hurdles. These range from complex standards and international character sets to the inability to confirm if a mailbox can actually receive mail, which creates unreliable results.

  • The RFC 5322 standard is too complex for a single regular expression to cover completely. This leads to unreadable patterns that either reject valid emails or accept invalid ones, a common issue with the regex method.
  • Client-side scripts cannot perform DNS or SMTP checks to confirm mailbox existence. All three methods—HTML validation, regex, and libraries like Validator.js—only check syntax, which allows undeliverable addresses to pass as valid.
  • Most validation methods, particularly those that rely on a single regular expression, fail to support internationalized addresses. They often reject valid emails that contain Unicode characters or punycode domains, which limits global compatibility.
  • Email providers define their own rules for local-parts, such as case sensitivity or support for special characters. A fixed validation rule, used by the HTML API or regex, may incorrectly block real users on certain servers.

Validate Emails with Abstract API
Implement email validation in your JavaScript project to ensure you're collecting accurate user data.
Get started for free

How Abstract API Handles Email Validation in JavaScript

Traditional methods only confirm if a string looks like an email. Abstract API addresses these weaknesses with a network layer that performs comprehensive, real-world checks.

  • It performs a suite of checks that cover syntax, typo autocorrection, and lookups for disposable or free providers. The API also detects role-based and catch-all accounts, discovers MX records, and completes an SMTP handshake.
  • This approach removes complex DNS, SMTP, and list-maintenance code from your application. It also cuts sign-up latency for valid addresses and reduces bounce rates and sender reputation risk.
  • The API is a stateless HTTP endpoint. You can call it from a browser, Node, or edge functions without the need for extra dependencies or state management.

How to Bring Abstract API to Your Dev Environment

Once you know Abstract’s capabilities, you can add its email validation API to your project with ease.

  • Create a free Abstract account and copy the Email Validation API key from the dashboard.
  • Install axios with the command "npm i axios", or use the native fetch API. No SDK is required.
  • Store the key in an environment variable, for example, "ABSTRACT_API_KEY", so it never reaches the client bundle.
  • Write a thin helper that builds the query string and contacts https://emailvalidation.abstractapi.com/v1/ with your API key and the email address.
  • Parse the JSON response and gate user flows on specific data points, such as "data.deliverability === 'DELIVERABLE'" and "data.is_disposable_email.value === false".
  • Add retry logic, as Abstract is idempotent, and set a three-second timeout to complete the setup.
const axios = require('axios');
const { ABSTRACT_API_KEY } = process.env;

async function validateEmail(email) {
  const url = `https://emailvalidation.abstractapi.com/v1/?api_key=${ABSTRACT_API_KEY}&email=${encodeURIComponent(email)}`;
  const { data } = await axios.get(url, { timeout: 3000 });
  return data;
}

Sample Email Validation Implementation with Abstract API

The code defines an asynchronous function named "validateEmail". This function accepts an email address as an argument. It constructs a request URL that includes your private API key and the email address you want to check. The function then uses the "axios" library to send an HTTP GET request to the Abstract API endpoint. It awaits the response, extracts the JSON data, and returns it. The code also sets a timeout of 3000 milliseconds to prevent long waits.

A successful request returns a JSON object with detailed information about the email address. Here is a sample response for a valid address:

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

The key fields in the response are "deliverability" and "quality_score". A "DELIVERABLE" status indicates the email address is active and can receive mail. The API also provides boolean checks for disposable services, role-based accounts like "admin@", and catch-all configurations.

Final Thoughts

Traditional validation methods only check format, so they cannot tell if a mailbox exists or belongs to a disposable provider. This weakness leads to fake sign-ups and high bounce rates. Abstract API solves these problems with its comprehensive, real-world checks. For reliable validation, create an account on Abstract API and get your free API key.

Validate Emails with Abstract API
Improve your data quality by adding simple and effective email validation to your JavaScript project.
Get started for free

Related Articles

Phone Validation
key now
Get your free
stars rating
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