4 ms·
My goto for email validation is /^.+?@.+?\..+?$/ Incase I've typed it wrong, that should basically work for anything that contains at least one @ and one dot,
by MattBearman 13y ago
My goto for email validation is /^.+?@.+?\..+?$/
Incase I've typed it wrong, that should basically work for anything that contains at least one @ and one dot, in that order, as well as at least one character at beginning, middle and end. It's served me well thusfar.
Edit for clarification:
The reason I prefer this over just checking for an @ is that if you're just checking for @ a common mistake like "me@hotmail,com" will be considered valid.
- claudius 13y agoI think that the domain part of email addresses could be an IP address. Depending on how IPv6 addresses are displayed there, they won’t contain a dot. Somewhat artificial, yes.
- MattBearman 13y agoI'll admit I hadn't considered IPv6b addresses, might have to rethink my trusty regex. Sad, it's served me well for so many years.
- sp332 13y agoIPv4 also has a valid decimal representation. http://1249764136/ http://1249764136/ will send you to Google!
- hayksaakian 13y agoTIL. I wonder what the security implications of this is.
- syncsynchalt 13y agoIf using IPs in an email address you are supposed to surround it with square brackets anyway. I wouldn't add IP address support to an email regex because I'd rather turn away such perverse data anyway. Nobody uses IP-based email addresses.
- claudius 13y agoA square bracket, however, is not a dot (which is for what the original regexp checked). And I just tried sending an email to me@[my.ip.addr.[0]]. Postfix somehow recognised it was for this local host, but failed because it wasn’t part of the virtual domains I had put into it. ‘perverse’ seems to be somewhat appropriate. [0] Sorry, too lazy to check the appropriate documentation IP address ranges. 20db?
- MichaelApproved 13y agoI thought new TLD being worked on didn't need to have dots in them. Why not just check for x@x?
- fein 13y agoThat's exactly what you should do. ^(.+)@(.+)$ max length is 254 according to the RFC I believe, so you can check for that too.
- carbocation 13y agoBingo. I'm sure there are some HNers with arpanet emails who would appreciate this; it's not just about ipv6!
- therealunreal 13y agoIIRC, last I checked max length was 320.
- fein 13y agoI thought this as well, until looking for info one day and stumbling onto this: http://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address http://stackoverflow.com/questions/386294/what-is-the-maximu... Turns out the 320 value was actually incorrect, its 256 but the mailbox is wrapped in square brackets, so 256 - 2 = 254.
- jnelso99 13y agoThe bonus with that regex is that it kind of looks like a dead clown.
- MattBearman 13y agoAs far as I know the new tlds will still have a dot between tld and actual domain (I could be wrong though) but it's just been pointed out that IPv6 emails addresses could fail on my regex. The reason I don't currently use x@y is just to have a better shot at catching typos without being too restrictive.
- sirn 13y agoMy favorite: /.\@.*\../ It should be similar to your version, but only matches just enough parts that require for email validation (i.e. "o@example.c" part of foo@example.com).
- MattBearman 13y agoI like that, much more elegant! My only changes would be to make the middle .* into .+? as that way it requires at least one char, and the ? is for lazy repetition.
- rralian 13y agoYep, that's pretty much what I do too.
- deleted 13y ago[deleted]
- rallison 13y agoJust a top level domain after the @ is a valid email (e.g. foo@com). Things like this are why we end up with massive regular expressions for email validation. That said, if you are only warning the user, but not preventing them from submitting foo@com, then it is probably good enough.
- mikeash 13y agoNote that there are valid, probably in-use, e-mail addresses on TLDs, e.g. "username@cx". You may not care enough to support them though.