0

I have string coming in this format as shown bellow:

"mark345345@test.com;rtereter@something.com;terst@gmail.com;fault@mail"

What would be the most efficient way to validate each of these above and fail if it is not valid e-mail?

sujith karivelil
  • 26,861
  • 6
  • 46
  • 76
bane 975
  • 87
  • 6
  • possible duplicate of http://stackoverflow.com/questions/9809357/regex-for-validating-multiple-e-mail-addresses – Manoj Naik Apr 27 '16 at 10:25
  • split them to list and then validate them with regex. Like this: `string[] emails = "mark345345@test.com;rtereter@something.com;terst@gmail.com;fault@mail".Split(';');` Then, iterate thru array and use Regex in answer that @Manoj wrote – Nino Apr 27 '16 at 10:28

4 Answers4

2

you can use EmailAddressAttribute class of System.ComponentModel.DataAnnotations namespace for validating the email address. Before that you need to split up individual mails and check whether it is valid or not. the following code will help you to collect the valid mails and invalid mails seperately.

List<string> inputMails = "mark345345@test.com;rtereter@something.com;terst@gmail.com;fault@mail".Split(';').ToList();
List<string> validMails = new List<string>();
List<string> inValidMails = new List<string>();
var validator = new EmailAddressAttribute();
foreach (var mail in inputMails)
{
    if (validator.IsValid(mail))
    {
        validMails.Add(mail);
    }

    else
    {
        inValidMails.Add(mail);
    }
}
sujith karivelil
  • 26,861
  • 6
  • 46
  • 76
  • `var emails = "test@gmail.com;testghj@feet.u;set_retret"; var validator = emails.Split(';').Select(email => new { email, IsValid = new EmailAddressAttribute().IsValid(email) } ); foreach ( var var in validator ) { Console.WriteLine(var.email + " " + var.IsValid); }` – bane 975 Apr 27 '16 at 12:59
0

You can use Regex or you might split the string by ';' and try to create a System.Net.Mail.MailAddress instance for each and every address. FormatException will occur if address is not in a recognized format.

Gabor
  • 1,989
  • 8
  • 17
0

If you're sure, that all e-mails are semi colon separated, you can split it and make a list of all. The best way for me to validate each e-mail is to use a regex pattern. I've used this one:

        var emailPattern = @"(?=^.{1,64}@)^[a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?=.{1,255}$|.{1,255};)(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])(;(?=.{1,64}@)[a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?=.{1,255}$|.{1,255};)(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9]))*$";
        var incomingString = "mark345345@test.com;rtereter@something.com;terst@gmail.com;fault@mail";
        var emails = incomingString.Split(';').ToList();

        foreach (var email in emails)
        {
            if (new Regex(emailPattern).IsMatch(email))
            {
                // your logic here
            }
        }
Tanya Petkova
  • 155
  • 1
  • 1
  • 7
0

Since .Net has out of the box ways to validate an email id, I would not use a regex and rely upon .Net. e.g the EmailAddressAttribute from System.ComponentModel.DataAnnotations. A clean way to use it would be something like:

var emailAddressAttribute = new EmailAddressAttribute();
var groups = yourEmailsString.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries) 
                             .GroupBy(emailAddressAttribute.IsValid);

This will give you 2 groups, the one with the Key == true will be valid email ids

var validEmailIds = groups.Where(group => group.Key)
                        .SelectMany(group => group);

the one with Key == false will be invalid email ids

var invalidEmailIds = groups.Where(group => !group.Key)
                        .SelectMany(group => group);

You could also run up a for loop after grouping, according to your needs..

bit
  • 4,139
  • 1
  • 25
  • 44
  • I came up with `var emails = "test@gmail.com;testghj@feet.u;set_+"; var areValid = emails.Split(';').All(o => new EmailAddressAttribute().IsValid(o));` but this is certainly far away of efficient solution – bane 975 Apr 27 '16 at 12:40