Solved

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

Posted on 2006-07-24
7
225 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

717 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