faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

16 of 19 people (84%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

What is the best way to verify that an email address is valid in php?

Oct 9th, 2001 11:52
mike gifford, http://www.phpsquare.com/tips/003.html


There are lots of options & different flavours of this.  
I've tried the following:
<?
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$",
$Email)) {
$error = 1;
$error_html .= "This email address is invalid: " . $Email;
echo  $error_html;
}
?>
But this one looks quite impressive too:
<?
           function checkaddress($mail) {
               if ( !ereg( "^([0-9,a-z,A-Z]+) ([.,_]
([0-9,a-z,A-Z]+))*[@] ([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]
           +))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$", $mail ) ) {
                   return false;
               }
               else
               {
                   return true;
               }
           } 
?>
More recently I discovered the following :
http://phpclasses.upperdesign.com/browse.html/package/13
which features:
- Simple validation just by looking at the e-mail address string.
- Validation of a e-mail address host as a valid mail exchange domain.
- Validation of a e-mail address by connecting to the mail host server
to determine if there is really a deliverable mail box.
Mike