Thursday, June 14, 2012

Email validation using PHP and REGULAR EXPRESSION

Form validation is a very important part of web programming. This is to avoid any mistakenly adding data in database. Validating emails can be done in many ways. But the most faster and efficient way to validate emails is using regular expression.

Here is the code of this validation in PHP.

if(isset($todo) and $todo=="test")
{  
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){  
      echo "<center>Invalid email</center>";  
    }else{  
     echo "<center>Valid Email</center>";
    }  
}   

No comments:

Post a Comment

Please feel free to post your comments. If anyone has a good article or good thing to share, just send me that with your name to asadmehmoodstar@gmail.com. and if anyone want so receive updates regarding my blog, he can subscribe to my weekly newsletter on "Subscribe to our mailing list" section.

Thanks