从字符串数组中验证电子邮件地址

validate email address from an array of strings

我需要通过字符串数组作为输入来验证电子邮件地址是否有效?

我有下面的代码适用于 string emailID 但我需要同样的代码用于 string[] emailID。提前致谢!!!

public static class EmailValidator
{
  const string EmailEx = @ "^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"

  private static ErrorDetail IsEmail(string input)
  {
    return !Regex.IsMatch(input, EmailEx)
        ? new ErrorDetail { Message = "Not a valid Email" }
        : null;
  }

  public static Func <string, ErrorDetail> ValidationMethod(string emailID)
  {
    return IsEmail;
  }
}

添加 using System.Collections.Generic; 以使以下方法生效。

public static class EmailValidator
    {
        const string EmailEx = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$";

        private static ErrorDetail IsEmail(string input)
        {
            return !Regex.IsMatch(input, EmailEx)
                ? new ErrorDetail { Message = "Not a valid Email" }
                : null;
        }

        public static Func<string, ErrorDetail> ValidationMethod(string emailID)
        {
            return IsEmail;
        }

        public static Dictionary<string, ErrorDetail> AreEmailValid(string[] arrEmail)
        {
            Dictionary<string, ErrorDetail> invalidEmailDict = new Dictionary<string, ErrorDetail>();
            ErrorDetail errorDetail;

            foreach (string str in arrEmail)
            {
                errorDetail = IsEmail(str);
                if (errorDetail != null)
                {
                    invalidEmailDict.Add(str,errorDetail);
                }
            }

            return invalidEmailDict;
        }
    }