Last active
August 29, 2015 13:58
-
-
Save nootn/10091198 to your computer and use it in GitHub Desktop.
Yet another email validation mechanism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Model : IValidatableObject | |
{ | |
[Display(Name = "Send to (email)")] | |
[Required(ErrorMessage = "'{0}' must be supplied")] | |
[RegularExpression(@".+\@.", ErrorMessage = "{0}' must be an email address")] | |
public override string ToEmailAddress { get; set; } | |
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | |
{ | |
ValidationResult emailInvalidError = null; | |
try | |
{ | |
var email = new System.Net.Mail.MailAddress(ToEmailAddress); | |
Debug.WriteLine("Email validated with host '{0}'", email.Host); | |
} | |
catch (FormatException) | |
{ | |
emailInvalidError = new ValidationResult("'Send to' must be a valid email address", new[] { "ToEmailAddress" }); | |
} | |
if (emailInvalidError != null) | |
{ | |
yield return emailInvalidError; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment