Incomplete Leo

Something from my heart

Comparing E-mail Address Validating Regular Expressions

Comparing E-mail Address Validating Regular Expressions
Updated: 12/15/2006

I wanted to find a regular expression to validate e-mail addresses, but when I searched I found dozens of slightly different versions of the same expression. Almost all of them had people leaving comments about how it failed to validate or invalidate some rare but completely valid/invalid address syntax. But dispite all the criticism, I couldn’t find a definitive “best” expression. [Here is a good explanation of why there’s no perfect regular expression to validate e-mail addresses]. So, I setup arrays of some of the most promising expressions as well as a (hopefully) complete sampling of valid and invalid addresses. It’s my philosophy that it’s better to accept a few invalid addresses than reject any valid ones, so I’m shooting for 0 false-positives and as few false-negatives as possible.

Conclusion:
The best one was: ^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$ from Quirksmode.org
but it still had some problems so I modified it to come up with: ^([a-zA-Z0-9_’+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$

Note: Some of these are dependent on being executed using case-insenstive regex functions

 

^([a-zA-Z0-9_’+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$
My modification of Quirksmode.org regex
Uses eregi()

These should be valid
sufjan@fightingforalostcause.net valid
tim-o’reilly@oreilly.com valid
michael.o’leary@domain.com valid
info@chester.museum valid
ok@911.com valid
ian_dunn@yah00.com valid
someone@209.523.264.26 valid
someone-else@209.523.264.26:25 valid
1@test.com valid
ok@foo.b-r.com valid
o.k@foo.com valid
ok7@foo.com valid
ok@dmv.ca.us valid
.tom.@example.com valid
$%^%JidO_kdoe*&&2@lkd.vfg valid
gav..gav@glensharp.com valid
!def!xyz%abc@example.com valid
*@foo.com valid
a@foo.com valid
jack+jill@inlove.com valid
 
These should be invalid
ian invalid
ian@.com invalid
  invalid
@.org invalid
ian.dunn.net invalid
ian@do@net invalid
_invalid@foo.com valid
invalid_@foo.com valid
invalid.@foo.com valid
inv@lid@foo.com invalid
inv#lid@foo.com invalid
invalid@foo..com invalid
invalid@-foo.com valid
invalid@foo-.com valid
invalid@foocom invalid
invalid@f#o.com invalid
invalid@foo.commie valid
invalid@foo.c0m valid
invalid@foo.b@r.com invalid
invalid@foo.b#r.com invalid
invalid@foo. invalid
invalid@foo_bar.com invalid

Valid: 20/20
Invalid: 15/22

 

^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$
http://www.quirksmode.org/js/mailcheck.html
Uses eregi()

These should be valid
sufjan@fightingforalostcause.net valid
tim-o’reilly@oreilly.com invalid
michael.o’leary@domain.com invalid
info@chester.museum valid
ok@911.com valid
ian_dunn@yah00.com valid
someone@209.523.264.26 valid
someone-else@209.523.264.26:25 invalid
1@test.com valid
ok@foo.b-r.com valid
o.k@foo.com valid
ok7@foo.com valid
ok@dmv.ca.us valid
.tom.@example.com valid
$%^%JidO_kdoe*&&2@lkd.vfg invalid
gav..gav@glensharp.com valid
!def!xyz%abc@example.com invalid
*@foo.com invalid
a@foo.com valid
jack+jill@inlove.com invalid
 
These should be invalid
ian invalid
ian@.com invalid
  invalid
@.org invalid
ian.dunn.net invalid
ian@do@net invalid
_invalid@foo.com valid
invalid_@foo.com valid
invalid.@foo.com valid
inv@lid@foo.com invalid
inv#lid@foo.com invalid
invalid@foo..com invalid
invalid@-foo.com valid
invalid@foo-.com valid
invalid@foocom invalid
invalid@f#o.com invalid
invalid@foo.commie valid
invalid@foo.c0m valid
invalid@foo.b@r.com invalid
invalid@foo.b#r.com invalid
invalid@foo. invalid
invalid@foo_bar.com invalid

Valid: 13/20
Invalid: 15/22

 

^[_a-zA-Z0-9-]+[@][a-zA-Z0-9]+[\.](com|net|org){1}$
My original attempt
Uses eregi()

These should be valid
sufjan@fightingforalostcause.net valid
tim-o’reilly@oreilly.com invalid
michael.o’leary@domain.com invalid
info@chester.museum invalid
ok@911.com valid
ian_dunn@yah00.com valid
someone@209.523.264.26 invalid
someone-else@209.523.264.26:25 invalid
1@test.com valid
ok@foo.b-r.com invalid
o.k@foo.com invalid
ok7@foo.com valid
ok@dmv.ca.us invalid
.tom.@example.com invalid
$%^%JidO_kdoe*&&2@lkd.vfg invalid
gav..gav@glensharp.com invalid
!def!xyz%abc@example.com invalid
*@foo.com invalid
a@foo.com valid
jack+jill@inlove.com invalid
 
These should be invalid
ian invalid
ian@.com invalid
  invalid
@.org invalid
ian.dunn.net invalid
ian@do@net invalid
_invalid@foo.com valid
invalid_@foo.com valid
invalid.@foo.com invalid
inv@lid@foo.com invalid
inv#lid@foo.com invalid
invalid@foo..com invalid
invalid@-foo.com invalid
invalid@foo-.com invalid
invalid@foocom invalid
invalid@f#o.com invalid
invalid@foo.commie invalid
invalid@foo.c0m invalid
invalid@foo.b@r.com invalid
invalid@foo.b#r.com invalid
invalid@foo. invalid
invalid@foo_bar.com invalid

Valid: 6/20
Invalid: 20/22

 

^([A-Z0-9]+[._]?){1,}[A-Z0-9-]+\@(([A-Z0-9]+[-]?){1,}[A-Z0-9]+\.){1,}([A-Z]{2,4}|museum)$
http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/59886
Uses eregi()

These should be valid
sufjan@fightingforalostcause.net valid
tim-o’reilly@oreilly.com invalid
michael.o’leary@domain.com invalid
info@chester.museum valid
ok@911.com valid
ian_dunn@yah00.com valid
someone@209.523.264.26 invalid
someone-else@209.523.264.26:25 invalid
1@test.com invalid
ok@foo.b-r.com valid
o.k@foo.com valid
ok7@foo.com valid
ok@dmv.ca.us valid
.tom.@example.com invalid
$%^%JidO_kdoe*&&2@lkd.vfg invalid
gav..gav@glensharp.com invalid
!def!xyz%abc@example.com invalid
*@foo.com invalid
a@foo.com invalid
jack+jill@inlove.com invalid
 
These should be invalid
ian invalid
ian@.com invalid
  invalid
@.org invalid
ian.dunn.net invalid
ian@do@net invalid
_invalid@foo.com invalid
invalid_@foo.com invalid
invalid.@foo.com invalid
inv@lid@foo.com invalid
inv#lid@foo.com invalid
invalid@foo..com invalid
invalid@-foo.com invalid
invalid@foo-.com invalid
invalid@foocom invalid
invalid@f#o.com invalid
invalid@foo.commie invalid
invalid@foo.c0m invalid
invalid@foo.b@r.com invalid
invalid@foo.b#r.com invalid
invalid@foo. invalid
invalid@foo_bar.com invalid

Valid: 8/20
Invalid: 22/22

 

^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
http://www.regular-expressions.info/email.html
Uses eregi()

These should be valid
sufjan@fightingforalostcause.net valid
tim-o’reilly@oreilly.com invalid
michael.o’leary@domain.com invalid
info@chester.museum invalid
ok@911.com valid
ian_dunn@yah00.com valid
someone@209.523.264.26 invalid
someone-else@209.523.264.26:25 invalid
1@test.com valid
ok@foo.b-r.com valid
o.k@foo.com valid
ok7@foo.com valid
ok@dmv.ca.us valid
.tom.@example.com valid
$%^%JidO_kdoe*&&2@lkd.vfg invalid
gav..gav@glensharp.com valid
!def!xyz%abc@example.com invalid
*@foo.com invalid
a@foo.com valid
jack+jill@inlove.com invalid
 
These should be invalid
ian invalid
ian@.com invalid
  invalid
@.org invalid
ian.dunn.net invalid
ian@do@net invalid
_invalid@foo.com valid
invalid_@foo.com valid
invalid.@foo.com valid
inv@lid@foo.com invalid
inv#lid@foo.com invalid
invalid@foo..com valid
invalid@-foo.com valid
invalid@foo-.com valid
invalid@foocom invalid
invalid@f#o.com invalid
invalid@foo.commie invalid
invalid@foo.c0m invalid
invalid@foo.b@r.com invalid
invalid@foo.b#r.com invalid
invalid@foo. invalid
invalid@foo_bar.com invalid

Valid: 11/20
Invalid: 16/22

 

^[a-z0-9\._-]+@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+)$
http://www.zend.com/zend/spotlight/code-gallery-wade5.php?article=code-gallery-wade5&kind=sl&id=1988&open=1&anc=0&view=1#Heading10
Uses eregi()

These should be valid
sufjan@fightingforalostcause.net valid
tim-o’reilly@oreilly.com invalid
michael.o’leary@domain.com invalid
info@chester.museum valid
ok@911.com valid
ian_dunn@yah00.com valid
someone@209.523.264.26 invalid
someone-else@209.523.264.26:25 invalid
1@test.com valid
ok@foo.b-r.com valid
o.k@foo.com valid
ok7@foo.com valid
ok@dmv.ca.us valid
.tom.@example.com valid
$%^%JidO_kdoe*&&2@lkd.vfg invalid
gav..gav@glensharp.com valid
!def!xyz%abc@example.com invalid
*@foo.com invalid
a@foo.com valid
jack+jill@inlove.com invalid
 
These should be invalid
ian invalid
ian@.com invalid
  invalid
@.org invalid
ian.dunn.net invalid
ian@do@net invalid
_invalid@foo.com valid
invalid_@foo.com valid
invalid.@foo.com valid
inv@lid@foo.com invalid
inv#lid@foo.com invalid
invalid@foo..com invalid
invalid@-foo.com invalid
invalid@foo-.com invalid
invalid@foocom invalid
invalid@f#o.com invalid
invalid@foo.commie valid
invalid@foo.c0m invalid
invalid@foo.b@r.com invalid
invalid@foo.b#r.com invalid
invalid@foo. invalid
invalid@foo_bar.com invalid

Valid: 12/20
Invalid: 18/22

 

/^[^\x00-\x20()<>@,;:\”.[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\”.[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\”.[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\”.[\]\x7f-\xff]+)+$/i
http://www.webmaster-forums.net/showthread.php?s=314529bbe536c5978d88e308479498e9&p=209363
Uses preg_match()

These should be valid
sufjan@fightingforalostcause.net valid
tim-o’reilly@oreilly.com valid
michael.o’leary@domain.com valid
info@chester.museum valid
ok@911.com valid
ian_dunn@yah00.com valid
someone@209.523.264.26 valid
someone-else@209.523.264.26:25 invalid
1@test.com valid
ok@foo.b-r.com valid
o.k@foo.com valid
ok7@foo.com valid
ok@dmv.ca.us valid
.tom.@example.com invalid
$%^%JidO_kdoe*&&2@lkd.vfg valid
gav..gav@glensharp.com invalid
!def!xyz%abc@example.com valid
*@foo.com valid
a@foo.com valid
jack+jill@inlove.com valid
 
These should be invalid
ian invalid
ian@.com invalid
  invalid
@.org invalid
ian.dunn.net invalid
ian@do@net invalid
_invalid@foo.com valid
invalid_@foo.com valid
invalid.@foo.com invalid
inv@lid@foo.com invalid
inv#lid@foo.com valid
invalid@foo..com invalid
invalid@-foo.com valid
invalid@foo-.com valid
invalid@foocom invalid
invalid@f#o.com valid
invalid@foo.commie valid
invalid@foo.c0m valid
invalid@foo.b@r.com invalid
invalid@foo.b#r.com valid
invalid@foo. invalid
invalid@foo_bar.com valid

Valid: 17/20
Invalid: 12/22

 

^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$
Used by previous developer in a website I’m working on
Uses eregi()

These should be valid
sufjan@fightingforalostcause.net valid
tim-o’reilly@oreilly.com invalid
michael.o’leary@domain.com invalid
info@chester.museum valid
ok@911.com valid
ian_dunn@yah00.com valid
someone@209.523.264.26 invalid
someone-else@209.523.264.26:25 invalid
1@test.com valid
ok@foo.b-r.com valid
o.k@foo.com valid
ok7@foo.com valid
ok@dmv.ca.us valid
.tom.@example.com invalid
$%^%JidO_kdoe*&&2@lkd.vfg invalid
gav..gav@glensharp.com invalid
!def!xyz%abc@example.com invalid
*@foo.com invalid
a@foo.com valid
jack+jill@inlove.com valid
 
These should be invalid
ian invalid
ian@.com invalid
  invalid
@.org invalid
ian.dunn.net invalid
ian@do@net invalid
_invalid@foo.com valid
invalid_@foo.com valid
invalid.@foo.com invalid
inv@lid@foo.com invalid
inv#lid@foo.com invalid
invalid@foo..com invalid
invalid@-foo.com valid
invalid@foo-.com valid
invalid@foocom invalid
invalid@f#o.com invalid
invalid@foo.commie valid
invalid@foo.c0m invalid
invalid@foo.b@r.com invalid
invalid@foo.b#r.com invalid
invalid@foo. invalid
invalid@foo_bar.com invalid

Valid: 11/20
Invalid: 17/22

Tháng Ba 22, 2007 - Posted by | .NET Related

Chưa có phản hồi.

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s

%d bloggers like this: