Hacker News new | ask | show | jobs
by infogulch 3594 days ago
Can there be more than one @ or whitespace? If not you could use:

    /^[^@\s]+@[^@\s]+$/
Filtering out emails with multiple @'s and whitespace seems like an obvious win to help prevent typos and copy-paste errors.
2 comments

Whitespace in front of the @ is only allowed within quotes, and any quoted part must be seperated by dots from unqoted parts. "my address"@example.com is allowed, as is my." ".address@example.com, but not my"address"@example.com.

Once you get that into a regex format, remember that a quoted dot is different from an unqoted dot, and don't forget that you handle comments within email addesses correctly.

There are plenty of reasons why people say not to use regex to validate email addresses.

Yes, the are valid e-mail addresses that contain whitespace or multiple @s.

Wikipedia <https://en.wikipedia.org/wiki/Email_address#Valid_email_addr... > gives the following example:

    "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com
Exactly. It is very much our nature as programmers to attempt to capture this seemingly trivial bit of structured text into seemingly innocuous programming rules, but the rewards of the added complexity are just too meager. Even if an email address validates, it still doesn't mean that you have a working email address. Sending an email is the only way to be sure, and if you are already doing that, then why risk excluding valid email addresses with a decidedly non-trivial regex?

There are regexes out there that capture all the complexity of valid email addresses today (but who knows if they'll work with, say, next year's additions to the top-level domains?), and you can copy them from StackOverflow if you really want them; but why bother?

It is safe in almost all applications to assume that quoted-string local-parts do not constitute a valid email address (ditto for domain literals).
Oh my.

Nevermind. `/.+@.+/` it is.