Jump to content

[SOLVED] email address reg


verN

Recommended Posts

hi, i have the following regular expression working for a single email address however when sending emails the to field in the form can take two or more email address which are sepearted by ,(comma) but the regular expression does't work for this. hERE IT IS:

 

if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['to'])))) {

}

 

THANKS, i have chnaged it to inlcude a comma but it still doesn't work. HELP!

Link to comment
Share on other sites

if you change the regex to include a comma then it will allow commas in any email address can you not -

 

$string = "form field as a string including all email addresses and commas";
$pieces = explode("," , $string); //$pieces becomes an array each holding the email addresses that were comma deliminated
for($i = 0; $i < count($pieces); $i++)
{
//regex each email address here and do what you need after that
}

 

Link to comment
Share on other sites

i tried doing this but still a invalid email address

 

$to = trim($_POST['to']);

        $pieces = explode("," , $string); //$pieces becomes an array each holding the email addresses that were comma deliminated

        for($i = 0; $i < count($pieces); $i++)

        {

        // checks for a valid to address

            if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['to'])))) {

          //stores the email address

          $to= trim($_POST['to']);

        } else {

          $error[]= "Please enter a valid 'to' address! e.g. blog@ac.com";

        }

       

        }

Link to comment
Share on other sites

$pieces = explode("," , $string); //$pieces becomes an array each holding the email addresses that were comma deliminated

 

instead of $string in the above line you have to put whatever you have called the string in your code

 

try this

 

<?php
$to = trim($_POST['to']);//I take it this is the post with all the email addresses in??
$pieces = explode("," , $to); 
for($i = 0; $i < count($pieces); $i++)
{
$addy = $pieces[$i]; //takes the array email address with the key value of $i
// checks for a valid to address
if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', $addy) 
{
	//stores the email address
		$toarr[]= $addy; //places the address in another array called $toarray
} 
else 
{
	$error[]= "Please enter a valid 'to' address! e.g. blog@ac.com";
}
}
print_r($toarr);//prints out all the good email addresses
?>

Link to comment
Share on other sites

which $string is it the $to one. I tried this now and I get two invalid email addresses

 

$string = trim($_POST['to']);

        $pieces = explode("," , $string); //$pieces becomes an array each holding the email addresses that were comma deliminated

        for($i = 0; $i < count($pieces); $i++)

        {

        // checks for a valid to address

            if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['to'])))) {

          //stores the email address

          $to= trim($_POST['to']);

        } else {

          $error[]= "Please enter a valid 'to' address! e.g. blog@ac.com";

        }

       

        }

 

 

help!

Link to comment
Share on other sites

try this

<?php
$to = stripslashes(trim($_POST['to']));//I take it this is the post with all the email addresses in??
$pieces = explode("," , $to); 
for($i = 0; $i < count($pieces); $i++)
{
$addy = $pieces[$i]; //takes the array email address with the key value of $i
// checks for a valid to address
if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', $addy) 
{
	//stores the email address
		$toarr[]= $addy; //places the address in another array called $toarray
} 
else 
{
	$error[]= "Please enter a valid 'to' address! e.g. blog@ac.com";
}
}
print_r($toarr);//prints out all the good email addresses
?>

Link to comment
Share on other sites

Here are a couple options:

<?php
$string = "email1@mydomain.com, email2@mydomain.com, email3@mydomain.com";

// option #1 - require all email addressed to be valid
$emails = explode(',', $string);
$valid = true;
foreach ($emails as $e) {
  $e = trim($e);
  if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z|i', $e)) {
    // current address is not valid
    $valid = false;
    $error = "$e is not a valid email address";
  }
}

// option #2 - send to only valid addresses
preg_match_all('|\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b|i', $string, $match);
$to = implode(', ', $match[0]);
?>

 

Notice in the second example, you are simply matching all valid email addresses in the string and then putting them back together with the comma separator. This may be a better way to handle it. You could kick back a message something like: "Send to $to" to show the user that not all the entered addresses were contacted (ie, the invalid ones).

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.