Skip to content

Blog

Email validation regex: what a pattern can check, and what it cannot

What RFC 5321, RFC 5322 and RFC 6531 allow in an email address, a practical regex for forms, the cases it misses, and what to run after it.

Updated 2026-11-24

A regular expression checks that a string looks like an email address. It cannot check that the address works. Use a short pattern in the form so a user gets immediate feedback on obvious mistakes, and then run the checks that need DNS and the mail server. Everything in this article follows from that split: syntax is cheap and local, deliverability is neither.

What the standards say an address is

Three documents matter.

  • RFC 5321 defines SMTP, the protocol that moves mail. It sets limits: the local part, before the @, can have at most 64 octets, and the domain at most 255. The forward path, which wraps the address in angle brackets, is limited to 256 octets, so a usable address is about 254 characters.
  • RFC 5322 defines the format of a message and the grammar of an address, called addr-spec. The local part is either a dot-atom (letters, digits and a set of symbols, with dots between atoms) or a quoted string. The domain is a dot-atom or a bracketed address literal.
  • RFC 6531 extends SMTP so that addresses can contain UTF-8 in the local part and an internationalized domain. A server that supports it advertises the SMTPUTF8 extension.

Domain labels follow DNS rules from RFC 1035: each label is at most 63 octets. Those limits, and not the grammar, are what a regex can check cheaply.

Why the full grammar is a poor regex

The RFC 5322 grammar also allows comments in parentheses, folding whitespace and a set of obsolete forms kept for compatibility. A pattern that matched all of it would run to thousands of characters and still say nothing about delivery. The goal for a form is the opposite: accept what real people have, reject what they mistype.

A practical pattern

The HTML Living Standard defines what a browser treats as a valid email address for input type=email, and it says outright that its definition is a willful violation of RFC 5322, chosen to be simpler and more practical. It is a good default for forms:

javascript
const emailPattern =
  /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

export function looksLikeEmail(value) {
  const email = value.trim()
  return email.length <= 254 && emailPattern.test(email)
}

It limits each domain label to 63 characters, as DNS does, and it allows the symbols RFC 5322 permits in an unquoted local part. We ran it against a set of inputs to see where it agrees with the standard and where it does not.

Input Pattern says Comment
jane@example.com Accepts Fine
jane+shop@example.com Accepts Subaddressing is legitimate
jane@gmial.com Accepts A typo: syntax is fine, the domain is wrong
jane@localhost Accepts A domain with no dot; fine for the standard, rarely what a signup wants
a@b Accepts Same: one-label domain
jane..doe@example.com Accepts Not allowed by the dot-atom rule in RFC 5322
.jane@example.com Accepts Same: a leading dot
jane@example..com Rejects An empty label
jane@-example.com Rejects A label cannot start with a hyphen
"jane doe"@example.com Rejects Legal per RFC 5322, but a quoted local part is rare
jane@[192.0.2.1] Rejects An address literal, legal and almost never wanted
用户@例子.广告 Rejects A UTF-8 address under RFC 6531; see below
jane@example.com. Rejects A trailing dot

The pattern is lenient on the dots in the local part and strict on the domain. That is the pragmatic trade, and it shows the point of the next section: syntax cannot settle what is deliverable.

What a regex cannot know

  • Whether the domain exists. jane@this-domain-does-not-exist.example is well formed. A DNS lookup for MX records is what shows that a domain can receive mail.
  • Whether the mailbox exists. Only the recipient's mail server knows. A verifier asks it, with the RCPT TO command, and never sends a message.
  • Whether the user mistyped. gmial.com is syntactically perfect. A suggestion engine that compares the domain with common ones is what catches it.
  • Whether the address is disposable or a role. info@ and a throwaway inbox match every pattern.
  • Whether it is the user's own. Only a confirmation email proves that.

Each of those is a different check. How to verify an email address walks through them in order.

Internationalized addresses

RFC 6531 lets an address carry non-ASCII characters in the local part, and the domain can be written in its own script. Support is growing but uneven, and the pattern above rejects those addresses. Decide on purpose: if your market has such users, loosen the local part to accept any non-whitespace character other than @, and let the later checks decide. If it does not, a clear message is better than a silent failure. Whichever way you go, test it. Do not discover your behaviour from a support ticket.

Case, whitespace and the plus sign

  • Trim whitespace. Paste often brings a trailing space. Trim before you test and before you store.
  • Keep the case you were given. Domains are case-insensitive. The local part is, by RFC 5321, case-sensitive in theory and treated as case-insensitive by almost every provider. Lowercase for comparison, and store the original too.
  • Never strip the plus tag. Providers use it for subaddressing. Removing it changes the address.
  • Do not collapse dots. Some providers ignore dots in a local part. Many do not. You cannot know which.

Then run the checks that matter

Syntax is the first of several checks, and the Qelka email API runs them all. checks.syntax is the pattern-level result, and the rest of the response covers what a regex cannot:

bash
curl https://api.qelka.com/v1/email \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"email": "jane@gmial.com", "mode": "fast"}'

The response carries status, a reason, a did_you_mean suggestion such as jane@gmail.com for jane@gmial.com, and a checks object with syntax, mx, smtp, catch_all, disposable, role and free_provider. In fast mode no mailbox check runs, so the best status is unknown with the reason smtp_not_checked. In full mode the mail server is asked about the mailbox without sending anything. Both cost 1 credit, and errors are not billed. The email API reference lists every status and reason.

A sensible form flow looks like this:

  1. While typing: run the local pattern. Show a quiet hint on a clear mistake, and never block typing.
  2. On submit, on your server: call the API. Reject undeliverable with the reason, offer did_you_mean when there is one, and decide your policy for risky.
  3. After signup: send a confirmation email when the address will be used to sign in.
javascript
async function checkEmail(email) {
  if (!looksLikeEmail(email)) return { ok: false, hint: 'format' }

  const response = await fetch('https://api.qelka.com/v1/email', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ email, mode: 'fast' }),
    signal: AbortSignal.timeout(5000),
  }).catch(() => null)

  if (!response || !response.ok) return { ok: true, unchecked: true }
  const result = await response.json()
  if (result.status === 'undeliverable') return { ok: false, hint: result.reason, suggestion: result.did_you_mean }
  return { ok: true, status: result.status, suggestion: result.did_you_mean }
}

The function fails open: if the API is slow or unreachable, the form goes through and the address can be checked later. A validation outage should not be the reason nobody can sign up. Call the API from your server, never from the browser, or your key ships to every visitor. For the signup side see validate signup forms.

Mistakes to avoid

  • A heroic regex copied from a forum. It rejects real addresses and still accepts every typo.
  • Rejecting the plus sign. It turns away people who use tagged addresses.
  • A length limit of 50. Real addresses are longer. Use the RFC limits.
  • Blocking submit on a pattern failure with no explanation. Say what is wrong.
  • Treating a regex pass as a reason to send a campaign. Deliverability is a different question: see email bounce.
  • Validating only in the browser. Anyone can post to your endpoint directly.

Try it

The free email syntax checker runs the format check on any address, and the email verifier runs the full set after you create a free account. The email verification API page covers pricing and volumes.

Frequently asked questions

What is the best regex to validate an email address?
There is no perfect one, because the grammar in RFC 5322 is too permissive and too intricate for a readable pattern. For a form, use something short that catches obvious mistakes, such as the pattern the HTML standard uses for input type=email, or even a check for one @ with text on both sides. Then verify the domain and the mailbox, which no pattern can do.
Can a regex tell me if an email address exists?
No. A regex reads characters. It accepts nobody@example.com as readily as a real mailbox, and it accepts typos like jane@gmial.com. Whether a domain receives mail is a DNS question, and whether a mailbox exists is a question for the mail server. A regex only filters out strings that cannot be an address.
How long can an email address be?
RFC 5321 limits the local part, before the @, to 64 octets and the domain to 255 octets, and caps the whole forward path at 256 octets including its angle brackets, which in practice means about 254 characters for the address. The Qelka API accepts up to 320 characters as input, so an over-long string gets a clear verdict instead of an error.
Should I allow plus signs and unusual characters in the local part?
Yes. A plus sign is common for subaddressing, as in jane+shop@example.com, and RFC 5322 allows many other symbols. Rejecting them turns away real customers and breaks users who rely on tagged addresses. Do not strip the tag either: the provider decides how to deliver, and the address you store should be the one the person typed.
Do email regexes support international characters?
Most do not. Pre-2012 patterns assume ASCII. RFC 6531 extends SMTP so that local parts can hold UTF-8 and domains can be internationalized, and both are in use. A pattern that only accepts a-z rejects those users. If you serve markets where non-ASCII addresses are normal, test your validator with them before launch.
What should I do after the regex passes?
Verify the address with checks a pattern cannot do. Look for a typo in a known domain, confirm that the domain has mail servers, ask the mail server whether the mailbox exists, and flag disposable and role addresses. Qelka's email API runs all of them in one call and returns a status and a reason. Then send a confirmation email when the address will be used for sign-in.

Put this check in your product

Get an API key in under a minute. 100 free credits every month, no card required.

  • 100 free credits a month
  • No card
  • Test keys spend nothing
  • Cancel any time