ASP.NET MVC Client Side Validation

半腔热情 提交于 2019-12-07 05:52:11

问题


I am all about using ASP.NET MVC, but one of the areas that I hope gets improved on is Client-Side Validation.

I know the most recent version (Preview 5) has a lot of new features for Validation, but they all seem to be after the page has been posted.

I have seen an interesting article by Steve Sanderson... using Live Validation, Castle.Components.Validator.dll, and a Validator framework he made.

I have used it in my project, but I am hoping something like it will get integrated into the official ASP.NET MVC release. I definitely think the business rules should reside either on the model or in the controller rather than in the View.

Have any of you used a similar approach?

Are you aware of something like this being added to the official ASP.NET MVC release?


回答1:


"Obviously you'll still need to validate your input on the server side for the small percentage of users who disable javascript."

Just an update to this comment. Server-side validation has nothing to do with users that run with JavaScript disabled. Instead, it is needed for security reasons, and to do complex validation that can't be done on the client. A form should always have server-side validation. Client-side validation is only there as a convenience.

A malicious user could easily post data to your form bypassing any client-side validation that you have in place. Never trust input data!




回答2:


I agree with other posters, client side validation is strictly for improving user experience.

Check out the JQuery Validation plugin. It's super easy to get started with basic validation -- literally one line of JS plus adding class names to the input controls. It's also very powerful. You can extend to do whatever you want.




回答3:


LiveValidation is another helpful javascript library that can help out. See an example (with ASP.NET MVC) here:

http://blog.codeville.net/2008/09/08/thoughts-on-validation-in-aspnet-mvc-applications/




回答4:


It looks like this area will see lots of improvement in ASP.NET MVC 2

http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-preview-1-released.aspx




回答5:


Have a look at this blog article. It describes how to automatically generate client-side validation rules with xVal and also how to automatically implement remote client-side validation.



来源:https://stackoverflow.com/questions/153039/asp-net-mvc-client-side-validation

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