Reputation: 27
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?
Upvotes: 0
Views: 5073
Reputation: 9
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([email protected], FILTER_VALIDATE_EMAIL))
<?php
$emails = '[email protected], test.com, [email protected]';
$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
}
}
?>
Upvotes: 0
Reputation: 4425
Something like this you need to do,
$emails = '[email protected], test.com, [email protected]';
$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 = '[email protected], test.com, [email protected]';
$filtered_emails = array('[email protected]', '[email protected]');
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.
e.g. test.com
has failed to pass the criteria, and you don't want to send email at all(to other two [email protected]
and [email protected]
)
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.
Upvotes: 1