Solved

Regular Expression: comma-delimited email addresses

Posted on 2004-09-02
12
1,902 Views
Last Modified: 2008-02-01
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!
0
Comment
Question by:ddetering
12 Comments
 
LVL 3

Expert Comment

by:gnudiff
ID: 11971204
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
 
LVL 1

Author Comment

by:ddetering
ID: 11973071
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
 
LVL 3

Accepted Solution

by:
gnudiff earned 250 total points
ID: 11975148
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
 
LVL 3

Expert Comment

by:gnudiff
ID: 11975264
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
 
LVL 1

Author Comment

by:ddetering
ID: 11975538
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
 
LVL 1

Author Comment

by:ddetering
ID: 11976036
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:ddetering
ID: 11984485
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
 
LVL 84

Expert Comment

by:ozo
ID: 11985400
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
 
LVL 1

Author Comment

by:ddetering
ID: 11985563
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
 
LVL 1

Author Comment

by:ddetering
ID: 11991144
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
 
LVL 3

Expert Comment

by:gnudiff
ID: 11995710
>> 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
 

Expert Comment

by:etsellinc
ID: 24214885
(((([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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

932 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

13 Experts available now in Live!

Get 1:1 Help Now