I got a Regex that validates my mail-addresses like this:
([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]
In an old regex book they stated that you cannot write a regex to match all valid email addresses (although you can come close).
Here is a website dealing with regex and email addresses.
I would recommend that you split the string at ;
and ,
boundaries and check each email address separately for being valid/invalid with your regex.
Here is a simple Program which does this for you without using the Regular Expression.Well Actually it does use a regular expression but we don't have to worry about it how it looks .php does this for us .
public function test_reg()
{
$email_list = '';
$array = explode(";",$email_list);
foreach ($array as $value)
{
$value;
if (!filter_var($value, FILTER_VALIDATE_EMAIL) === false)
{
$msg = "Email List Contains valid email addresses.";
}
else
{
$msg ="Your Email list contains an invalid email at.     ".$value;
break;
}
}
echo $msg;
}
You have to separate your emails by a semi colon.
This is a very old question, but I figured I'd share my C# code.
I decided to parse by the semicolon then check each email individually:
string toAddress = "testemail@gmail.com;test2@testing.com;";
Regex rgx = new Regex(
@"^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$");
List<string> emailArray = new List<string>();
if(toAddress != null && toAddress != "")
{
if (toAddress.IndexOf(";") != -1)
{
emailArray = toAddress.Replace(" ","").Split(';').ToList();
}
else
{
emailArray.Add(toAddress);
}
foreach (string email in emailArray)
{
if (rgx.IsMatch(email ?? ""))
{
SendEmail(email, subject, body);
}
}
}
Please try this
^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4};?)+$