I’m trying to validate an e-mail string, and it seems like all public options I use in C# don’t work at all An example, using the regex found in http://www.regular-expressions.info/email.html, i create a model with the following property:
[RegularExpression(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", ErrorMessage = "Please enter a valid email address.")]
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "The Email address where we can reach you:")]
public string Email { get; set; }
This always throws the error for some reason, even though it is just a straight copy from that website. I have the @ parameter in front of the regex, but are there other considerations i need to make in order to have this work in C#?
You need to allow lowercase letters too. Every time you have
A-Zyou can change it toA-Za-z.Note also that this regular expression rejects some valid email addresses, as explained on the site. For example, it rejects email addresses from the
.museumtop-level domain, to give just one example.