简体   繁体   中英

filter_var : validate array of email addresses

I have a dropdown list where when the user choose All, it will sent email to all recipients that I get from the database. If the user choose 'Other', a textfield will appear which the user have to enter the email address. I would like to validate email that the user entered in the 'Other' textfield is valid. I want to allow user to be able type multiple email addresses, which separated by commas.

This is my form:

 <tr> <td>To</td> <td>:</td> <td> <select name="email" id="email" onchange="if(this.value == 'Other') {this.form['Other'].style.visibility='visible'}else{this.form['Other'].style.visibility='hidden'};"> <option value=""></option> <option value="All">All</option> <option value="Other">Other</option> </select> <?php if(isset($error[ 'email'])) echo $error[ 'email']; ?> <input type="text" id="Other" name="Other" style="visibility:hidden;" size="46" placeholder="Enter e-mail separated with commas" /> <?php if(isset($error[ 'Other'])) echo $error[ 'Other']; ?> </td> </tr>

This is my validation for email:

 if($_POST['Other'] != "" && (!filter_var($_POST['Other'], FILTER_VALIDATE_EMAIL))) { $error['Other'] = "<div class=error>Email entered is not valid.</div>"; }

I use FILTER_VALIDATE_EMAIL to validate email addresses. My problem is, If entered one email address, there is no problem, the message will be sent and I received it. But if i entered more than one email, the validation error message will prompt me that the email address I entered is not valid. Can I know why it only works for one email but not for multiple email addresses?

Something like this you need to do,

$emails = 'bill@microsoft.com, test.com, obama@usa.gov';
$emails = explode(', ', $email);
$filterd_emails = array();
foreach($emails as $email){
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $error['Other'][] = "<div class=error>$email is not valid Email.</div>";
    }else{
        $filtere_emails[] = $email
    }
}

EDIT

Now you've all validated emails stored in $filtered_emails . Here are two different scenarios.

1 ) You want to send emails to all filtered_emails (that pass the criteria), no matter how many pass the criteria.

//$emails = 'bill@microsoft.com, test.com, obama@usa.gov';
$filtered_emails = array('bill@microsoft.com', 'obama@usa.gov');

for that you can use following approach, it'll skip test.com and will send email to other two

if(count($filtered_emails) > 0)
foreach($filtered_emails as $filtered_email){
    //send email here. email($filtered_email);
}

2 ) you don't want to send email any person if any of input emails failed to pass validation.

eg test.com has failed to pass the criteria, and you don't want to send email at all(to other two bill@microsoft.com and obama@usa.gov )

in this case,

if(count($error['other'])>0){
    //display error and exit
}

it's now totally up to you which technique you want to use.

The string of emails is converted into an array with the PHP explode function

Then we instantiate a PHP array function and set it to a variable as an empty array

Then we iterate through the array with a PHP foreach loop function

Then we check each email's validity PHP filter_var and FILTER_VALIDATE_EMAIL functions filter_var(!filter_var(bill@microsoft.com, FILTER_VALIDATE_EMAIL))

<?php
  $emails = 'bill@microsoft.com, test.com, obama@usa.gov';
  $emails = explode(', ', $email);
  $filterd_emails = array();
  foreach($emails as $email){
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
       $error['Other'][] = "<div class=error>$email is not valid Email.</div>";
    }else{
       $filtered_emails[] = $email
    }
  }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM