Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Form that allows multiple E-mails and validating it through php

Posted on 2006-07-24
7
Medium Priority
?
227 Views
Last Modified: 2012-05-05
Hello,

I have a form that allows the user to enter one e-mail then it validates it. All is working ok, but now I would like to allow the user to enter more than one e-mail in the form. Wondering if anyone has any tips to help me achive this process in php. I guess I can have user seperate e-mails by a comma, but how do I seperate them for verification? Here is part of my code below.

In the form page:

<form name="forward" method="post" action="email.php">
<input type="text" name="email">
</form>

In the email.php page:

$email = ($_POST['email']);

//Validation

if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,
3})$", $email)) {
                  echo "Invalid E-mail";
}
else .........

Any help would be appreciated. Thanks.

0
Comment
Question by:shamus650
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:maUru
ID: 17170183
<form name="forward" method="post" action="email.php">
<input type="text" name="email[]">
<input type="text" name="email[]">
<input type="text" name="email[]">
<input type="text" name="email[]">

</form>

In the email.php page:

$emails = ($_POST['email[]']);

//Validation

foreach($emails as $email) {
    if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,
       3})$", $email)) {
                     echo "Invalid E-mail";
     }
else .........
0
 
LVL 7

Expert Comment

by:maUru
ID: 17170224
here is the whole script so you dont mess up:

<form name="forward" method="post" action="email.php">
<input type="text" name="emails[]">
<input type="text" name="emails[]">
<input type="text" name="emails[]">
<input type="text" name="emails[]">
<input type="submit">
</form>


---email.php---
$emails = ($_POST['emails']);


foreach($emails as $email) {
    if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
                     echo "Invalid E-mail";
     }
    else {
      echo $email . ' is a valid email<br>';
    }
}
?>
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17170612
$email_arr = explode(',', $_POST['emails']); // split different emails

and now - validate each from $email_arr[0] to $email_arr[count($email_arr)]
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:shamus650
ID: 17179638
hmm, ok thanks for tha answer mauru, but that code has four input boxes. I would just like one input box for e-mail on my form with the ability to seperate e-mails with a space or a comma. I seem to have it to work and the explode argument helps me split the e-mails up into an array, but how do I validate each e-mail in the array. I see where you are heading Ivanov G, but not totally sure how to implement that.

 
0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 1000 total points
ID: 17179732
Something like this:

$email_arr = explode(',', $_POST['emails']); // split different emails

for ($i=0; $i<count($email_arr); $i++)
{
  $current = $email_arr[$i]; // get a email to validate
  if (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})", $current))
  {
    // invalid email, decide what to do
  }
}
0
 
LVL 7

Assisted Solution

by:maUru
maUru earned 1000 total points
ID: 17179898
$emails = explode(',', $_POST['emails']);

foreach($emails as $email) {
    if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
                     echo "Invalid E-mail";
     }
    else {
      echo $email . ' is a valid email<br>';
    }
}
0
 

Author Comment

by:shamus650
ID: 17180092
thanks guys. For some reason when I use the explode function to seperate the e-mails with a comma, it doesn't work. But if I seperate it with a space, it works just fine. weird huh?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question