Solved

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

Posted on 2006-07-24
7
222 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 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