Course contentsShow
← JavaScript
Lesson 1729 of 2,72336. Regular ExpressionsFree lesson

Common Pattern: Email Validation

Building and understanding a practical regex pattern for basic email address validation.

Common Pattern: Email Validation

You'll learn how to build a practical regex pattern that validates basic email addresses using the concepts you've mastered.

Why Email Validation Matters

Email validation is one of the most common real-world uses of regular expressions. While perfect email validation is incredibly complex (the full RFC 5322 spec is thousands of words), a practical regex can catch 99% of typos and invalid formats.

A Practical Email Pattern

Here's a regex that handles typical email addresses:

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

emailPattern.test('user@example.com');     // true
emailPattern.test('john.doe@mail.co.uk');  // true
emailPattern.test('invalid@');             // false
emailPattern.test('@example.com');         // false
emailPattern.test('user@domain');          // false

Breaking It Down

  • ^ and $ anchor the pattern to match the entire string
  • [a-zA-Z0-9._%-]+ matches the username part (letters, digits, and common symbols)
  • @ matches the literal @ symbol
  • [a-zA-Z0-9.-]+ matches the domain name
  • \. matches the literal dot before the TLD (escaped because dot is a metacharacter)
  • [a-zA-Z]{2,} ensures the top-level domain is at least 2 letters

What This Pattern Doesn't Handle

This is a basic validator. It doesn't catch every edge case (like consecutive dots, or very rare valid formats). For production, you might use a library or combine regex with additional checks. But for form validation and catching obvious mistakes, this pattern works beautifully.

Key Takeaway: A practical email regex uses character classes for local and domain parts, anchors to ensure full-string matching, and escaped dots to validate structure—catching most typos without the complexity of RFC-perfect validation.