Simple
$email = '[email protected]';
$validation = filter_var($email, FILTER_VALIDATE_EMAIL);
if ( $validation ) $output = 'proper email address';
else $output = 'wrong email address';
echo $output;
Advanced
This function doesn’t only check if the format of the given email address is correct, it also performs a test if the host is existing.
<?php
$email="[email protected]";
if (isValidEmail($email))
{
echo "Hooray! Adress is correct.";
}
else
{
echo "Sorry! No way.";
}
//Check-Function
function isValidEmail($email)
{
//Perform a basic syntax-Check
//If this check fails, there's no need to continue
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
return false;
}
//extract host
list($user, $host) = explode("@", $email);
//check, if host is accessible
if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
{
return false;
}
return true;
}
?>
Regular Expression Test
function checkEmail($email) {
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$email))
{
return true;
}
return false;
}
The above, with domain name validation:
function checkEmail($email) {
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$email))
{
list($username,$domain)=split('@',$email);
if(!checkdnsrr($domain,'MX')) {
return false;
}
return true;
}
return false;
}
do u have this installed here? can i post with an invalid email address? let’s check
lolz.. Hey Adrian what was the result let us know?
This checks syntax and MX, but what if I type [email protected]? It will validate. Check out amplemedia.com. They validate emails with a superior algorithm that can tell [email protected] is a bogus email. ;)
yeah, but what if [email protected] is someone’s actual email?
How i can allow into my site register only with for example 10 emails and thats it..what i need to use to check emails is that is one from 10 currect??? Thanks!
Oh i found that way! :) i dont know is that secure and good but have a look
Hi,
I tested the advanced email adress validation snippet.
Did i make a mistake, or is it faulty?
I tried it with “[email protected]”, a little alteration of my original domain.
“Soltecks.de” isn’t neither in use nor registered, see “Denic.de” for further information, or just verify.
The snippet says “Hooray! Adress is correct.” And that’s not true.
By the way – I appreciate yout site. I obtained a lot of useful information.
Regards
Swirl
City: Dortmund / State: Germany
From Drupal 6 + some fixes
Is there an updated version?
Of course, provided code doesn’t fulfill standards for email address format. What about e.g. [email protected]?