Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

c# - validate email address from an array of strings

I need to validate that email address is valid or not from an array of strings as input?

I am having the below code which works for string emailID but i need the same for string[] emailID. Thanks in Advance!!!

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;
  }
}
question from:https://stackoverflow.com/questions/65936435/validate-email-address-from-an-array-of-strings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add using System.Collections.Generic; for following method to work.

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;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...