
Email Validation: Why Your Regex Is Wrong and What to Do Instead
The email validation regex that circulates in Stack Overflow answers and blog posts rejects perfectly valid email addresses and accepts some invalid ones. The RFC 5321 specification for email addresses is far more permissive than most developers realize, and trying to validate with a regex is a losing battle. Here is what actually works. Valid email addresses that your regex probably rejects All of the following are valid per the RFC: user+tag@example.com (plus addressing, used by Gmail for filtering) "user name"@example.com (quoted local part with spaces) user@[192.168.1.1] (IP address literal) very.unusual."@".unusual.com@example.com (quoted strings with special characters) x@example.com (single character local part) user@subdomain.subdomain.example.com (multiple subdomains) Most validation regexes reject several of these. The plus sign causes failures in roughly 20% of email validation implementations, which breaks Gmail's plus addressing feature. The regex that tries to be complete
Continue reading on Dev.to Beginners
Opens in a new tab




