Validate Email address with PHP

I have my website completely made in PHP. But I m stuck in the Email address validation. How can i do that with PHP

By using regular expressions.

Take your pick of email expressions from regexlib.com

http://www.php.net/manual/en/filter.filters.validate.php

Don’t try to re-invent the wheel.

Just don’t use Swift Mailer’s embarrassing monstrosity:


[FONT=couriernew]/^(?:(?:(?:(?:(?:(?:(?:[  \	]*(?:\\r\
))?[ \	])?(\\((?:(?:(?:[ \	]*(?:\\r\
))?[  \	])|(?:(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x19\\x7F]|[\\x21-\\x27\\x2A-\\x5B\\x5D-\\x7E])|(?:\\\\[\\x00-\\x08\\x0B\\x0C\\x0E-\\x7F])|(?1)))*(?:(?:[  \	]*(?:\\r\
))?[ \	])?\\)))*(?:(?:(?:(?:[ \	]*(?:\\r\
))?[  \	])?(\\((?:(?:(?:[ \	]*(?:\\r\
))?[  \	])|(?:(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x19\\x7F]|[\\x21-\\x27\\x2A-\\x5B\\x5D-\\x7E])|(?:\\\\[\\x00-\\x08\\x0B\\x0C\\x0E-\\x7F])|(?1)))*(?:(?:[  \	]*(?:\\r\
))?[ \	])?\\)))|(?:(?:[ \	]*(?:\\r\
))?[  \	])))?(?:[a-zA-Z0-9!#\\$%&'\\*\\+\\-\\/=\\?\\^_`\\{\\}\\|~]+(\\.[a-zA-Z0-9!#\\$%&'\\*\\+\\-\\/=\\?\\^_`\\{\\}\\|~]+)*)+(?:(?:(?:(?:[  \	]*(?:\\r\
))?[ \	])?(\\((?:(?:(?:[ \	]*(?:\\r\
))?[  \	])|(?:(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x19\\x7F]|[\\x21-\\x27\\x2A-\\x5B\\x5D-\\x7E])|(?:\\\\[\\x00-\\x08\\x0B\\x0C\\x0E-\\x7F])|(?1)))*(?:(?:[  \	]*(?:\\r\
))?[ \	])?\\)))*(?:(?:(?:(?:[ \	]*(?:\\r\
))?[  \	])?(\\((?:(?:(?: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?:(?:[ \	]*(?:\\r\
))?[ \	])?(\\((?:(?:(?:[  \	]*(?:\\r\
))?[  \	])|(?:(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x19\\x7F]|[\\x21-\\x27\\x2A-\\x5B\\x5D-\\x7E])|(?:\\\\[\\x00-\\x08\\x0B\\x0C\\x0E-\\x7F])|(?1)))*(?:(?:[  \	]*(?:\\r\
))?[ \	])?\\)))*(?:(?:(?:(?:[ \	]*(?:\\r\
))?[  \	])?(\\((?:(?:(?:[ \	]*(?:\\r\
))?[  \	])|(?:(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x19\\x7F]|[\\x21-\\x27\\x2A-\\x5B\\x5D-\\x7E])|(?:\\\\[\\x00-\\x08\\x0B\\x0C\\x0E-\\x7F])|(?1)))*(?:(?:[  \	]*(?:\\r\
))?[ \	])?\\)))|(?:(?:[ \	]*(?:\\r\
))?[ \	])))?)))$/D[/FONT]

Absurd

I have this one that is being used for several projects.


function checkEmail($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;
	}
	# Split it into sections to make life easier
	$email_array = explode("@", $email);
	$local_array = explode(".", $email_array[0]);
	for($i = 0; $i < sizeof($local_array); $i++){
		if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\\.-]{0,63})|(\\"[^(\\\\|\\")]{0,62}\\"))$", $local_array[$i])){
			return false;
		}
	}  
	if(!ereg("^\\[?[0-9\\.]+\\]?$", $email_array[1])){ # Check if domain is IP. If not, it should be valid domain name
		$domain_array = explode(".", $email_array[1]);
		if(sizeof($domain_array) < 2){
			return false; # Not enough parts to domain
		}
		for($i = 0; $i < sizeof($domain_array); $i++){
			if(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])){
				return false;
			}
		}
	}
	return true;
}

Here is mine:


    /**
     * Check if an email address is in a standard format.
     *
     * @param   string      $value
     * @return  false|string
     */
    public static function isEmail($value)
    {
        // Tidy up first.
        $value = strtolower($value);
        $value = str_replace('www.', '', $value);

        // The part before the '@' will be called the 'account'.
        // The part after the '@' will be called the 'host'.

        $accountChars = preg_quote('a-z0-9-_\\.+', '/');
        $hostChars =    preg_quote('a-z0-9-', '/');     // dot excluded dileberatly.

        $match = preg_match("
              /^
                [{$accountChars}]+                      # account
                @
                [{$hostChars}]+(\\.[{$hostChars}]+)*     # host
              $/ix",
            $value);

        // If the match is fine, return the clean email address; else false.
        return (($match === 1) ? $value : false);
    }

You can also use regex…

Really simple: http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm

Maybe, but RFC822 compliant. Unlike other solutions offered in this thread, choose your hammer wisely.

This.
I’m in favor of the php filter solution proposed or going with this : http://www.linuxjournal.com/article/9585?page=0,3 (read the first page to understand what it does).

Gotta love the

<noscript>
            <meta http-equiv="refresh" content="0; url=http://www.sadtrombone.com/" />
        </noscript>

on the blog linked.

Not sure if you are being sarcastic, but thanks anyway :smiley: