Validate an email address in JavaScript, properly
Most "email validation" is a regex. That catches foo@ but not [email protected], [email protected], or [email protected].
Layer 1: shape (client side)
const ok = /^[^@\s]+@[^@\s]+\.[^@\s]{2,}$/.test(email);
Keep it loose. Strict regexes reject valid addresses such as [email protected].
Layer 2: the domain can receive mail
Look up MX records for the domain. No MX and no A record means mail cannot be delivered. Browsers cannot do DNS directly, so do it on your server, or call an API:
const v = await (await fetch("https://pw-d20261007a2.plainwork-apps.workers.dev/check?email=" + encodeURIComponent(email))).json();
console.log(v.verdict, v.score, v.did_you_mean);
Layer 3: confirmation email
The only proof an inbox exists and belongs to the user is a confirmation link. Layers 1 and 2 reduce bounces before you send it.
Try the endpoint free (20 per day, no signup) on the main page.