5 ms·
Email Address Regular Expression That 99.99% Works
- Retr0id 3y agoArchived: https://web.archive.org/web/20231024092745/https://emailregex.com/index.html https://web.archive.org/web/20231024092745/https://emailrege...
- erikgahner 3y agoThanks! I had problems accessing the webpage (due to my adblocker).
- HPsquared 3y agoVery ironic if that's a regex bug or "special case" in the adblocker filter list.
- evrimoztamur 3y agoEach time that this or its variants get posted here, the response is the same: Check if there’s an @ and send a verification email.
- tiborsaas 3y agoDon't use a regex, test only for a @ and a dot after it and call it a day. Btw content is unreadable thanks to aggressive adblocker detection so much that this feels like spam.
- bazoom42 3y agoEven foo@bar may be a vaild email.
- addandsubtract 3y agoThis is where the linked regex fails at.
- ceejayoz 3y agoOr correctly handles, for most of the use cases you’d want it for. If you’re building a web app, foo@gmailcom is almost certainly a mistake.
- bazoom42 3y agoOf course, since there isn’t a top level domain called gmailcom. But a regex cant tell you that.
- ceejayoz 3y agoGmail doesn't need to support "you own an entire TLD and use its root for email" use cases, so you can at least check for a dot after the @ and catch a bunch of typos that leave frustrated users.
- tiborsaas 3y agoI know about this, but how many domains exists with just a root domain? The vast majority of cases, a missing dot is a typo. I also wouldn't want to imagine how painful using services must be with a foo@bar style address.
- bazoom42 3y agoThe vast majority of common typos result in a syntactically valid email address anyway. A regex is just not an appropriate tool for email validation.
- ceejayoz 3y ago"We can't catch all typos, so we won't catch any" is a silly approach to form validation. I can't check that 123-45-6789 is a valid Social Security Number, but I can still reject "FARTS". The same is true for "foo@gmailcom" for a public-internet facing contact us form.
- bazoom42 3y agoWhy not reject invalid top level domains then? This is going to catch a vastly larger amount of typos (including foo@gmailcom) without eliminating any valid emails.
- Etheryte 3y agoYeah no, if you try to check this into any codebase it should get rejected straight away. There is no need to regex emails pretty much anywhere, when you use an email for signup or similar you send a confirmation email which serves the same purpose - to make sure the address is valid and correct. Use `<input type="email">` or check that "@" is present if you must, but anything beyond that is nonsense.
- shaky-carrousel 3y agoNot everything is web development.
- Etheryte 3y agoThat is fair, but I don't think it changes the outcome. The only way you can verify an email address is correct is to send a verification email. If you want to help the user ensure their input was what they intended, having two inputs for the same value is a better way to do that than a regex on a single field. As an example, my email is 20 chars long, the whole email regex thing only validates one or two of those 20 chars and doesn't help against typos in any way. In short, it's a lot of complexity without any real upside.
- ceejayoz 3y agoImagine how fast this falls down when writing, say, an email client. There’s a place for validation.
- a9ex 3y agoI usually just test for the presence of @ - that’s it. Works for IDN addresses as well. I remember some story back in the day where someone had an email address using the top level to domain only. Like “x@to”, pretty cool but probably a pain to use (:
- arp242 3y agoUse "mail.parse()" or whatever there is for your language. In general just parse stuff instead of letting some regexp loose on it. For a quick check to make sure people don't mix up fields or accidentally hit enter before they finished typing, just /.+@.+\..{2,}/ is more than enough (technically foo@com is valid, but no one uses that – note that root@localhost or cron@sysops CAN be valid, so in some contexts you want to use just /.+@.+/, but that doesn't really apply for signups and the like).
- swores 3y ago> "just /.+@.+\.{2,}/ is more than enough" Isn't that asking for 2x '.'s after the @, which isn't required? Should instead be /.+@.+\..+/ Which will allow foo@bar.com but not foo@bar ?
- arp242 3y agoEh yeah, I missed a dot there >_< I may be a sad case, but I don't write unit tests for my HN comments.
- creshal 3y agoWhy does this site need an adblocker-blocker?
- arun-mani-j 3y agoI have uBlock Origin and it works fine in my Firefox. Sorry I didn't knew of this adblock detection nonsense, otherwise wouldn't have posted it. :(
- sam_lowry_ 3y agoThe first thing I tried, root@localhost, did not match ;/
- mmh0000 3y agoThis[1] is or at least, was PHPs validator for email. Author provides a lot of insight into the complexity of regexing emails. [1] https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php https://fightingforalostcause.net/content/misc/2006/compare-...
- MilStdJunkie 3y agoWhy would you do this?! Mail parse libraries are everywhere