1

I want to validate some email addresses. The input is given by users in a textarea and the addresses are seperated by comma, like this:

email1@domain1.com, email2@domain2.com, etc. etc.

This is the script i use to check the addresses:

    $emailadressen = explode(',', $_POST['uitnodigen']);

        $aantal = count($emailadressen);
        $i = 1;

     foreach($emailadressen as $emails)
       {    
             if(check_email_address($emails) == false)    {
             $Melding['omschrijving'] = '<div class="error">Error in address number '.$i.'</div>';
             $i++;
             }
       }

And this is the function i use:

   function check_email_address($email) {
    // First, we check that there's one @ symbol, and that the lengths are right
    if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
        // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
        return false;
    }
    // Split it into sections to make life easier
    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    for ($i = 0; $i < sizeof($local_array); $i++) {
        if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
            return false;
        }
    }
    if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
        $domain_array = explode(".", $email_array[1]);
        if (sizeof($domain_array) < 2) {
            return false; // Not enough parts to domain
        }
        for ($i = 0; $i < sizeof($domain_array); $i++) {
            if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
                return false;
            }
        }
    }

    return true;
}

When the array only contains one emailaddress there is no problem and everything is working fine.

But when I have 2 emailaddresses at least, I get the FALSE error everytime. (Even when the emailaddresses are valid)

What do i do wrong?

Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
Rick A.
  • 213
  • 2
  • 4
  • 11
  • 1
    You might want to look into the [php email filter](http://php.net/manual/en/filter.filters.validate.php). – Fluffeh Nov 01 '13 at 05:14
  • http://stackoverflow.com/questions/13719821/email-validation-using-regular-expression-in-php – Krish R Nov 01 '13 at 05:16

3 Answers3

2

I would have used

filter_var($email, FILTER_VALIDATE_EMAIL) 

And:

filter_var($email, FILTER_SANITIZE_EMAIL)

Just like

   foreach($emailadressen as $email)
   {    
   if(filter_var($email, FILTER_SANITIZE_EMAIL === FALSE) || 
      filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
    {
    $Melding['omschrijving'] = 
    '<div class="error">Error in address number '.$i.'</div>';
    $i++;
   }
   }
mamdouh alramadan
  • 8,349
  • 6
  • 36
  • 53
1

Using a filter, you should be able to simply use:

foreach($emailadressen as $emails)
{    
    if(!filter_var($emails, FILTER_VALIDATE_EMAIL))
    {
        $Melding['omschrijving'] = '<div class="error">Error in address number '.$i.'</div>';
        $i++;
    }
}

Without the need for any of that huge function.

This is assuming that by the time you run this foreach, the array from the post is containing the email only.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • for example: i put in those 2 addresses: patrickmyname@hotmail.com and patricknoname@hotmail.com Output: patrickmyname@hotmail.com is not valid Script: $emailadressen = explode(',', $_POST['uitnodigen']); $aantal = count($emailadressen); $i = 1; foreach($emailadressen as $emails) { if(!filter_var($emails, FILTER_VALIDATE_EMAIL)) { $Melding['emailwrong'] = '

    '.$emails.' is '.(filter_var($emails, FILTER_VALIDATE_EMAIL) ? '' : 'not').' valid

    '; $i++; } }
    – Rick A. Nov 01 '13 at 05:32
1

I think space is getting added in your email address try by using trim

    foreach($emailadressen as $emails) {
        if(check_email_address(trim($emails)) == false)    {
            echo '<div class="error">Error in address number '.$i.' ' . $emails. '</div>';
        }
        else {
            echo '<div class="noerror">No Error in address number '.$i. ' ' . $emails. '</div>';
        }
        $i++;
    }
Dhivya
  • 689
  • 8
  • 14