Live data from Hacker News

Ruby's Email Address Regexp

github.com

31–40 of 69 posts

Re: Ruby's Email Address Regexp

#31
post #13

Earlier quoted context omitted.

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

And 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

Yep! 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 “.+@.+”.

Re: Ruby's Email Address Regexp

#32

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…

No post body was provided.

Re: Ruby's Email Address Regexp

#33
post #13

Earlier quoted context omitted.

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

And 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

I'll quote the WHATWG HTML spec, which that Ruby code cites as its source (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.

Re: Ruby's Email Address Regexp

#34

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…

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

Re: Ruby's Email Address Regexp

#35
post #6

Earlier quoted context omitted.

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!

I would use `email.includes('@')`, it only needs to be polyfilled for IE since it's in every modern browser JS engine.

[deleted]

Re: Ruby's Email Address Regexp

#36
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!

Why 2? Greater than 0, sure.

Re: Ruby's Email Address Regexp

#37
post #9

Earlier quoted context omitted.

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

If you are writing a webpage that accepts email addresses, then please just use 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.

That’s just for the client though. Best to check on the server as an authoritative source of truth.

Re: Ruby's Email Address Regexp

#38
post #36
post #6

Earlier quoted context omitted.

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!

Why 2? Greater than 0, sure.

Yeah...I have an email address I use a lot, "me@(domain)". It's perfectly valid, despite indexOf("@") == 2.

Re: Ruby's Email Address Regexp

#39

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…

Level 4:

You need an address backed by an actually valid mailbox. At which point you need to send an email to the address to validate.

Re: Ruby's Email Address Regexp

#40

SMTP 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]
Post reply on HN