The best e-mail validating regular expression
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 posted this at 23:52 — 23rd October 2006.
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
'IanD posted this at 14:32 — 24th October 2006.
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?
Fighting for a Lost Cause.net
DDoSAttack posted this at 14:49 — 24th October 2006.
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 posted this at 19:09 — 24th October 2006.
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!
DDoSAttack posted this at 00:02 — 25th October 2006.
He has: 38 posts
Joined: Oct 2006
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.
Mark Hensler posted this at 07:02 — 2nd November 2006.
He has: 4,048 posts
Joined: Aug 2000
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.
IanD posted this at 14:03 — 25th October 2006.
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?
Fighting for a Lost Cause.net
IanD posted this at 14:49 — 2nd November 2006.
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.
Fighting for a Lost Cause.net
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.