开发者

Client/Server Validate Common Data Types (e-mail address) with ASP.NET MVC

What is the preferred method (one that has minimal custom code and ideally is portable to planned future versions of MVC without extra widgets) to validate common datatypes (e.g. e-mail addresses, dates, phone numbers) at both the client and server?


MY RESEARCH

I'm going to list a few methods I've seen, approximately from worst to best 开发者_如何学Go(IMHO). I'm currently using the last method listed. I'll focus on e-mail validation in this post, to keep things clear.

REGEX AND/OR CUSTOM VALIDATION ATTRIBUTE

I know jQuery validate includes some common datatypes including e-mail, and additional plugins exist for download (e.g. integer, max words). So custom regex's here are not the right answer.

I know how to write a custom validator from scratch at server and client, and even to 'adapt' an existing client-side rule to a custom attribute when using the unobtrusive connector. http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html but it probably doesn't make sense for such a common type as e-mail address.

Nor, probably, to extend a regex rule, as per: http://www.pagedesigners.co.nz/2011/02/asp-net-mvc-3-email-validation-with-unobtrusive-jquery-validation/

OVERRIDING STOCK DataType ATTRIBUTE VALIDATORS

.NET includes [System.ComponentModel.DataAnnotations.DataType(DataType.EmailAddress)] This causes always-succeed validation at client or server, and is really only useful out-of-the-box for a tangential purpose of formatting display strings. Overriding the always-succeed validation is possible, as per: http://weblogs.asp.net/srkirkland/archive/2011/02/15/adding-client-validation-to-dataannotations-datatype-attribute.aspx

MVC DATA VALIDATION EXTENSION (VIA NUGET)

A NuGet-downloadable validation extension was released several months ago, and I'm currently using it, but I was surprised to see that it did not leverage the existing DataAnnotations.DataType enum. It makes me wonder if there is some development divergence that I should be avoiding here. http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx Also, it doesn't include phone numbers or US phone numbers.

Any better method?


Have you know about ASP.NET MVC 3 Futures Validation attributes, http://weblogs.asp.net/imranbaloch/archive/2011/02/05/new-validation-attributes-in-asp-net-mvc-3-future.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜