0

Hi,

I want to validate multiple Email ids separated with comma(,) with single TextBox as CC.

please help me.

Thanks

Pankaj

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sknake
0

You can use logic like this to indicate the position of invalid emails. You didn't say VB/C# so I assumed C#:

private void button3_Click(object sender, EventArgs e)
    {
      string input = @"a@b.com, a@c.com, ffff23805789232(*@#(*@#&)(*, a@d.com, e@f.com";
      System.Net.Mail.MailAddress[] addrs = GetEmails(input);
      if (addrs.Length == 0)
      {
        //No emails were entered. Do something
      }
      else
      {
        List<int> invalidEmailPositions = new List<int>();
        for (int i1 = 0; i1 < addrs.Length; i1++)
        {
          if (addrs[i1] == null)
            invalidEmailPositions.Add(i1+1); //+1 since we count from 0
        }
        if (invalidEmailPositions.Count > 0)
        {
          string errorMessage = string.Format("Invalid addresses in positions: {0}",
            string.Join(", ", invalidEmailPositions.ConvertAll<string>(Convert.ToString).ToArray()));
          //They entered invalid emails in at least one position.
        }
        else
        {
          //You have all of the emails validated
        }
      }
      
    }

    private System.Net.Mail.MailAddress[] GetEmails(string s)
    {
      List<System.Net.Mail.MailAddress> lst = new List<System.Net.Mail.MailAddress>();
      if (string.IsNullOrEmpty(s))
        return lst.ToArray();
      
      string[] addrs = s.Split(new char[] { ',' }, StringSplitOptions.None);
      foreach (string email in addrs)
      {
        try
        {
          System.Net.Mail.MailAddress ma = new System.Net.Mail.MailAddress(email);
          lst.Add(ma);
        }
        catch (FormatException)
        {
          lst.Add(default(System.Net.Mail.MailAddress));
        }
      }
      return lst.ToArray();
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.