E-Mail Address Validation -- "Yahoo! You're all clear k

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

E-Mail Address Validation -- "Yahoo! You're all clear k

Post by Brian »

For quite some time I have wanted to come up with a good regular expression for validating e-mail addresses. When I say for quite some time I mean since years ago--back when I wrote a lot of Perl, which I hardly do lately since I have been so into using PHP, SQL, and even a bit of C for doing dynamic web stuff. I vaguely recall coming up with a simple, mostly sufficient but rather loose pattern or two at some point for some project or another, but I never really got serious about making a more complex pattern for stricter matching until recently. After some research and some tinkering, I have come up with a regular expression that seems better than any other I have come across for e-mail address validation, including some I found on the Zend site. I do not want to sound cocky, but I am very pleased with my work! With this, I can validate e-mail addresses in many, many projects better than I have ever done before. I feel a bit like Luke Skywalker when Han Solo swoops in like the calvalry, takes out Darth Vader, and announces "Yahoo! You're all clear kid! Now let's blow this thing and go home!" except that I took out Darth Vader myself and I get to take out the Death Star too. Woo hoo! :)

I am tempted to paste my shiny new regular expression in right here for all to use, enjoy, and possibly improve upon, but I have made extensive notes about what everything actually does (so I will not forget then have to work it out later; documentation is your friend) and I am planning to work everything into a nice article. That way, I can explain what everything does and how to use it instead of just dropping down some code out of context. I am fairly busy looking for work and attending school right now, but I may be able to make the time to write the article this weekend.

If you would be interested in reading yet another article about e-mail address validation, please feel free to post a note here--including anything you would like to see that you think other articles have left out or not explained well enough.
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

Do you have a cold?

Post by Brian »

There is some okay stuff on that page, but some of it does not quite make sense either. I made my entire regular expression, though, so I understand what it does much better than if I had simply cut and pasted something from somewhere else. :)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Sure, I would love to read an article on that! Post it here, and heck, if you let me, I would love to post it over on NewbieNetwork. We could use a good RegEx tutorial.

You probably have it, but Mastering Regular Expressions is the best book out there on the topic of RegEx (probably the only one, the only one I have seen). So if you haven't seen it, check it out on Amazon, its a good book.
Craig
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 3:13 pm

Post by Craig »

Sharing is everything :) share with us! :D
User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

Post by hex »

Code: Select all

<?
function email_check($email) &#123;
   if (eregi("^&#1111;0-9a-z](&#1111;-_.]?&#1111;0-9a-z])*@&#1111;0-9a-z](&#1111;-.]?&#1111;0-9a-z])*.&#1111;a-wyz]&#1111;a-z](g|l|m|pa|t|u|v)?$", $email, $check)) &#123;
      if (checkdnsrr(substr(strstr($check&#1111;0], '@'), 1), "ANY")) &#123;
         return TRUE;
      &#125;
   &#125;
   return FALSE;
&#125;
?>
Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

Okay, fine! Here is mine! :)

Post by Brian »

Okay, fine! Here is my regular expression for matching e-mail addresses with preg_match()! :)

Code: Select all

/^&#1111;w-.]+@((&#1111;^W_]+-+)*&#1111;^W_]+.)*(&#1111;^W_]+-+)*&#1111;^W_]+.&#1111;A-z]&#123;2,6&#125;$/
There are actually other ways to write exactly the same thing, but I was trying to minimize the length of the expression.

I am still planning to write that article I mentioned. I am just a bit busy with my job search and my studies right now. I have been looking for a job every day and I have mid-term examinations on Saturday and Monday.
Post Reply