Live data from Hacker News

Ruby's Email Address Regexp

github.com

61–69 of 69 posts

Re: Ruby's Email Address Regexp

#61
post #42

Earlier quoted context omitted.

> 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.

I'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?"

> In my opinion, the optimal validation is soft validation: "your email address doesn't look right, continue with it?"

And then hope the server's e-mail workflow can handle subaddressing because you entered in something like "username+service@gmail.com".

Seems like a lot of folks don't like '+', even though it's been part of the e-mail system since the 1980s.

Re: Ruby's Email Address Regexp

#62

Earlier quoted context omitted.

> 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.

Yep. Back in university I was acquainted with the fellow who set up the MX record for .ai (and assigned himself the memorable address n@ai). IIRC he was involved in planning an academic conference to be held there.

For the record, it is owned by Anguilla (in the Caribbean):

* https://en.wikipedia.org/wiki/.ai

Re: Ruby's Email Address Regexp

#63
post #6
post #3

The best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.

Never 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!

[deleted]

Re: Ruby's Email Address Regexp

#64
post #60
post #36

Earlier quoted context omitted.

Why 2? Greater than 0, sure.

Heh, I actually meant `>=`. But as others have pointed out, that still excludes 2 letter users. Really it should be `>= 0`.

> 0 I would think, no? Can an email address be '@domain', no preface?

Re: Ruby's Email Address Regexp

#65
post #3

The best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.

What about @@.@?

Interesting 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.

Re: Ruby's Email Address Regexp

#66

Earlier quoted context omitted.

> 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.

Yep. Back in university I was acquainted with the fellow who set up the MX record for .ai (and assigned himself the memorable address n@ai). IIRC he was involved in planning an academic conference to be held there.

Yeah I met him once. I didn't want to call him out on it :)

Re: Ruby's Email Address Regexp

#67

Earlier quoted context omitted.

> 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.

As 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!

I actually searched and was surprised no one beat me to it.

Re: Ruby's Email Address Regexp

#68

Earlier quoted context omitted.

The 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.

This 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

[deleted]

Re: Ruby's Email Address Regexp

#69

There 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…

Thank you. Your gem helped me multiple times throughout the years and helped me dealing with some hard problems
Post reply on HN