Grow your CSS skills. Land your dream job.

Last updated on:

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;
}

Comments

  1. Permalink to comment#

    do u have this installed here? can i post with an invalid email address? let’s check

  2. Flo
    Permalink to comment#

    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. ;)

  3. Gvidas
    Permalink to comment#

    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!

    • Gvidas
      Permalink to comment#

      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;
      }
      
      
  4. Swirl Locker
    Permalink to comment#

    Hi,
    I tested the advanced email adress validation snippet.
    Did i make a mistake, or is it faulty?
    I tried it with “test@soltecks.de”, 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

  5. Permalink to comment#

    From Drupal 6 + some fixes

    function valid_email_address($mail) {
      $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';  
      $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])(\.[a-zA-Z0-9]+)*)+';
    
      $ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
      $ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
    
      return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail);
    }
    

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".