verN Posted April 19, 2007 Share Posted April 19, 2007 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! Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 19, 2007 Share Posted April 19, 2007 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 } Quote Link to comment Share on other sites More sharing options...
verN Posted April 19, 2007 Author Share Posted April 19, 2007 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"; } } Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 19, 2007 Share Posted April 19, 2007 $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 ?> Quote Link to comment Share on other sites More sharing options...
verN Posted April 19, 2007 Author Share Posted April 19, 2007 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! Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 19, 2007 Share Posted April 19, 2007 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 ?> Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
verN Posted April 19, 2007 Author Share Posted April 19, 2007 hi, thanks for your help i got it working now! Quote Link to comment Share on other sites More sharing options...
gkhelloworld Posted April 23, 2007 Share Posted April 23, 2007 My EREGI eregi("^([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)$", $reg_email) chek mail Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.