code-tutorial.com

The best resources for developers

Please let user@example or user@localhost in email validation?

<?php 
function valid_email($email) { 
    // First, we check that there"s one @ symbol, and that the lengths are right 
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { 
     // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
     return false; 
    }

    // take a given email address and split it into the username and domain. 
    list($userName, $mailDomain) = split("@", $email); 
    if (checkdnsrr($mailDomain, "MX")) { 
        // this is a valid email domain! 
        return true;
    } 
    else { 
        // this email domain doesn"t exist!
        return false;
    }
} 
?>

Tags: php email web-applications

Source: By Matt Farina as answer to the question

This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 2.5


Related code-snippets: