Jump to content

Multiple Email Validation


phpretard

Recommended Posts

I am trying to validate multiple emails on one submit.

 

The rules are

1. to validate at least the format

2. Then at least 5 email must be submitted

3. If there is an error the input with the error should be indicated.  Also not deleting any emails that were correct.

 

I know many ways to display the errors I don't exactly what to do with the array values in terms of validating and re-inserting them into the appropriate input boxes.

 




if (isset($_POST['submit'])){

$Emails=$_POST['InviteEmail'];

//I know there will probably need to be a "foreach" in here somewhere.

if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $Emails)) {
$error="X number of emails are incorrect";

}

} //END POST



<form action="" method="post">

<input style="width:200px;" type="text" name="InviteEmail[]" value=""> Email 1
<input style="width:200px;" type="text" name="InviteEmail[]" value=""> Email 2
<input style="width:200px;" type="text" name="InviteEmail[]" value=""> Email 3
<input style="width:200px;" type="text" name="InviteEmail[]" value=""> Email 4
<input style="width:200px;" type="text" name="InviteEmail[]" value=""> Email 5
<input style="width:200px;" type="text" name="InviteEmail[]" value=""> Email 6

<br />

<input type="submit" name="submit" value="Send Emails">

</form>


 

Any pointers would be appriciated.  Thanks

Link to comment
Share on other sites

I'd set up an array containing 5 elements - 1 for each email.

$arrEmails=array(false,false,false,false,false);

 

Then set up a loop to run through the emails and replace a "false" with a "true" if it matches OK.

for ($i=0;$i<5;++$i) {
  if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$",$Emails[$i])) {
    $arrEmails[$i]=true;
  }
}

 

Now we should (I say should - this isn't tested) have an array with 5 TRUEs if all were OK.

 

To check...

foreach ($arrEmails as $key => $val) {
  if ($val==false) {
    echo 'Email '.$key.' is malformed.<br />';
  }
}

 

How's that? Off the top of my head and untested but I hope it gives you some ideas :)

 

(This is presuming all 5 emails are in the array $Emails and you're not checking one at a time.)

 

What you can do at the start of the script place all emails into an array called $Emails:

$Emails[0]=$_POST['email1'];
$Emails[1]=$_POST['email2'];
$Emails[2]=$_POST['email3'];
$Emails[3]=$_POST['email4'];
$Emails[4]=$_POST['email5'];

Link to comment
Share on other sites

That is good stuff, I am not really sure where to put it though.

 

I should have posted the way the email inputs are "built":

 

 


<?php

if (!isset($_POST['AddEmailBoxes']))    {$_SESSION['Totalboxes']="5";}

elseif (isset($_POST['AddEmailBoxes'])){$_SESSION['Totalboxes']=$_POST['AddEmail'] + 1;}

if (isset($_POST['DeleteEmailBoxes']))  {$_SESSION['Totalboxes']=$_POST['AddEmail'] - 1;}

$TotalBoxes=$_SESSION['Totalboxes'];

for ($i=1; $i < $TotalBoxes; $i++)
{

$EmailText="
<input style=\"width:200px;\" type=\"text\" name=\"InviteEmail[]\" value=\"\"> Email $i
";

echo $EmailText;

}


?>

 

Does this make a difference?

Link to comment
Share on other sites

I must admit you've lost me a bit now.

 

I can write code OK but I'm terrible at playing someone else's code in a few situations and this is one of them.

 

The best thing I can say (and this applies to EVERY single script) is think of it from the start and imagine you're the CPU running through it - what order would you need to place things so they can be accessed later on?

 

It's the flow of logic.

 

Basically you want to grab the email addresses at the start of the script and place them into an array like I did at the end of my post.

 

When displaying the HTML to build the text boxes...

for ($i=1; $i < $TotalBoxes; $i++) {

$EmailText='
<input style="width:200px;" type="text" name="email'.$i.'" value=""> Email $i';

echo $EmailText;

}

Link to comment
Share on other sites

The rules are

1. to validate at least the format

2. Then at least 5 email must be submitted

If it should be a minimal of 5 emails you need to be inserted I would validate

1. If the email is valid.

2. If the email field is not empty

 

3. If there is an error the input with the error should be indicated.  Also not deleting any emails that were correct.

 

I know many ways to display the errors I don't exactly what to do with the array values in terms of validating and re-inserting them into the appropriate input boxes.

 

 

you can resest the values in the input fields like so

<input style="width:200px;" type="text" name="InviteEmail[]" value="<?php echo(isset($_POST['InviteEmail'][0]))?$_POST['InviteEmail'][0]:"";?> Email 1

Link to comment
Share on other sites

The simple work behind the code:

 

      1. There is a sesion set that determines the number of inputs

      2. There are three submit button in the form

                a. Submit 1 adds a number to the session thus adding an input

                b. Submit 2 subtracts a number from the session thus removing an input

                c. Submit 3 submits all the inputs

 

      3. When the form is submitted the post value is however many emails were added to the form.

          I am not sure what to do with them at this point.

 

My Biggest Questions I suppose is how do I capture email$i in a post?  The $i variable could be any number of values.  So $email1=$_POST['email1']; (how I usually do it) won't work becuase there is no set number.

 

Can I use the SESSSION to build the $email$i=$_POST['email$i']?

 

Step 1 and 2(a-c) works great but it may not be the best approach.

 

Please don't jet on me yet...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.