Introduction

Email validation is crucial for forms and user input. However, perfect email validation is complex due to the extensive RFC 5322 specification. This guide covers practical validation approaches.

Common Regex Patterns

Simple Pattern

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

Problems:

  • Doesn’t handle all edge cases
  • May reject valid emails

More Complete Pattern

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

Best Practices

✅ Do This

1. Validate format, then verify

// Step 1: Format validation
if (!emailRegex.test(email)) {
  return "Invalid format";
}

// Step 2: Send verification email
sendVerificationEmail(email);

2. Use HTML5 validation

<input type="email" required />

❌ Don’t Do This

1. Don’t over-validate

// ❌ Too strict - rejects valid emails
/^[a-z]+@[a-z]+\.[a-z]{2,3}$/;

Tools

Use our tools:

Conclusion

Email validation:

Approach:

  • Basic format check
  • Send verification email
  • Don’t over-validate

Remember:

  • Perfect validation is impossible
  • Verification email is essential
  • Use HTML5 type=“email”

Next Steps