EasyDeveloper

Regex

Email Regex: How to Validate an Email Address

Published 2026-08-15 · 7 min read

TL;DR: Validate an email with ^[^@\s]+@[^@\s]+\.[^@\s]+$ for most cases, and prefer an HTML5 type=email input or a library over a giant RFC 5322 pattern.

Validating an email address is the case where a regex should be simple. Most tutorials post a pattern that is either a tiny one-liner that misses real typos or a hundred-character RFC 5322 monster that is too strict to use. The working answer sits in the middle: a short pattern that catches the common mistakes, plus a confirmation step that proves the address is real. The address grammar a real validator should follow is defined in RFC 5322.

What is a simple email regex that works?

This pattern is the practical baseline. It requires a local part, an @, a domain and a dot, and it rejects spaces:

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

Reading it: one or more characters that are not @ and not whitespace, then an @, then one or more non-space characters, then a literal dot, then one or more non-space characters, all anchored to the full string. It rejects user@ (no domain), user@@example.com (two @) and user@example (no dot), which are the typos that actually happen. The regex tester lets you paste sample addresses and watch the pattern behave.

Why is the RFC 5322 email regex a trap?

The specification that defines the email grammar allows quoted local parts like "a b"@example.com, comments, escaped characters and internationalized addresses. A faithful pattern is long, hard to read and hard to maintain, and it still cannot tell you whether the mailbox exists. In practice a regex-valid address can bounce and a simple address that the RFC would frown at can deliver fine. The complexity buys almost nothing for a sign-up form.

What should you actually do?

  • Use the simple pattern or the HTML5 email input to block obvious typos.
  • Normalize the address, usually by lowercasing the domain.
  • Send a confirmation email and require a click to activate the account.
  • Revalidate on the server, never trusting the client alone.

The confirmation email is the step the regex cannot replace. Syntax validation stops bad input; a round-trip email proves ownership, which is what you actually want for accounts, newsletters and password resets.

How do you validate email in HTML5?

The type=email input gives you browser validation with zero JavaScript. The browser enforces its own reasonable email rules and shows a localized error before the form submits:

<input type="email" name="email" required placeholder="you@example.com">

The browser rule is deliberately loose, so pair the input with a server-side check. On the server the same logic applies: run the simple pattern, then send the confirmation.

How do you validate email in JavaScript?

const re = /^[^@\s]+@[^@\s]+\.[^@\s]+$/;
const ok = re.test(email.trim());
console.log(ok ? 'valid' : 'invalid');

Note the trim: a leading or trailing space makes the pattern fail, and users paste spaces all the time. The same regex is portable to Python, Java and Go with only the delimiter removed.

How do you validate email in Python?

import re

pattern = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
ok = bool(pattern.fullmatch(email.strip()))
print("valid" if ok else "invalid")

The raw string prefix r before the pattern is the Python way to keep backslashes intact. fullmatch behaves like the ^ and $ anchors: it requires the whole string to match rather than a substring.

What about plus addressing and subdomains?

The simple pattern accepts plus addressing such as name+tag@example.com and domains with subdomains such as name@mail.example.com, because plus and dots are allowed characters and the pattern only requires a final dot. It also accepts addresses with international characters if the input includes them. If you need stricter rules, add them deliberately rather than pasting a bigger regex, and consider a library such as email-validator for the edge cases you actually care about.

What is the verification email for?

Syntax validation answers is this shaped like an address. Only a confirmation loop answers does a person control this mailbox, and that is the question every sign-up form really asks. Send the link, expire it after a short window, and treat the account as unverified until the click lands. For patterns in the same family, see the URL regex guide.

Frequently Asked Questions

Is my regex email pattern enough?

The simple pattern catches typos such as missing @ or a dotless domain, which is most of what goes wrong. It accepts a few edge cases the RFC would reject and rejects a few exotic valid addresses, and for the real world that trade-off is usually the right one.

Why is the RFC 5322 email regex so huge?

The full grammar allows quoted strings, comments, escaped characters and internationalized forms. A faithful pattern is hundreds of characters long, and almost no one needs it, because a syntax-valid address can still not exist and a slightly nonconforming address can still work.

Should I match emails with a single regex?

Use the simplest check that stops real typos, then rely on a confirmation step to prove the address is real. Over-validating with a giant pattern rejects real users and still cannot prove the mailbox exists.

What does type=email do in HTML?

The browser validates the value against its own email rules before the form submits, and shows a built-in error message. It is free validation with no JavaScript, and you should still revalidate on the server.

Does the regex allow plus addressing?

The simple pattern does, because a plus is an allowed local-part character. Gmail-style aliases such as name+tag@example.com pass, which is correct behavior - they are valid addresses.