Tag Archives: validation

Data Annotations MVC Validation

If you are looking for the attribute or data annotation to be used in your MVC model e.g. to validate email addresses you may in reality be looking for the DataType Enumeration (remember it next time you google it).

Actually, the EmailAddress data type doesn’t work for the validation. I use my own regex:

[RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Not a valid e-mail")]

This is just a little note for me, as I lost quite some time to find this.

UPDATE: the regular expression above is wrong, email like first.last@my-company.com was treated as invalid (to be updated when I have free time).

This works (with .net 4.5):

public string Email { get; set; }