Validating emails directly in HTML is a fundamental step for good data hygiene and user experience. Below are four ways to do this, each with working code. You will also learn about the common pitfalls of these methods and how Abstract API offers a more reliable alternative.
How to Implement Email Validation in HTML
You can validate emails directly in HTML with a few different methods. These techniques use built-in browser features to check email formats and provide immediate feedback to the user.
Native Input Type Email
The simplest method uses the email input type. The browser parses the input against the WHATWG e-mail grammar and exposes errors through the constraint-validation API. This process requires no JavaScript.
The check runs on blur, on submit, or when you call the form's reportValidity() method. Mobile browsers also display an “@”-optimized keyboard, which improves the user experience. The multiple attribute lets you accept a comma-separated list of emails.
<form>
<input id="workMail" type="email" required autocomplete="email">
<button>Send</button>
</form>
Pattern Attribute on the Same Element
The pattern attribute adds another layer of validation with a regular expression. This check happens after the built-in syntax check. A failure toggles the patternMismatch validity state, but typeMismatch remains false if the value is syntactically correct.
You can use it for domain whitelists, disposable address filters, or length caps. The regular expression must match the entire candidate string. Unicode-set syntax is supported, so you must escape special characters.
<input id="corpMail"
type="email"
pattern=".+@beststartupever\.com"
title="Use your company address"
required>
JavaScript and the Constraint Validation API
For more control, you can use JavaScript with the Constraint Validation API. The validity property offers specific flags like valueMissing, typeMismatch, and patternMismatch. You can use setCustomValidity to replace the default browser message and flip the customError flag.
The reportValidity method forces live feedback without a block on the form submission logic. This approach lets you combine asynchronous checks, like a debounce for an MX lookup, and clear the message when the operation completes.
const f = document.querySelector('form');
const mail = f.elements.email;
mail.addEventListener('input', () => {
if (mail.validity.typeMismatch) {
mail.setCustomValidity('Not a valid address');
} else if (mail.value.endsWith('.con')) {
mail.setCustomValidity('Did you mean “.com”?');
} else {
mail.setCustomValidity('');
}
mail.reportValidity();
});
Challenges of HTML Email Validation
While client-side checks offer immediate feedback, they come with significant drawbacks. These limitations affect reliability, cross-browser consistency, and the actual deliverability of the email addresses you collect.
- The official email syntax is vast and complex. The native input type="email" and pattern attributes cannot cover every valid format, which means they either reject legitimate addresses or accept typos and other invalid inputs.
- Browsers use different validation engines. An address that passes on one browser might fail on another. Updates to a browser can also break a previously functional pattern attribute, which creates an inconsistent user experience.
- HTML validation methods often fail with internationalized email addresses. Browsers handle non-ASCII characters inconsistently, so the same address may be rewritten or rejected, which complicates server-side normalization and user logins.
- Client-side checks only confirm syntax, not deliverability. Methods like the Constraint Validation API cannot detect disposable domains, check MX records, or prevent bounces. A syntactically correct address can still be undeliverable.
Validate Emails with Abstract API
Validate emails at sign-up to improve data accuracy and ensure your messages reach the inbox.
Get started for free
How Abstract API Handles Email Validation
Abstract API addresses the core weaknesses of traditional methods through a comprehensive, multi-layered check.
- It moves past simple format validation to catch subtle typos and syntax errors that a basic regex check would miss.
- The API identifies disposable, temporary, and role-based email addresses that often fail to reach an actual person.
- It executes technical checks, which include MX record lookups and SMTP handshakes, to confirm a domain accepts mail and the mailbox exists.
- All complex logic and data upkeep reside on Abstract’s servers, so developers do not need to maintain their own systems or blocklists.
How to Bring Abstract API to Your Dev Environment
Once you are familiar with Abstract’s capabilities, it is simple to add its email validation API to your project.
- Create an Abstract account and copy your Email Validation API key from the dashboard.
- Add the key to your secret store, such as dotenv or AWS Secrets Manager.
- Install an HTTP client like axios or fetch.
- Call the API endpoint from your form's submit handler.
- Parse response data like `deliverability` and `quality_score` to decide whether to accept, warn, or reject the address.
- Deploy your code. Abstract handles all backend scaling and updates.
async function validateEmail(email) {
const url = `https://emailvalidation.abstractapi.com/v1/?api_key=${process.env.ABSTRACT_KEY}&email=${encodeURIComponent(email)}`;
const res = await fetch(url);
if (!res.ok) throw new Error('API error');
return res.json();
}
// form submit
const data = await validateEmail(values.email);
if (data.deliverability !== 'DELIVERABLE' || data.is_disposable_email.value) {
setError('Enter a reachable, non-disposable address.');
} else {
continueSubmit();
}
Sample Email Validation Implementation with Abstract API
The JavaScript code above defines an asynchronous function, `validateEmail`, that sends a user's email address to the Abstract API endpoint. In a typical form submission, the code awaits the API response. It then inspects key data points like `deliverability` and `is_disposable_email` to decide if the submission should proceed or if it should show an error to the user.
The API returns a detailed JSON object that provides much more context than a simple format check. The `deliverability` field tells you if the email can receive mail, while `quality_score` gives a numeric confidence level. Other boolean flags like `is_disposable_email` and `is_role_email` help you filter low-value addresses. These data points allow you to enforce strong validation rules without complex backend maintenance.
{
"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" }
}
Final Thoughts
Traditional validation only checks format, so it accepts disposable addresses, typos, and domains that cannot receive mail. These issues lead to bounced emails and poor data quality. Abstract API performs a deep, multi-layer check to confirm an email is both syntactically correct and truly deliverable. For reliable email validation, consider an account on Abstract API to get your free API key.
Validate Emails with Abstract API
Validate user emails at the point of entry to maintain a clean list and improve deliverability.
Get started for free