email address not validating

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

email address not validating

Post by Cirdan »

I have these two methods to assist in validating email addresses.

Code: Select all

     public static function validateEmail($email) {
          $errors = array();
 
          if(!empty($email)) {
               if(!System_Validator::isValidEmailAddress($email)) {
                   $errors[] = 'Invalid email address';
               }
           } else {
               $errors[] = 'You must provide an email address';
           }
 
           return $errors;
      }
 
    public static function isValidEmailAddress($email) {
        // Regex from regular-expressions.info
        $regex = '/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/';
        
        if(preg_match($regex, $email)) {
            return true;
        }
        
        return false;
        
    }
When I type in trooper@gmail.com as the email address, it tells me that the address is "invalid". I think it's safe to assume the regex is not the issue.
jmaker
Forum Newbie
Posts: 16
Joined: Tue May 21, 2002 11:13 pm

Re: email address not validating

Post by jmaker »

Here's something a little easier.

Code: Select all

 
$email = 'trooper@gmail.com';
if(filter_var($email, FILTER_VALIDATE_EMAIL))
    print "Email is valid";
else
    print "Email is not valid";
 
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: email address not validating

Post by Cirdan »

Okay, the problem was with the regular expression. I needed to include the lower case a-z
Post Reply