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>";
    }  
}