Solved

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

Posted on 2006-07-24
7
224 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 250 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 250 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 40
PHP function parameters defined 14 41
How to use session variables in php? 22 48
Converting PHP triple des decrypt to c# 9 70
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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