Switch to full style
:read: Start PHP with us. Includes topics to help you in php
Post a reply

validate e-mail in php

Sun Nov 30, 2008 7:30 pm

if you have a form for user registration and you want to check the user mail ,if it valid or not ,it is easy just use the following check .
Note : the check here is based one the "Regular Expression"
Code:

<?php
if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email))
{
   return
FALSE;
}

?>

or put it in php function
Code:
<?php
function checkmail($email){
    return
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}


Above , i didn't check the domain name ! . you just checked expression
Code:
[email protected]


so programmer make more checking , by checking on the domain name if it valid or not , below you can see an example for this .

Code:
<?php
list($mailusername, $Domainname) = split("@",$myemail);
if(
getmxrr($Domainname, $MXHost))
{
   return
TRUE;
}
else
{
   if(
fsockopen($Domainname, 25, $errno, $errstr, 30))
   {
      return
TRUE;
   }
   else
   {
      return
FALSE;
   }
}
?>

the function getmxrr(...) and fsockopen(...) are built in functions in php library .



Post a reply
  Related Posts  to : validate e-mail in php
 validate mail in jsp     -  
 validate username     -  
 validate domain name in php     -  
 validate email address in asp     -  
 validate international phone number value     -  
 validate age entered as selection box in javascript     -  
 send mail using php     -  
 send mail cc and bcc in php     -  
 how to send mail with Ant     -  
 How to send conformation e-mail through JSP     -  

Topic Tags

PHP Validation