public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (string.IsNullOrEmpty(PlanId) && (string.IsNullOrEmpty(MemberId) || MemberId.Length != 9)) { yield return(new ValidationResult("The Member ID should have 9 characters", new[] { "MemberId" })); } if (Username == Password) { yield return(new ValidationResult("Password cannot be the same as Username", new[] { "Password" })); } if (Regex.IsMatch(Username, "[ '\";!@#$%^&*]")) { yield return(new ValidationResult("Username cannot contain space or next symbols ' \" ; ! @ # $ % ^ & * ", new[] { "Username" })); } // Validate ZipCode Length, mask from front-end might have 5 characters, but contains "_" so check for that if (!String.IsNullOrWhiteSpace(Zipcode) && (Zipcode.Length != 5 || Zipcode.Contains("_"))) { yield return(new ValidationResult("Invalid Zip Code", new[] { "Zipcode" })); } // Validate Phone Length if (!String.IsNullOrWhiteSpace(Phone)) { var cleanPhone = Phone.Replace("(", "").Replace(")", "").Replace("-", "") .Replace(" ", "").Replace("_", ""); if (cleanPhone.Length != 10) { yield return(new ValidationResult("Invalid phone number", new[] { "Phone" })); } } }