Forum Moderators: coopster

Message Too Old, No Replies

Validating comma seperated email addresses.

Displaying invalide results from the resulting array

         

don_dr

8:38 am on May 6, 2009 (gmt 0)

10+ Year Member



I am trying to validated multiple email addresses separated by comma delimiter. The problem is, I don't just want to validate the emails, but would also like to tell the user which email is invalid amongst the lot.

Here is an example of what I have so far;

$contactlist = $_POST['friends']; //value from text field

$contactlist = str_replace("\r\n", "\n", $contactlist);
$csv_array= explode(",",$contactlist);
$csvnum=count($csv_array);

for ($n=0;$n<$csvnum;$n++)
{
trim($csv_array[$n]);
$emails = $csv_array[$n];
if( !eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $emails)){
$badMail = "You've got an invalid email somewhere";
}
//end mail check loop
}

Can anyone tell me how to tell which particular email is invalid?

Thanks

Frank_Rizzo

9:13 am on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the badmail will be

$emails

you just need to change

$badMail = "You've got an invalid email somewhere";

to

$badMail = "You've got an invalid email: $emails";

-------------------


$contactlist = "example1@example.com,example2@example.com,example3example.com,example4@example.com";

$contactlist = str_replace("\r\n", "\n", $contactlist);
$csv_array= explode(",",$contactlist);
$csvnum=count($csv_array);

for ($n=0;$n<$csvnum;$n++)
{
trim($csv_array[$n]);
$emails = $csv_array[$n];
if(!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $emails)){
$badMail = "You've got an invalid email: $emails";
echo $badMail . "\t";
}
//end mail check loop
}


-----------

outputs:

You've got an invalid email: example3example3.com

don_dr

9:47 am on May 6, 2009 (gmt 0)

10+ Year Member



Thanks for the reply, but apparently echo $badMail displays all email addresses in the array. That's the problem I've been having.

Frank_Rizzo

10:26 am on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you run the exact code I have in that example the output should be:

You've got an invalid email: example3example.com

If you run your code and you are seeing all emails then there must be a problem with the csv file (in my example I have hard coded the variable).

Try some debugging.

At the start of your code print out the csv variable to see if it is CSV:

******
$contactlist = $_POST['friends']; //value from text field

$contactlist = str_replace("\r\n", "\n", $contactlist);
$csv_array= explode(",",$contactlist);
$csvnum=count($csv_array);
******

below that add

echo $contactlist;
echo "\t" . $csvnum;

You should see all your email addresses comma separated and the total number of elements in the array.

If it doesn't look like csv and the csvnum is 1 then the problem is with the csv datafile.

[edited by: Frank_Rizzo at 10:26 am (utc) on May 6, 2009]

don_dr

11:00 am on May 6, 2009 (gmt 0)

10+ Year Member



Ok. I think I found the problem. I added some additional lines below the code, which I forgot to comment.

Thank a million. It's now working perfectly.

don_dr

11:31 am on May 6, 2009 (gmt 0)

10+ Year Member



I just noticed that when the array contains more than 1 invalid email, it still displays all the values in the array;

You've got an invalid email: example1example.com You've got an invalid email: example3example.com You've got an invalid email: example4@example.comexample1@example.com You've got an invalid email: example3example.com

Frank_Rizzo

11:50 am on May 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks like because you have no comma separator here:

example4@example.comexample1@example.com

If the csv file is not valid then it will be difficult for the script to determine the start and end of an email address.

How is the code entered? line by line?

example1@example.com,
example2@example.com,

or inline?

example1@example.com,example2@example.com,

You may need to parse the data in the first example to ensure that each line ends in a comma (I see you replace \r\n with \n) ...

Consider line by line without the commas and then replace them after they are posted

User posts:->

example1@example.com
example2@example.com

$contactlist = $_POST['friends']; //value from text field

$contactlist = str_replace("\r\n", ",", $contactlist);
$csv_array= explode(",",$contactlist);

(note that you may need to trim off the last ","

BTW I do not tend to use commas for csv data incase a user makes a typo by putting in a comma

e.g.

example1@example,com

The comma is next to the dot. Far better to use a character which needs shifting on the keyboard such as a broken pipe or tilde

example1@example.com¦example2@example.com

or

example1@example.com~example2@example.com

Using a different seperator will significantly reduce parsing errors.

don_dr

12:00 pm on May 6, 2009 (gmt 0)

10+ Year Member



To answer your initial question, the emails are entered inline.

I'll try what you suggested and see how it goes.

Thanks again.

don_dr

1:07 pm on May 6, 2009 (gmt 0)

10+ Year Member



It works when posted line by line. But is there anyway to check that the user actually posted line by line, so as to prompt them.

Thanks

don_dr

3:02 pm on May 6, 2009 (gmt 0)

10+ Year Member



Okay. I think I was a bit confused. I see that it works both ways for both inline and line by line.

Thanks.

don_dr

3:09 pm on May 6, 2009 (gmt 0)

10+ Year Member



Apparently not. It only appears that way. Back to my former question. Is there any way to prompt the user when email is entered inline?