Comments on: TIP: Simple Regular Expression for Email Validation https://gofedora.com/tip-simple-regular-expressi-email-validation/ How Tos, Tutorials, Tips and Tricks Fri, 19 Jun 2015 15:19:58 +0000 hourly 1 https://wordpress.org/?v=4.9.4 By: Raj https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-102929 Wed, 06 Jun 2012 08:22:25 +0000 http://gofedora.com/?p=667#comment-102929 Here is Regular Expression for RFC 5322

/^(?:[a-zA-Z0-9!#$%&’*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&’*+/=?^_`{|}~-]+)*|”(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*”)@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))/;

This wil validate following valid and invalid mail Ids:

Valid email addresses

niceandsimple@example.com
simplewith+symbol@example.com
less.common@example.com
a.little.more.unusual@dept.example.com
user@[IPv6:2001:db8:1ff::a0b:dbd0]
“much.more\ unusual”@example.com
“very.unusual.@.unusual.com”@example.com
“very.(),:;[]\”.VERY.\”very@\\\ \”very\”.unusual”@strange.example.com
0@a
!#$%&’*+-/=?^_`{}|~@example.org
“()[]:,;@\\\”!#$%&’*+-/=?^_`{}|\ \ ~\ \ \ ?\ \ \ ^_`{}|~.a”@example.org
“”@example.org
postbox@com (top-level domains are valid hostnames)

Invalid email addresses

Abc.example.com (an @ character must separate the local and domain parts)
Abc.@example.com (character dot(.) is last in local part)
Abc..123@example.com (character dot(.) is double)
A@b@c@example.com (only one @ is allowed outside quotation marks)
a”b(c)d,e:f;gi[j\k]l@example.com (none of the special characters in this local part is allowed outside quotation marks)
just”not”right@example.com (quoted strings must be dot separated, or the only element making up the local-part)
this is”not\allowed@example.com (spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a slash)
this\ still\”not\\allowed@example.com (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must

]]>
By: Frank https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-39712 Mon, 06 Dec 2010 13:50:03 +0000 http://gofedora.com/?p=667#comment-39712 It worked as a charm 🙂

]]>
By: Bjoern https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-34751 Tue, 14 Sep 2010 13:35:12 +0000 http://gofedora.com/?p=667#comment-34751 Come on, Bernie! Your suggestion is a joke, right? Didn’t think it through, right? Verifying an email your way would mean

1. any email thats not established yet to be invalid.
2. For existing emails your method would be the slowest imagineable and would of course not only
3. lead to unnecessary network traffic, but also to
4. the requirement of being always connected.

]]>
By: Ian https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-14301 Tue, 01 Dec 2009 21:50:55 +0000 http://gofedora.com/?p=667#comment-14301 I wrote a script that compared various regexs from around the web against a set of sample data and the best one I found was:

/^[-_a-z0-9\’+*$^&%=~!?{}]++(?:\.[-_a-z0-9\’+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD

More details @ Comparing E-mail Address Validating Regular Expressions

]]>
By: CakePHP – Email Validation « Eureka! https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-5330 Fri, 18 Sep 2009 01:32:11 +0000 http://gofedora.com/?p=667#comment-5330 […] Reference: TIP: Simple Regular Expression for Email Validation […]

]]>
By: Bernie https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-2288 Tue, 18 Aug 2009 15:23:21 +0000 http://gofedora.com/?p=667#comment-2288 E-mail validation means RFC 5322. If your regex doesn’t match it you are perceived as thus redefining this standard; how angry would you be if you have a valid e-mail address according to the real standard and someone else’s web page tells them they don’t according to their regex?

Here’s what to do.

1. Check there’s a valid MX record for the domain.
2. Do an SMTP call-forward to that domain: connect to the lowest priority MX on port 25 and issue
EHLO my.machine.name[crlf]
MAIL FROM:[crlf]
RCPT TO:[crlf]
RSET
QUIT

Wait for a banner first, and a response each time beginning “2nn “.

After the RCPT TO, “2nn ” means e-mail address deliverable, “4nn ” means not currently deliverable, “5nn ” means not deliverable. Up to you how you treat “4nn “.

If the above is too difficult, just send them an e-mail containing a token needed to complete whatever your process is. Then not only do you know it’s a valid address, but even that it is the user’s actual address.

]]>
By: Kulbir Saini https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-2277 Tue, 18 Aug 2009 09:31:14 +0000 http://gofedora.com/?p=667#comment-2277 @Edward, Tushar Thanks for sharing your regex(s). I’ll test them out.
@Bernie Take it easy dude. I am not defining a standard. I needed a simple regex for email validation in my app and I wrote it. Period.

]]>
By: Tushar(Chia) https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-2265 Tue, 18 Aug 2009 02:15:07 +0000 http://gofedora.com/?p=667#comment-2265 Here is the Jquery Email verification function
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((“[\w-\s]+”)|([\w-]+(?:\.[\w-]+)*)|(“[\w-\s]+”)([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
Regards

]]>
By: Bernie https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-2256 Mon, 17 Aug 2009 21:28:20 +0000 http://gofedora.com/?p=667#comment-2256 Your regex does not appear to be based on RFC 5322.

What was your source? It looks to me like you’re choosing to reinvent a standard which has carefully been considered by more appropriate people.

This is exactly the sort of thing the F/OSS community accuses proprietary software houses of doing.

]]>
By: Edward Rudd https://gofedora.com/tip-simple-regular-expressi-email-validation/comment-page-1/#comment-2254 Mon, 17 Aug 2009 21:20:26 +0000 http://gofedora.com/?p=667#comment-2254 Here is my validation Regex which matches a bit more that yours. Specifically emails with apostrophes, which I would prefer not be allowed, but several corporations allow them. also it only allowed a-z in the domain name.

This regex is designed to run in “case-insensitive mode”

this link was a very nice source of why certain characters that are allowed in the email spec probably shouldn’t and aren’t by most systems.

http://www.remote.org/jochen/mail/info/chars.html

^(?:(?:[\-+%=_’a-z0-9]+)(?:\.(?![\.@]))?)+@(?:[a-z0-9\-]+\.)+[a-z]+

Though personally I like the regex on this page

http://ex-parrot.com/~pdw/Mail-RFC822-Address.html

That’s a good one to scare anyone.

]]>