March 8, 2026·6 min read·RegexTutorial

How to Write Regex for Email Validation (The Right Way)

Email validation is one of the most common uses of regex — and one of the most misunderstood. Search for "email regex" and you'll find everything from one-liners to 200-character monsters claiming RFC compliance. This guide cuts through the noise: you'll learn the simple email regex pattern that works for 99% of cases, understand why perfect validation is impossible, and know when to reach for more complex patterns.

The Simple Email Regex (Start Here)

For most applications, this pattern is all you need:

// Simple email regex (good enough for 99% of cases)
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

// Test it
emailRegex.test("user@example.com");     // ✅ true
emailRegex.test("invalid.email");        // ❌ false
emailRegex.test("user@domain");          // ❌ false
emailRegex.test("user @example.com");    // ❌ false

What it does: Checks for at least one character before the @, at least one character after it, and at least one character after a dot. It rejects emails with spaces and ensures the basic structure is present.

What it doesn't do: Validate that the domain actually exists, check for typos like gmial.com, or enforce strict RFC 5322 compliance. That's fine — those checks belong in your backend, not your regex.

A Better Email Regex (Recommended)

If you want slightly stricter validation without going overboard, use this:

// More robust email regex
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

// What this matches:
// ✅ user@example.com
// ✅ john.doe+tag@company.co.uk
// ✅ test_123@sub.domain.com
// ❌ @example.com (no local part)
// ❌ user@.com (no domain name)
// ❌ user@domain (no TLD)

This pattern adds character class restrictions and enforces a minimum 2-character top-level domain (TLD). It handles common formats like user+tag@domain.comand first.last@company.co.uk while rejecting obvious garbage.

💡 Why this pattern works

The + symbol in email addresses (e.g., user+tag@example.com) is valid and widely used for email filtering. The pattern [a-zA-Z0-9._%+-]+allows letters, numbers, dots, underscores, percent signs, plus signs, and hyphens — covering 99.9% of real-world email addresses.

The RFC 5322 Compliant Regex (Probably Overkill)

The official email specification (RFC 5322) allows bizarre edge cases like quoted strings, comments, and IP addresses as domains. A fully compliant regex looks like this:

// RFC 5322 compliant email regex (overkill for most cases)
const rfcEmailRegex = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;

// This handles edge cases like:
// ✅ "user name"@example.com (quoted local part)
// ✅ user@[192.168.1.1] (IP address domain)
// ✅ user+tag@sub-domain.example.com

// But do you really need this? Probably not.

Should you use this? Almost certainly not. This pattern is unreadable, hard to maintain, and validates emails that real mail servers reject anyway. Stick with the simpler patterns unless you have a specific requirement for RFC compliance.

Common Email Regex Mistakes

  • Forgetting to escape the dot: In regex, . matches any character. Use \\. to match a literal dot.
  • Blocking valid characters: Email addresses can contain +,_, %, and hyphens. Don't reject them.
  • Requiring specific TLDs: New TLDs are added constantly. Don't hardcode .com|.net|.org — use [a-zA-Z]{2,} instead.
  • Trusting regex alone: Regex can't verify thatuser@nonexistent-domain-12345.com is a real address. Always send a confirmation email.

Email Validation in Different Languages

Python

import re

# Simple email validation in Python
email_pattern = r'^[^\s@]+@[^\s@]+\.[^\s@]+$'

def is_valid_email(email):
    return re.match(email_pattern, email) is not None

# Test
print(is_valid_email("user@example.com"))  # True
print(is_valid_email("invalid@"))          # False

HTML5 (Browser Validation)

<!-- HTML5 email input with built-in validation -->
<input 
  type="email" 
  name="email" 
  required 
  pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
  placeholder="your@email.com"
/>

<!-- The browser validates automatically on submit -->

HTML5's type="email" provides built-in validation that's good enough for most forms. Combine it with a custom pattern attribute if you need stricter rules.

When to Use Simple vs Strict Validation

Use CaseRecommended Pattern
Newsletter signup^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$
User registration^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Enterprise B2B app^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Email parsing toolRFC 5322 compliant (or use a library)

The Truth About Email Validation

Here's the uncomfortable reality: perfect email validation is impossible with regex alone. The only way to know if an email address is valid is to send a message to it and see if it bounces.

Your regex should do two things:

  1. Catch obvious typos (missing @, no domain, etc.)
  2. Prevent malicious input (SQL injection, XSS attempts)

Everything else — checking if the domain exists, verifying the mailbox is active, detecting disposable email services — belongs in your backend validation logic, not your regex pattern.

Generate custom email regex patterns instantly

RegSQL's AI Regex Generator creates validation patterns for emails, phone numbers, URLs, and more — with plain-English explanations. Describe your requirements and get production-ready regex in seconds.

🔍 Try RegSQL Regex Generator Free →

Quick Reference

  • Simple pattern: ^[^\s@]+@[^\s@]+\.[^\s@]+$
  • Recommended pattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
  • Always escape dots: Use \. not .
  • Allow common characters: + _ % - are all valid
  • Don't hardcode TLDs: Use [a-zA-Z]{2,} instead
  • Send confirmation emails: Regex can't verify deliverability

The best email validation strategy combines a reasonable regex pattern with backend verification and a confirmation email. Keep your regex simple, readable, and focused on catching obvious errors — not achieving theoretical perfection.