Using [RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$")]
attribute never validates an email property of my model. I'm new to regular expressions so I can't figure out what is the problem. Little help?
Look at this article: How To Validate Email Address With Regular Expression. The guy not only providing you with the regular expression, but also explaining step by step what it means. Not only its useful as an expression, but you can also learn a thing or two from this article.
Hope this helps. Please let me know if not.
[Required]
[Display(Name = "Email Address")]
[EmailAddress]
public string email { get; set; }
Something like this should solve you problem. The email validation has been build in 4.5 check out this msdn documentation
Did you check the ModelState property for validation errors? Look at the example at the bottom of this page:
http://blogs.msdn.com/b/youssefm/archive/2012/06/28/error-handling-in-asp-net-webapi.aspx
for a way of sending back error messages to the client when validation fails.
来源:https://stackoverflow.com/questions/15612603/asp-net-mvc-web-api-data-annotation-regular-expression-for-email