开发者

ASP.Net MVC2 Client and Server Validation sharing the same code - is it possible?

With the excellent XVal by Steve Sanderson, it is possible to tell the client side validation to post the value being validated to the server using jquery. A meth开发者_JS百科od on the server then uses the same server side code you use for your server side validation, and returns simply a true or false to determine if the field is valid.

The advantage of this method is that you write your complex validation logic once in C# code and then put some JQuery plumbing in to tell your client page where to go to access your server validation.

I have been reading some blogs on MVC2 but no one seems to mention this functionality. Is it possible to tell the Microsoft MVC validation javascript to call a url validate data? Or do you have to write your own client side validation routines.

I should note that using the xVal method a custom validation to say if an email address is in use or not can be run from the client via a JQuery post which accesses the server side validation logic.


There's a video by Phil Haack from Mix 2010 here: http://live.visitmix.com/MIX10/Sessions/FT04

There's a section where he deals with writing JS code to perform custom validation....you could use this as a starting point for making a server side call to perform the validation.


I wrote a blog article exactly about this: Remote form validation with jquery.validate and xVal

It also comes with a demo project. The codebase is still ASP.NET MVC 1, but the changes required for ASP.NET MVC 2 are minor.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜