[Test] public void TestValidateEmail() { CustomerAddress customerAddressController = new CustomerAddress(); string messageGood = customerAddressController.Rules.ValidateEmail("name@domain.com"); Assert.IsNullOrEmpty(messageGood); string messageBad = customerAddressController.Rules.ValidateEmail("name@domain,com"); Assert.IsNotNullOrEmpty(messageBad); }this is the rule
public string ValidateEmail(string email) { string msg = null; if (!Validate.Email(email)) { this.EntityPropertyDisplayName = "Customer Email"; msg = this.EntityPropertyDisplayName + " is not a valid email address!"; AddErrorProviderWarning("Email", msg); } return msg; }this is the Validate.Email method
public static bool Email(string email) { string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+" + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@" + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+" + @"[a-zA-Z]{2,}))$"; Regex matcher = new Regex(patternStrict); bool isMatch = matcher.IsMatch(email); return isMatch; }