Code Snippet
Email Address Validation
Simple
$email = 'mail@example.com';
$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="test@geemail.com";
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 whatever@yahoo.com? It will validate. Check out amplemedia.com. They validate emails with a superior algorithm that can tell whatever@yahoo.com is a bogus email. ;)
yeah, but what if whatever@yahoo.com 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
function checkEmail($str) { //1name if(preg_match("/^[\.A-z0-9_\-\+]+[@][name_\-]+([.][co_\-]+)+([.][uk_\-]+)[A-z]{1,4}$/",$str)) { return true; } //2name else if (preg_match("/^[\.A-z0-9_\-\+]+[@][name_\-]+([.][com_\-]+)+[A-z]{1,4}$/",$str)) { return true; } //3name else if (preg_match("/^[\.A-z0-9_\-\+]+[@][name3_\-]+([.][org_\-]+)+[A-z]{1,4}$/",$str)) { return true; } return false; }