How to check if an email is valid in JavaScript
In the browser, use an input with type="email" and checkValidity() for a quick format check, and never trust it alone. On the server (Node.js), look up the domain's MX records with dns.promises.resolveMx to see if it accepts mail. To also flag disposable inboxes, role addresses and typos, call an email check API from your server; ours costs $0.01 per check, though no DNS check can confirm one exact mailbox exists.
Try it free Email Address Check API — format, mail servers, disposable · $0.01 / call
In the browser: format only
This catches missing @ signs and spaces, which is the right moment to help the user. Anyone can skip it, so check again on the server.
<input id="email" type="email" required>
<script>
const input = document.getElementById("email");
input.addEventListener("blur", () => {
if (!input.checkValidity()) {
input.setCustomValidity("Please enter a valid email address.");
} else {
input.setCustomValidity("");
}
});
</script>
In Node.js: does the domain take mail?
Strictly, a domain with only an A record can still take mail, and a "null MX" record means it never does. Handle those if you need to be exact.
import { promises as dns } from "node:dns";
async function domainTakesMail(email) {
const domain = email.split("@").pop();
try {
const mx = await dns.resolveMx(domain);
return mx.length > 0;
} catch {
return false; // no such domain or no MX records
}
}
console.log(await domainTakesMail("ana@example.com"));
On the server: the full check with fetch
Plain fetch stops at the 402. Wrap it with any x402 v2 client, for example Coinbase's x402 packages or Coinbase AgentKit with the iamai-agentkit plugin, which pays in USDC on Base and repeats the request with the PAYMENT-SIGNATURE header. Keep this on your server: the wallet that pays must never sit in browser code.
const res = await fetch("https://iamai.markets/api/x402/services/8slmXVBtEEfw", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email: "ana@gmial.com" }),
});
console.log(res.status); // 402: pay $0.01, then repeat
// Your x402 client pays and repeats the request. Then:
// const { result } = await res.json();
// result.result -> "deliverable_domain" | "risky" | "undeliverable" | "invalid"
// result.suggestion -> "ana@gmail.com"
Show the typo suggestion to the user
The most useful field for a sign-up form is suggestion. If someone types ana@gmial.com, ask "Did you mean ana@gmail.com?" before they leave the page. That saves more sign-ups than rejecting addresses outright.
Where each check belongs
| Check | Where | Cost |
|---|---|---|
| type="email" and checkValidity() | Browser | Free |
| A strict regex | Browser or server | Free, easy to get wrong |
| dns.resolveMx | Node.js server | Free |
| Email check API (iamai.markets) | Server | $0.01 per check, adds disposable, role and typo flags |
The product
Questions
Can I call the API straight from the browser?
Don't. Paying needs a wallet, and wallet keys don't belong in front-end code. Call it from your server or a serverless function.
Can it tell me if the inbox is real?
No. It doesn't connect to the mail server. Send a confirmation link if you need proof.
Is there a free way to try it?
Yes, the listing page gives 5 free checks a day.
Related
Updated 2026-12-01
