Code / PHP Snippets / Function: validate_email()

  1. <?
  2. function validate_email($email) {
  3.    // Create the syntactical validation regular expression
  4.    $regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$";
  5.    
  6.    // Presume that the email is invalid
  7.    $valid = 0;
  8.    
  9.    // Validate the syntax
  10.    if (eregi($regexp, $email)) {
  11.       list($username,$domaintld) = split("@",$email);
  12.       if (getmxrr($domaintld,$mxrecords))
  13.          $valid = 1;
  14.    } else {
  15.       $valid = 0;
  16.    }
  17.    return $valid;
  18. }
  19. ?>

Download as Text : January 6th, 2009