Is this email validation funcation correct?

Hai folks,

function validateEmail($email){
    $pattern = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$";
    if (preg_match($pattern, $email)==0) {
       echo "Email id not valid.";
       return false;
    }
}


There are actually a bunch of symbols which are valid in email address that you didn’t account for:
! # $ % & ’ * + - / = ? ^ _ ` { | } ~

Also, with .'s, they can’t be the first or last character and there can’t be more than one in a row (so no bob..smith@email.com).

Thanks for the feedback, let me find a more suitable one then in google :slight_smile:

Here is a script that works well. The email address in this case is in a form called ProductOrder.


// check email address is valid 
          var checkEmail=document.ProductOrder.emailAddress.value;
      // check if entry
      // removes spaces if any
          checkEmail=checkEmail.replace(/ /gi,"")                              
          document.ProductOrder.emailAddress.value=checkEmail;                       // return corrected value to field 
     // check format of email address
          var ok="-a-z0-9!#$%&'*/=?+^_'{|}~";
          var re=new RegExp("^(\\"?)(?:["+ok+"][.]?)*(?:["+ok+"])+\\\\1@(?:(?:[-a-z0-9])+[.])+(?:[a-z]{2}|[a-z]{3}(?:[.][a-z]{2})?)$","i"); 
     //
          var resultObj=re.exec(checkEmail);
          if( !resultObj || !resultObj[0])              // no match found  
               { alert("Please enter a valid email address before re-submitting")
                  return false                          
               }
 // -----