ASP.NET MVC Web API data annotation regular expression for email

不想你离开。 提交于 2019-12-08 00:08:59

问题


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?


回答1:


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.




回答2:


[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




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!