开发者

Best way to do client/server validation in ASP.NET in 2010?

First there was the ASP.NET validators and we used them... Then some people on the team did things manually in javascript... Then a bunch of jquery validation libraries came out... Then MVC2 came out with attributes as validators..

I work with apps that have alot of forms with alot of vari开发者_JAVA百科ous validation (Some fields needs to be compared with other values in a DB so a postball/ajax call is required) .. Right now I have a mess of ASP.NET custom validators and functions that calculate on the server side as well.

Can I get some opinions on the best tool/combination to approach this job that can create the smallest/most elegant code? Pure server side solution? AJAX/Jquery? A certain plugin for jquery?

For example, I have 2 dates.. I want to make sure that the 1st date is less than the 2nd date... Are there jquery validators that encapsulate this? My feeling is if I can get jquery plugins to handle half the more basic validation for my that could cut my code in half.


DynamicData for asp.net mvc and dynamic data.

On webforms I have only used the the asp.net validators approach. This doesn't mean to go copy-pasting all over custom validations you may have i.e. you add your own custom validators as needed and only on very special circumstances you use extra validators hooked to methods+scripts on the page.

Depending on how you structure your code, I would expect to be able to leverage some of the infrastructure around DataAnnotations. I can't give it for a fact, you know the usual issues on asp.net something usually tied up to implementation details, worth a shot if there aren't better options.

Update 1: a v. quick search gave: http://blogs.microsoft.co.il/blogs/gilf/archive/2010/04/08/building-asp-net-validator-using-data-annotations.aspx, which is rudimentary piece - but shows a way that you can leverage the data annotations on web forms (for the server side).

Update 2: another one with the same approach as above, but does both server & client side validation: http://geeks.ms/blogs/rcarreras/archive/2010/04/20/validando-asp-net-web-form-con-dataannotation-en-ambos-lado-cliente-y-servidor.aspx. Again rudimentary, but certainly can be used as the basis to get a more complete automatic validation based on the annotations. Note: depending on what you use you can have the validators automatically added on the appropriate places, although I must warn that it might be tricky to get it to work well if you haven't worked with dynamically added validators before


If you're using ASP.NET MVC, I recommend the DataAnnotations attributes across the board. There are "simple" ones included for required fields and length, and you can write your own custom validators for comparison of two fields and others. Phil Haack as a great article all about custom validators in ASP.NET MVC.


I usually use jquery RSV plugin http://plugins.jquery.com/project/RSV to do my validation job for the normal stuff, and i code my own jquery functions for the more complicated validation (requiring ajax calls and stuff) and i always re-validate on the server side.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜