by Freak_NL · 10y ago · 0 comments
The most sensible approach (in my opinion) is to validate with this minimal regex:
/.+@.+/
Essentially just requiring an at-mark between two other bits of text. After that send a confirmation email to see if it works. That is pretty much fool-proof and low maintenance.If you absolutely must help the user in preventing any common mistakes, use a library that provides hints in the UI for those presumed errors without actually invalidating the input. There are JavaScript libraries that do this fairly well.
I've been using this regex to make sure there actually is text before and after the @
/.+@.+/ /^[^@\s]+@[^@\s]+$/
Filtering out emails with multiple @'s and whitespace seems like an obvious win to help prevent typos and copy-paste errors.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.
Wikipedia <https://en.wikipedia.org/wiki/Email_address#Valid_email_addr... > gives the following example:
"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.comThere 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?
Nevermind. `/.+@.+/` it is.