email format

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

email format

Post by mickd »

hey, just making sure im not missing anything ;)

Code: Select all

function is_email($email) 
{
return preg_match('/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[.A-z]{2,4}$/', $email);
}
is that sufficiant for checking any email format? thanks.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: email format

Post by Roja »

mickd wrote:is that sufficiant for checking any email format? thanks.
Nope.

It misses numerous valid combinations. Keep in mind that the email RFC is considered one of the most challenging regex combinations in the world to comply with. It was covered in 'Mastering Regular Expressions', taking several pages to solve.

I'll save you time and trouble, and post a link to the function which contains the php version of that regex:

http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

8O thanks for the link, damn thats some pritty complex regex there. ill test it out.

EDIT:
File: [blacknova] / trunk / includes / validateemailformat.php (download) (as text)
Revision: 13, Fri Aug 19 15:24:02 2005 UTC (5 weeks, 5 days ago) by Roja
File size: 7126 byte(s)

nice.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

mickd wrote:8O thanks for the link, damn thats some pritty complex regex there. ill test it out.

Revision: 13, Fri Aug 19 15:24:02 2005 UTC (5 weeks, 5 days ago) by Roja

nice.
Thanks.

I can't/don't take all the credit. As I mention in the file itself, its part of the project (Blacknova Traders) I contribute to, and its based off of the incredibly well done book.

But yeah, I was the one that committed it to the project. :)

We're redoing the whole project in SVN, which is why only a handfull of files are there at the moment. Its a pretty exciting process. Thanks for noticing the commit.

I hope the function helps you out.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

all to validate an e-mail .. :: blink, bink:: that took some time.no doubt.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

If only an AGPL version were available...

Anyways, thought to mention the book itself is available through Google Print (http://print.google.com). Just look up the title "Mastering Regular Expressions". Yep, available as in free - just keep your Google/Gmail account details handy to sign in.

There's a huge selection of O'Reilly, SAMS, and other texts available also...Some are complete, some only have sections available - but worth a look.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Thanks :)

I use perl also, so I've been looking at Email::Valid and a few others...

There's also:
http://examples.oreilly.com/regex/email-opt.pl
straight from the horse's mouth...
User avatar
mr_griff
Forum Commoner
Posts: 64
Joined: Tue Sep 17, 2002 11:11 am
Location: Bozeman, Montana

Post by mr_griff »

Most account based systems that I setup use the e-mail address as their account login as well. So allowing all RFC-compliant addressed really isn't an option. For example, allowing login names with spaces, quotes, etc. don't usually work very well (even though they would be valid e-mail addresses).

With that in mind I usually go with the following regex:

Code: Select all

//Initalize Variables
$validationError = "NO";
$errorMessage    = "";

//Validate user input
if (!eregi("^([+_a-z0-9-]+)(\.[+_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $_POST['email']))
{
  $validationError = "YES";
  $errorMessage .= "<li>E-mail Address";
}
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Wondeful script Roja. I'm awful at regex so I never would've figured that out. I just used a simple regex to validate before, but now my mind can rest easily knowing my e-mail validation is more complete. How complete is it really? Does it fully comply with RFC 2822 (or whatever RFC it is)? Either way, it's more complete than what I've got. Thanks a lot!
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

php_wiz_kid wrote:How complete is it really?
Well, it is a php version of the definitive regex for email validation from the book 'Mastering Regular Expressions'. I haven't found any errata, comments, or test-cases that show it to be inaccurate. So far, it seems to match up.
php_wiz_kid wrote:Does it fully comply with RFC 2822 (or whatever RFC it is)?
Without rehashing the whole "which rfc" argument all over again, yes, it is (to my knowledge) fully compliant with the current relevant RFC's as of today. :)
Post Reply