Regular Expression: comma-delimited email addresses

Hi!
I have been using this RegEx for email address validation and it is working very well:
^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$

Now, I would like to validate comma-delimited input of multiple email addresses into a textbox in order to throw an error message when there is
- a wrong email address in there
or
- a delimiter missing
or
- a wrong delimiter being used.

It should accept anything that has strings matching the RegEx above and delimited by spaces (doesn't matter how many, could be none) and ONE comma. Any ideas? Thanks!
LVL 1
ddeteringAsked:
Who is Participating?
 
gnudiffCommented:
Possibly.
Theoretically you could do something like:

((your 1 email regexp)(\s*,\s*))+

However:
1) I am not sure how well ASP.net or client-side JS handle recursive pattern matches;
2) This by itself still does not give answer to what exactly was wrong (wrong delim/missing delim).
0
 
gnudiffCommented:
Well, the solution would depend on the programming language you use, which you haven't indicated. ;)

Still, it would be easy for program to detect a wrong email address, but much harder to see if there is a delimiter missing or a "wrong" delimiter is being used - that would require some analysis and multiple regexp checks - do you really need that?

The program in PHP would look something like this:

Assuming $input variable contains the textbox entry;

$single_email_regexp = '....';// your email regexp, not included for brevity's sake

$email_list = preg_split(("/[\s,]+/",$input);
$wrong_emails = array();
$good_emails = array();

foreach ($email_list as $email)
      if ( preg_match($single_email_regexp, $email) != 1)
                 $wrong_emails[]=$email;
       else
              $good_emails[]=$email;

if (count($wrong_emails) > 0)
{
    echo "Wrong emails encountered: ".join(" ",$wrong_emails)."\n";
}
else
    // do something with $good_emails
0
 
ddeteringAuthor Commented:
Hi gnudiff,

Thank you for your reply! Well, I am using VB in ASP.net and your approach would work there just fine. I thought I could do the entire job with a properly defined RegEx so that it would run in the JavaScript validator, too... I imagine some sort of grouping where my RegEx becomes just one element in another RegEx that allows only MyRegEx separated by exactly one comma and any number of spaces (or none). Wouldn't that work?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
gnudiffCommented:
Note also that it might be possible to optimize your Regexp perhaps:

^(([[:alnum:]]+[_\-\.\+]+))*[[:alnum:]]+@(\w+[\.\-]+)*\w{1,63}\.[a-zA-Z]{2,6}$

I haven't tested, but I think this should do the same yours does (the [:alnum:] is a shortcut for A-Za-z0-9, should work with PCRE - Perl compatible regexps).

Also, does your regexp handle things with domain count greater than 3, for example:
email@lab.dept.division.agency.gov ?
0
 
ddeteringAuthor Commented:
gnudiff, thanks a lot for your comments. Yes, the regex does handle extra long domain names, and I will also try your optimization. I will, of course, try your suggested regex for recursive pattern matching and will let you know shortly whether it works. I don't have a problem with an unspecific error message, you can't expect everything ;)
0
 
ddeteringAuthor Commented:
Unfortunately,
((^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$)(\s*,\s*))+
doesn't let anything through - at least not with JavaScript from ASP.net. Maybe the ^ and $ indicators are irritating? I couldn't submit anything past it.

Another question: I assume this one would, if it worked, require a comma at the end of the string. Any way around it?

It has gotten more complicated than I thought and I will increase the points.
0
 
ddeteringAuthor Commented:
I got this one to work for five digit postal codes:
(((D-)?\d{5})(\s*,*\s*))+
It does not require a comma at the end anymore but lets through a string like this as well:
12345 12345

This one: (((D-)?\d{5})(\s*,\s*))+
allows only a string like this 12345, 12345,
So I would have to make my users add a comma at the end of their string.

Hence, we now know that recursive matching is not a problem. Two problems remain:
1) My RegEx for an email address has to be integrated into gnudiff's form to work as well as the zip code example above.
2) Gnudiff's recursive pattern matching form has to be optimized to allow a string that does not end with a comma but requires commas as a delimiter.

Maybe after Labor Day...
0
 
ozoCommented:
There are many valid email addresses that do not match
(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}
0
 
ddeteringAuthor Commented:
even though it is off-topic - for example? I have just replaced another one with this because it caused problems... Any ideas on my question?
0
 
ddeteringAuthor Commented:
This here seems to work well:
(((([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})(\s*,\s*))+(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$

As you can see, I have removed the start character ^ from my email expression and simply added the email expression again after the recursive expression indicating with $ that this should be the end of the expression. It was easier than expected after gnudiff's help.
0
 
gnudiffCommented:
>> 2) Gnudiff's recursive pattern matching form has to be optimized to allow a string that does not end with a comma but requires commas as a delimiter.

>> (your 1 email regexp)(\s*,\s*))+

I believe you could possibly simply add the end of line match:

(your 1 email regexp)(\s*,\s*|$))+

0
 
etsellincCommented:
(((([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})(\s*,\s*))+(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$

This works when I enter 2 emails addresses, but with just one, it does not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.