ASP.NET MVC Client Side Validation

余生颓废 提交于 2019-12-05 10:27:36

"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!

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.

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/

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

Adrian Grigore

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.

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