The best e-mail validating regular expression

They have: 222 posts

Joined: Sep 1999

I got frustrated with all the incomplete e-mail validating regex's out there and setup a test to try and find the best. So far this one validate's everything it should and only a few that it shouldn't:

^([a-zA-Z0-9_'+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$

More details are here.

DDoSAttack's picture

He has: 38 posts

Joined: Oct 2006

I haven't tried your regex but I know this is the most perfect email validation script that I have ever used...

/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i'

They have: 222 posts

Joined: Sep 1999

I tried to add that to my tester but couldn't get it working. I kept getting this error from PHP:

Warning: eregi(): REG_BADRPT

Is that meant to be used in JavaScript or something? Can you modify it to make it work in PHP?

DDoSAttack's picture

He has: 38 posts

Joined: Oct 2006

I use preg_match instead of eregi. So my validation script looks like...

<?php
preg_match
('/^[^\x00-\x20()<>@,;:\\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\\".[\]\x7f-\xff]+)+$/i', $email)
?>

also believe it or not some of those email addys that you tested and said are supposed to be invalid are actually valid according to standard

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Best email validation is simply to send an email to the addy with a confirmation link. Shazam!

Laughing out loud

DDoSAttack's picture

He has: 38 posts

Joined: Oct 2006

Mark Hensler;209423 wrote: Best email validation is simply to send an email to the addy with a confirmation link. Shazam!

Laughing out loud

That is not always what you would be using this for...

I use it more for making sure that a user hasn't mistyped an email address. Wink

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

DDoSAttack;209456 wrote: That is not always what you would be using this for...

I use it more for making sure that a user hasn't mistyped an email address. Wink

Just because the email is valid syntax-wise doesn't mean it isn't mistyped. "[email protected]" is valid, but it isn't mine.... That's why I prefer to use email confirmation.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 222 posts

Joined: Sep 1999

DDoS, I added your regex to the test and it didn't do as well as the one I listed. You can check out the detailed results on the page (yours is at the bottom).

Which addresses that I have marked as invalid do you think are valid?

They have: 222 posts

Joined: Sep 1999

Ofcourse the regex doesn't confirm that the address actually exists, but it does help prevent people from making mistakes or purposefully entering a invalid email. It's not 100%, but very few things are. Used in addition to sending a confirmation e-mail it is usefull.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.