11 ms·
Ruby's Email Address Regexp
- jonpalmisc 5y agoI feel like every developer at some point Googles "URL regex" and is inevitably led down a rabbit hole of different regexes — some optimizing for maximum accuracy, others for minimum insanity. Having been down that rabbit hole before myself, I have to admit, this email regex is tamer than I expected it to be.
- ddoolin 5y agoE-mails, URLs, file names/extensions, these are the bane of my RegEx existence. Agreed, this is not as bad as I've seen in other places.
- secabeen 5y agoThe best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.
- ghayes 5y agoThe regex listed there isn’t that much more complex. It’s basically a check for *@a*.* where * is some minimal whitelist of valid characters and a is an alphanumeric to start the domain name.
- merlinscholz 5y agoAnd even though that sounds reasonable, it is still wrong. Technically, you do not even need the dot, if you add an MX record to a whole TLD or have some other funky DNS setup. Source: https://www.netmeister.org/blog/email.html https://www.netmeister.org/blog/email.html
- 0x640x6D 5y agoThe linked regexp doesn't require the TLD: https://rubular.com/r/Y9G4HR2Ox8gzqd https://rubular.com/r/Y9G4HR2Ox8gzqd
- NavinF 5y agoYep! I know that @mil addresses used to be a thing. With the explosion of new TLDs I would be unsurprised if name@gmail addresses became a thing in the future and silly devs handled it by adding an exception to their huge regex instead of just using “.+@.+”.
- ameliaquining 5y agoI'll quote the WHATWG HTML spec, which that Ruby code cites as its source (https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address https://html.spec.whatwg.org/multipage/input.html#valid-e-ma...): > This requirement is a willful violation of RFC 5322, which defines a syntax for email addresses that is simultaneously too strict (before the "@" character), too vague (after the "@" character), and too lax (allowing comments, whitespace characters, and quoted strings in manners unfamiliar to most users) to be of practical use here. Personally, I say if it's good enough for Web browsers, it's probably good enough for your app.
- 1123581321 5y agoIt’s probably a waste of time for an individual developer to write a one-off complicated regex for a contact form. A team of contributors to a standard library should be optimizing regex a bit more since doing so will save so many developers time vs using even a very simple one-off regex, when testing is accounted. The optimizations here are reasonable and internationally compatible.
- Gigachad 5y agoThere is really no point going further than this. It's more likely that someone will type the email wrong but still valid than they will type it completely invalid. There are also some completely wrong validators out there which expect the TLD to be 2-3 chars only. The ultimate email validation is just trying to send an email to the address and confirming with a code/link.
- ddoolin 5y agoI have had people rewrite my .io to .com because they were sure it was wrong, or something like that.
- DeepYogurt 5y agoWhat about @@.@?
- woranl 5y agoThere is nothing before the first @, and nothing after the last @.
- ojame 5y agoWhile that's true, `@@.@` fulfils "an @ symbol with something before it, and something after it".
- Skadabucci 5y agoThe original comment probably meant something like this: ^[^@]@[^@]$
- mekster 5y agoThat's only one letter before and after.
- duxup 5y agoPeople who have weird email addresses are used to them not working.
- secabeen 5y agoInteresting question. RFC1123 states that TLDs should be alphabetic, and that the first (or only) character must be a letter or a digit, which should exclude a TLD of '@'. local-parts can include most printable characters other than ()<>[];:@\,. /^+@+$/ is easy and the false positives it allows are weird enough to accept.
- mkdirp 5y agoNever mind the regex, `email.indexOf("@") > 2` does the trick and faster if you happen to need to check many emails. All websites these days require verification of emails (regardless of whether or not it's necessary), and if that's not enough validation, I don't know what is!
- rhinoceraptor 5y agoI would use `email.includes('@')`, it only needs to be polyfilled for IE since it's in every modern browser JS engine.
- deleted 5y ago[deleted]
- gumby 5y agoWhy 2? Greater than 0, sure.
- lostcolony 5y agoYeah...I have an email address I use a lot, "me@(domain)". It's perfectly valid, despite indexOf("@") == 2.
- zamadatix 5y agoThe index of the first character is 0 and an email must have a local part so that means the index of "@" has to be at least 1. My guess is OP also forgot the index of the first character is 0 instead of 1 resulting in 1+1=2 (that or they meant >=). Off by one errors are about half of working with arrays.
- zarzavat 5y agoThis is actually an uncommon example of an off-by-two error as they used strictly greater than 2. a@domain fail ab@domain fail abc@domain ok
- lilyball 5y agoThis depends on your use-case. If you're writing a mail agent, then you do probably want to parse email addresses in their entirety. If you're writing a website that accepts email addresses and wants to make sure the user doesn't just type "foo", then yeah, check for `.+@.+` and call it a day.
- runarberg 5y agoIf you are writing a webpage that accepts email addresses, then please just use <input type="email"> and check for: document.querySelector("[type='email']").validity.valid This will not only give you email validation for free, but also gives users with software keyboards (e.g. on phones) a contextual keyboard.
- tokumei 5y agoThat’s just for the client though. Best to check on the server as an authoritative source of truth.
- schappim 5y agoThe line of code you came for: https://github.com/ruby/ruby/blob/master/lib/uri/mailto.rb#L55 https://github.com/ruby/ruby/blob/master/lib/uri/mailto.rb#L...
- tyingq 5y agoThe one from Perl's Email::Valid is significantly harder on the eyes: https://metacpan.org/dist/Email-Valid/source/lib/Email/Valid.pm#L402 https://metacpan.org/dist/Email-Valid/source/lib/Email/Valid... Edit: Email::Valid is fairly well respected for getting the rules right...
- raasdnil 5y agoThere are basically three levels of address checking: 1) You need to validate an email field for login or a website - checking for an @ mark with some text before and at least one . after the @ will do for this. 2) You need to do some sort of address validation, library regexps like this will do for 99.9...% of these. 3) You are building an email handling system which needs to actually support the RFCs, in which case regexp will not handle what you need, and you need to use a proper parser, like https://github.com/mikel/mail/tree/master/lib/mail/parsers https://github.com/mikel/mail/tree/master/lib/mail/parsers Ref: I am the original author of the Ruby mail gem.
- vb6sp6 5y ago
- AdamJacobMuller 5y ago> at least one . after the @ will do for this technically not required... [adam@solomon]$ dig +noall +answer mx ai ai. 21572 IN MX 10 mail.offshore.ai.
- teruakohatu 5y agoAs soon as I read the OP's comment I knew someone would reply to say the dot was not technically required, but I didn't expect to see an actual publicly addressable example!
- AdamJacobMuller 5y agoI actually searched and was surprised no one beat me to it.
- zamalek 5y agoI've read this more than once, and it basically always boils down to: "The only way to verify an email address is to send an email to it." In my opinion, the optimal validation is soft validation: "your email address doesn't look right, continue with it?"
- tyingq 5y agoThe most helpful thing I've used in the real world is something that looks for common typographical errors, even if the email is technically valid. Like, if the user types "john.doe@gnail.com", it pops a dialogue asking "Did you mean john.doe@gmail.com?". But lets them keep what they typed, or do a different fix if needed. I found some JS called "mailcheck": https://github.com/mailcheck/mailcheck https://github.com/mailcheck/mailcheck I assume it's using popularity statistics, edit distance, etc, to come up with suggestions. There are updated clones that use react, vue, etc, instead of jquery. With a working ecommerce site, this improved the percentage of correct emails more than anything else I tried, and I had tried many things. Because it's a bad situation when you've taken someone's money and have nothing other than a shipping address to contact them if something goes wrong (bad shipping address, out of stock situation, etc).
- merlinscholz 5y agoRelevant: Your E-Mail Validation Logic is Wrong https://www.netmeister.org/blog/email.html https://www.netmeister.org/blog/email.html
- 0x640x6D 5y agoThis regexp and the whatwg one it is based off (correctly) do not validate the presence of a TLD since it's not technically required (foo@bar is considered valid). But if you are building consumer products it's best to test that there is at least a presence of something TLD-like after validating against this regexp.
- gclawes 5y agoRuby's URI classes are such a pain in the ass. They seem so un-rubyish
- superasn 5y agoSMTP had a very useful VRFY command after you've tested for the @ and MX record, but only a handful of service providers will tell you if the email is invalid nowadays due to spam concerns. Gmail still does though, which is a big deal as 90% of people who register on my sites are using a gmail address only and thus easy to verify instantly and notify the user to double check the email spelling.
- deleted 5y ago[deleted]
- nicoburns 5y agoYes, although that only helps if they typo the address to one that doesn’t happen to exist. Quite likely to hit a valid one by mistake with the size of gmails user base. I know at least one person who uses my emails address by mistake.
- nerdjon 5y agoI have yet to find the library that is doing this, but I have had a number of issues with website really not liking an "@me.com" email address. I assume there is some commonly used library (or multiple) out there that don't recognize an email a domain that is less than 3? But it is driving me insane, most recently I was on the phone with my vet and she told me their system told them my email was invalid (and would not accept it).
- mulmen 5y agoRecently I had a doctors office call me to confirm an appointment instead of obeying my wishes to be contacted via email. The email I provided to their contact form was <theirname>@<mydomain>. The receptionist was convinced the provided email was incorrect because it was "their" email. I'm not sure what I expected.
- foreigner 5y agoI've done this for decades so I could see who was selling my email address and spamming me, and the answer is: nobody. That's not where spam comes from apparently. I still do it out of habit though.
- don-code 5y agoIt's also interesting to step through the history on this line - it's undergone several revisions and, of course, also seen some reverts of well-intentioned features.
- User23 5y agoI have a .email domain that at least one major site rejects as invalid. Quite annoying.
- davchana 5y agoRight. Discover bank app's zelle settings don't allow any email.on .in domain, as in they assume that nobody from India, who already has an email on .in domain, will come to US & use their zelle.
- AceJohnny2 5y agoSee also this comparison of email regular expressions (as found in various languages and libraries), compared against a selection of valid email addresses: https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php https://fightingforalostcause.net/content/misc/2006/compare-...
- TheRealDunkirk 5y agoAren't all me.com addresses now icloud.com addresses as well? Couldn't you just tell them the other one?