Solved

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

Posted on 2006-07-24
7
218 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
Comment Utility
<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
Comment Utility
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
Comment Utility
$email_arr = explode(',', $_POST['emails']); // split different emails

and now - validate each from $email_arr[0] to $email_arr[count($email_arr)]
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:shamus650
Comment Utility
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
Comment Utility
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
Comment Utility
$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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now