You are here: HomeForums » PHP » Regex » Very simple function to validate email address in PHP

Very simple function to validate email address in PHP (2 posts)

in Forums » PHP » Regex

thdadmin (administrator)

The function uses regular expressions to validate the format of an email address. If you have questions and suggestions please let me know!

function isValidEmail($email)
{
	if(preg_match("/[.+a-zA-Z0-9_-]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email) == 0)
		return false;
	else
		return true;
}
Posted 1 month ago #

no1skid (Member)

Very basic email validation. I have a function that also uses regex, but also the getmxrr function that searches dns for MX records corresponding the hostname.

function check_email($email)
{

// Create the syntactical validation regular expression
$match = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-0-9-]+)*(\.[a-z]{2,4})$";

// Presume that the email is invalid
$valid = 0;

// Validate the syntax
if (eregi($match, $email))
{
list($username,$domain) = split("@",$email);

// Validate the domain
if (getmxrr($domain,$mxrecords))
$valid = 1;
}
else
{
$valid = 0;
}
return $valid;
}
?>
Posted 1 month ago #

Reply

You must log in to post.