Resources
Home
Downloads
Mailing list
Search this site
Link to this site

TT Web Site Manager
Documentation
Download
Tutorials
FAQ

TT calendar library
About
Documentation
Download
Demo

Other code
poptasticDB
Simple XML parser function
Email address validation function

TT articles
Mending a dead iBook battery
Battery guide for small devices
Build your own TV modulator
PHP Nipkow disk generator
The Napier coffeemaker

One of the most frequent requirements for a general purpose piece of PHP code that I have encountered is that for an email address syntax validation function. I wrote the first version of the function below a couple of years ago, it is part of my standard toolkit of PHP code.

To use it, paste the code below into your PHP script and then call the validateEmail function passing it the email address you require to be checked. For example:

validateEmail("example@example.com");

This returns a text string depending on the composition of the address. If the address appears valid, this string has the value "This address may be valid.".

This function detects the following conditions, addresses less than 5 characters long, addresses with more or less than one @ character and those with the wrong number of dots or dots in the wrong places. It is still possible that invalid email addresses may evade its scrutiny but they should not be those with invalid syntax.

function validateEmail($email){
  #copyright (c)John W. List 2002-2003 http://www.technotoad.com
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
  $output = "This address may be valid."; 	
  $email = trim($email);
  #shortest valid email address is 5 characters
  If ((strlen($email)<5) or strstr($email," ")){
    $output = "Too short, " . strlen($email) . " characters.";
  }
  If (substr_count($email, "@")<>1){
    #If we find one and only one @, then the
    #email address is good to go.
    $output = "Either more than or less than one @ character.";
  }
  Else{ #it has an at,split into 2 halves on the @
    $halves=explode ("@", $email);
    # look for text on either side of the at
    # at least 1 character on the lhs and at least 3 on the RHS
    If (strlen($halves[0])<1 or strlen($halves[1])<=3){
      $output = "Not enough text characters in this email.";
    }
    #dot search
    #look at LHS
    #no dots on the ends of LHS string
    If (substr($halves[0],0,1)=="." or substr($halves[0],-1,1)=="."){
      $output = "There is a dot on the end of the LHS of this address.";
    }
    #look at RHS
    #no dots on the ends of RHS string
    If (substr($halves[1],0,1)=="." or substr($halves[1],-1,1)=="."){
      $output = "There is a dot on the end of the RHS of this address.";	
    }
    #check it contains a dot and no dotdots
    if (!strstr($halves[1],".") or strstr($halves[1],"..")){
      $output = "This address has the wrong number of dots in it.";
    }
  }
  return $output;
}

© copyright John W. List 1998 - 2007