Reputation: 213
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:
[email protected], [email protected], 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?
Upvotes: 1
Views: 304
Reputation: 8528
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++;
}
}
Upvotes: 2
Reputation: 699
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++;
}
Upvotes: 1
Reputation: 33502
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.
Upvotes: 1