irwan

PHP email validation

Nov 15th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php
  2.  
  3. function check_email($email) {  // First, we check that there's one @ symbol, and that the lengths are right
  4.         if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {  
  5.                 // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.  
  6.                 return false;
  7.         }  // Split it into sections to make life easier
  8.        
  9.         $email_array = explode("@", $email);
  10.         $local_array = explode(".", $email_array[0]);
  11.         for ($i = 0; $i < sizeof($local_array); $i++) {    
  12.                 if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {    
  13.                         return false;  
  14.                 }
  15.         }  
  16.         if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
  17.                 // Check if domain is IP. If not, it should be valid domain name  
  18.                 $domain_array = explode(".", $email_array[1]);  
  19.                 if (sizeof($domain_array) < 2) {      
  20.                         return false; // Not enough parts to domain  
  21.                 }  
  22.                 for ($i = 0; $i < sizeof($domain_array); $i++) {    
  23.                         if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {      
  24.                                 return false;    
  25.                         }  
  26.                 }
  27.         }
  28.         return true;
  29. }
  30.  
  31. $email = $_POST['email'];
  32. if(!check_email($email)){
  33.         echo "Email is not valid";
  34. }
  35.  
  36. ?>
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment