Pages

Subscribe

Thursday, March 29, 2007

Email address validation with PHP

Ok this can be a tricky one....
I'm still new to PHP but I tried a number of different links and google searches and I tried these following examples and they didn't work:


ONE I TRIED

$pattern = "/^w[wd]+(.[wd]+)*@w[wd]+(.[wd]+)*.[a-z]{2,7}$/i";
return preg_match($pattern,$email);


ANOTHER ONE I TRIED

if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email)) {
return false;
}
return true;


Obviously these are functions that I added to my own functions.php that I include in every PHP file I need them in - and the function would be called from said PHP file...... anyway I made sure I fixed all parentheses etc. on the stuff above but neither of them worked for me. They both said invalid email account.

Then I finally found one that works.... it's simple, I mean it just checks the format of the email address rather than opening a socket connection to the mail server to see if it exists. But it does the job for me

It's available at
http://www.ilovejackdaniels.com/php/email-address-validation/

No comments: