0

I am working on sending email using smtp in mvc. I am able to validate single email address. But i want to validate multiple email addresses separated by semicolon (;). Here is regular expression which i am using

@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                            @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
adeel
  • 27
  • 6
  • possible duplicate of [How to validate multiple emails using Regex?](http://stackoverflow.com/questions/9780582/how-to-validate-multiple-emails-using-regex) – MeanGreen Aug 12 '15 at 13:31
  • I have already tried this.. but did not work for me – adeel Aug 12 '15 at 13:40

2 Answers2

0

Instead of creating an even more complex regex (a proper email validating regex is already far complex enough), why not just split the string and validate each address individually? For example:

bool isValid = true;
foreach (email in stringOfEmails.Split(';'))
{
    // validate the individual email address
    // if any fails, set `isValid` to `false` and break
}

You can wrap that up into a method on a utility class, or create an extension, if you prefer.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
0

Well i have done with multiple email validation. Email is separated by commaa(,). here is he regular expression which i have used. @"\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)([,]\s\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+))"

adeel
  • 27
  • 6