I should point out it's generally not a good idea to try to use regex on an email address. It's surprisingly complex and very easy to miss something. I'd rely on type="email" and send a verification link server-side.
What out the WHATWG's take on it: http://www.whatwg.org/specs/web-apps/current-work/multipage/...
Specifically, they've redefined the format of email addresses in a way that's much simpler and is actually amenable to regular expressions. "This requirement is a willful violation of RFC 5322, which defines a syntax for e-mail 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."
The spec even includes a Perl-compatible regex as an example. :)
s/What out/Check out/ # How the hell did I miss that typo? :(
You can edit your comments on HN, right?
Not after an hour.
Re:HTML5 Features you need to know
by ubernostrum on Sunday May 26, 2013 @02:01AM
(#5770210)
You know, I really think at this point that the random historical crap that's technically legal in an email address should be treated the same as if you walked into a restaurant and tried to order in Latin. It's time to retire a lot of it and tell people to move on.
That doesn't change the fact that a lot of validators disallow common, everyday things like + tags or even . characters. The only good way to be sure is to make sure the address contains an @ SOMEWHERE other than the last character (.+@.+, maybe), and then send a validation e-mail.
Re:HTML5 Features you need to know
by JoeAcchino on Saturday May 25, 2013 @05:25PM
(#5768913)
This regexp
[^ @]*@[^ @]*
will validate strings like `@example.com` and `joe@` and even a single `@`. Probably [^ @]+@[^ @]+
is what he wants, plus server side validation (that is the classic "click link to validate address" email).
Re:HTML5 Features you need to know
by Tautologistics on Sunday May 26, 2013 @02:29PM
(#5771956)
Forgot the SOL/EOL markers; the above will still match a substring, e.g. xxx@[email protected]. Try:
^[^ @]+@[^ @]+$
That said, HTML5 has something even better if the input is expected to be an email address: input@type=email
See http://www.w3.org/TR/html-markup/input.email.html for more details.
Thanks for the suggestion, i will check that :)
Re:HTML5 Features you need to know
by richbradshaw on Sunday May 26, 2013 @06:18AM
(#5770703)
The good thing is that the regex will be the same. It's also pretty trivial to write a polyfill to work on old browsers. I've found it's really nice to define that in one place then to use it everywhere. Newer users get the HTML5 validation, older ones the Javascript fallback, and everyone gets a second round of checking before it hits the database.
No, don't validate email addresses with regex. It's more complex than that.
Pattern attribute can also be used to specify what kind of keyboard should your smartphone show when field receives focus, e.g. numeric only.
The pattern attribute for regular expressions looks like a great addition. Obligatory comment that "no more...server side code to check if the user's input is a valid email" is clearly a terrible advice. Always validate on the server.