ToolSphere
Back to all articles
regex

Email Validation Regex That Actually Works

2026-07-217 min read

Try it: free Regex Tester & Matcher

Test regular expressions and preview text replacements

Open →

There is no perfect email regex, but there are good ones. Below are three patterns, from loose to strict, each tested against the same 21 addresses so you can see exactly what it lets through. Pick the one that matches how much you care about typos, then confirm the address the only reliable way: by sending an email to it.

The three patterns

1. Simple: something@something.something

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

No spaces, exactly one @, and a dot somewhere after it. It never rejects a real address, and it lets some typos through.

2. Practical: the one to use in most forms

^[A-Za-z0-9._%+'-]+@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}$

The local part (before @) allows letters, digits and . _ % + ' -. The domain is one or more labels that start and end with a letter or digit, each followed by a dot, and then a top-level domain of at least two letters. It accepts subdomains and country domains like .co.uk, and rejects empty labels, double dots in the domain and labels starting with a hyphen.

3. Strict: also checks the dots in the local part

^(?!\.)(?!.*\.\.)[A-Za-z0-9._%+'-]+(?<!\.)@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}$

The same, plus three rules: the local part may not start with a dot (?!\.), contain two dots in a row (?!.*\.\.), or end with a dot (?<!\.). The lookbehind needs JavaScript ES2018 (Safari 16.4 or later); Python, Java, PHP and .NET all support it.

Test results

Each pattern was run against the same addresses in JavaScript and Python (Y = accepted):

Address Simple Practical Strict HTML5
user@example.com Y Y Y Y
first.last@example.co.uk Y Y Y Y
user+tag@gmail.com Y Y Y Y
name@mail.example.co.uk Y Y Y Y
o'brien@example.ie Y Y Y Y
user@sub-domain.example.org Y Y Y Y
x@y.io Y Y Y Y
user@localhost - - - Y
user@example - - - Y
user@example.c Y - - Y
user@example..com Y - - -
user@.example.com Y - - -
user@-example.com Y - - -
.user@example.com Y Y - Y
user.@example.com Y Y - Y
us..er@example.com Y Y - Y
plainaddress, @example.com, user@ - - - -
user name@example.com, user@@example.com - - - -

Patterns copied from old tutorials often look like ^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$. That one rejects first.last@example.co.uk, name@mail.example.co.uk and o'brien@example.ie, all real addresses, because it allows only one dot after the @. Rejecting real customers is worse than letting a typo through, so test any pattern against addresses with subdomains before you ship it.

What the browser already does: type="email"

<input type="email"> validates with the pattern from the HTML standard (the HTML5 column above):

^[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 deliberately accepts user@localhost and user@example (no dot), which are valid on internal networks. If your users are on the public internet, add your own check for a dot in the domain, and always validate again on the server: browser checks are easy to bypass.

Code

JavaScript

const EMAIL = /^[A-Za-z0-9._%+'-]+@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}$/;
EMAIL.test(input.trim()); // true or false

Python (use fullmatch, not match, or trailing junk passes)

import re
EMAIL = re.compile(r"[A-Za-z0-9._%+'-]+@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}")
bool(EMAIL.fullmatch(address.strip()))

PHP has a built-in validator, so you may not need a regex at all:

$isValid = filter_var($email, FILTER_VALIDATE_EMAIL) !== false;

Java

private static final Pattern EMAIL = Pattern.compile(
    "^[A-Za-z0-9._%+'-]+@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\.)+[A-Za-z]{2,}$");
boolean isValid = EMAIL.matcher(email.trim()).matches();

Paste any of the patterns into the regex tester with your own list of addresses to see what matches.

Why not the "fully correct" RFC 5322 regex?

RFC 5322, the specification for email syntax, allows far more than any real provider issues: quoted local parts ("john doe"@example.com), comments and IP-address domains. A regex that implements all of it runs to thousands of characters, is unreadable, and accepts addresses your mail system probably cannot deliver to. It also still cannot tell you whether the inbox exists.

Format is not existence

No regex can tell you that jane@example.com exists or that the person typing it owns it. Only an email round trip can:

Check What it proves
Regex The text is shaped like an email address
Verification email sent and delivered The address exists and accepts mail
Verification link clicked The person who signed up controls that inbox

So use the regex to catch typos while the user is still on the form, and the verification link to confirm the address.

Common mistakes

  • Rejecting + addresses. user+tag@gmail.com is valid and common (people use it to filter mail). Patterns that only allow [A-Za-z0-9._-] lock these users out.
  • Allowing only one dot in the domain, which blocks .co.uk, .com.au and company subdomains.
  • Limiting the top-level domain to 2–4 letters. .museum, .photography and many newer TLDs are longer. Use {2,}.
  • Forgetting anchors. Without ^ and $ (or fullmatch in Python), junk user@example.com junk passes.
  • Not trimming. A trailing space from autofill or copy-paste fails an otherwise fine address; trim before testing.
  • Lowercasing the whole address. The domain is case-insensitive, but the local part technically is not. Compare case-insensitively, but store what the user typed.

FAQ

What is the best regex for email validation?
For most sign-up forms: ^[A-Za-z0-9._%+'-]+@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}$. It accepts real addresses with plus signs, apostrophes and subdomains, and rejects the common typos. Follow it with a verification email.

Is it worth writing a fully RFC 5322-compliant regex?
Rarely. It is enormous, accepts addresses no real provider issues, and still cannot confirm that an address exists, which is what actually matters.

Can a regex confirm an email address exists?
No. Only sending a message, usually with a verification link, and seeing it acted on can establish that.

Should an email regex reject addresses with a + in them?
No. user+tag@example.com is valid, and many providers support it for inbox filtering. Rejecting it is a common, avoidable mistake.

Use the Text Diff & Compare to compare your sample text before and after an edit. For a related walkthrough, read Regular Expressions: Patterns and Matches.

Looking for other tools?

Explore ToolSphere Homepage →