›The Shack ›Developer Tips ›Regex for Email Validation

Regex for Email Validation: Patterns That Actually Work

Back to All Posts

Search for "regex for email validation" and you'll find patterns ranging from 20 characters to several thousand. The long ones try to follow the email specification exactly. The short ones accept things that obviously aren't addresses. Neither does what most people actually need.

The practical goal is narrower than "is this a valid email address?" You want to catch typos and junk at the form, without rejecting real customers. This post covers the patterns that get that balance right, where each one breaks, and what to do after the regex passes.

Why There's No Perfect Email Regex

The formal grammar for addresses (RFC 5322, with RFC 5321 covering what mail servers actually accept) allows quoted local parts, comments, IP address literals, and characters most people have never seen in an email. "john smith"@example.com is technically valid. A regex that accepts everything the spec allows is enormous, hard to maintain, and still can't tell you whether the mailbox exists.

Go too strict and you lose real people. Common addresses that rigid patterns reject:

  • Plus addressing, such as jon+newsletter@example.com
  • Long top-level domains, such as .photography or .international
  • Subdomains, such as jon@mail.example.co.uk
  • Apostrophes, such as o'brien@example.com
  • Internationalized addresses that contain non-ASCII characters

Every one of those is a real customer who can't sign up. So the aim is a pattern that's deliberately a little loose.

Pattern 1: The Practical Minimum

^[^\s@]+@[^\s@]+\.[^\s@]+$

In plain terms: one or more characters that aren't spaces or @, then an @, then more of the same, a dot, and more of the same. It catches the mistakes people actually make at a form, such as a missing @, a missing domain, a space in the middle, or a name typed into the email field. It accepts every real-world address above.

It also accepts some nonsense, like a@b.c and x@y..z. That's acceptable, because the confirmation email (covered below) catches those anyway.

Pattern 2: The Common "Strict" Pattern

^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

This is the one you'll find in most tutorials. It limits the local part to letters, digits, and . _ % + -, and requires a top-level domain of at least two letters. It handles plus addressing and long TLDs, which is better than the older versions that capped the TLD at four characters.

Where it falls short: it rejects apostrophes and international characters, and it still allows consecutive dots (jon..smith@example.com) and a domain that starts with a hyphen. Fine for an internal tool. A bit risky for a public signup form.

Pattern 3: What Browsers Use

The HTML standard defines the check that browsers run on <input type="email">:

^[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])?)*$

It's a sensible middle ground. The local part allows all the special characters the spec permits outside quotes, and each domain label must start and end with a letter or digit and be no longer than 63 characters. The spec itself calls this a "willful violation" of RFC 5322, which is a fair summary of every good email regex.

One quirk: it doesn't require a dot in the domain, so admin@localhost passes. That's correct for intranets but probably not what a public form wants. If you're already using type="email", you get this check for free, and you can add a pattern attribute to require a dot.

Test Before You Ship

Paste your chosen pattern into the Regex Tester with a test list like this, one per line, using the multiline flag:

jon@example.com
jon+tag@example.com
o'brien@example.ie
first.last@mail.example.co.uk
someone@example.photography
jon@example
jon@@example.com
jon example@example.com
@example.com
jon@.com

With Pattern 1, the first five match and the last five fail. Pattern 2 also rejects o'brien@example.ie, and the browser pattern accepts jon@example, which is exactly the kind of difference this test list is meant to reveal. If you're new to anchors, quantifiers, and character classes, Regex for Beginners walks through each piece of syntax used above.

Using It in Code

JavaScript:

const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

function isLikelyEmail(value) {
  return EMAIL_RE.test(value.trim());
}

Call .trim() first. Pasted addresses often carry a trailing space, and rejecting a correct address over invisible whitespace is a frustrating experience for the user.

On the server, PHP has a built-in check that's maintained for you:

$email = trim($_POST['email'] ?? '');
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
    // reject
}

In Python, the email-validator package is a better choice than a hand-rolled regex. It also normalizes the address and can check whether the domain is able to receive mail.

Avoid giant patterns copied from forums. Some "RFC-compliant" email regexes use nested quantifiers that can backtrack catastrophically on certain inputs, freezing the thread that runs them. That's a denial-of-service risk on a public endpoint. Short patterns don't have this problem.

Regex Is Only the First Check

A regex tells you an address is shaped correctly. It can't tell you the domain exists, accepts mail, or belongs to the person typing it. Layer your checks:

  • Format: a loose regex at the form, for fast feedback while the user is typing
  • Domain: confirm the domain has MX records before saving. You can check any domain by hand with the DNS Lookup tool, and our DNS records guide explains what MX records do.
  • Ownership: send a confirmation link. This is the only check that proves the address works and that the person signing up controls it.

The confirmation step makes most of the regex debate moot. If the email bounces or the link is never clicked, the address wasn't real, however neatly it passed your pattern. Keep the regex loose, save the strictness for the confirmation step, and you won't turn away people with unusual but perfectly valid addresses.