Welcome Guest, Not a member yet? Register   Sign In
The best way of verifying an email
#6

[eluser]Sean Murphy[/eluser]
If you're interested, here's the function I use to validate email addresses. I originally got it from http://www.ilovejackdaniels.com/, but I might have made adjustments over the years. It's one of the best I've found.

Code:
/**
* Valid Email
*
* @access    public
* @param    string
* @return    bool
*/    
function valid_email($str) {
    // Check for invalid characters
    if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $str))
        return false;

    // Check that there's one @ symbol, and that the lengths are right
    if (!preg_match('/^[^@]{1,64}@[^@]{1,255}$/', $str))
        return false;

    // Split it into sections to make life easier
    $email_array = explode('@', $str);

    // Check local part
    $local_array = explode('.', $email_array[0]);
    foreach ($local_array as $local_part) {
        if (!preg_match('/^(([A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-]+)|("[^"]+"))$/', $local_part))
            return false;
    }

    // Check domain part
    if (!preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/', $email_array[1]) ||
        !preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/', $email_array[1])) {
        // If not an IP address
        
        $domain_array = explode('.', $email_array[1]);
        if (sizeof($domain_array) < 2)
            return false; // Not enough parts to be a valid domain

        foreach ($domain_array as $domain_part) {
            if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $domain_part))
                return false;
        }
    }
    
    return true;
}


Messages In This Thread
The best way of verifying an email - by El Forum - 04-19-2008, 06:00 AM
The best way of verifying an email - by El Forum - 04-19-2008, 09:37 AM
The best way of verifying an email - by El Forum - 04-19-2008, 12:44 PM
The best way of verifying an email - by El Forum - 04-19-2008, 05:24 PM
The best way of verifying an email - by El Forum - 04-20-2008, 03:30 AM
The best way of verifying an email - by El Forum - 04-21-2008, 10:37 AM
The best way of verifying an email - by El Forum - 04-21-2008, 12:20 PM
The best way of verifying an email - by El Forum - 04-21-2008, 03:51 PM
The best way of verifying an email - by El Forum - 04-22-2008, 11:20 AM



Theme © iAndrew 2016 - Forum software by © MyBB