ASP.NET MVC客户端验证

时间:2022-01-31 15:10:30

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

我所有关于使用ASP.NET MVC,但我希望改进的领域之一是客户端验证。

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.

我知道最新版本(预览版5)有很多新的验证功能,但它们似乎都是在页面发布后。

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

我看过Steve Sanderson的一篇有趣的文章......使用实时验证,Castle.Components.Validator.dll和他制作的Validator框架。

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.

我在我的项目中使用过它,但我希望它能够集成到官方的ASP.NET MVC版本中。我绝对认为业务规则应该驻留在模型上或控制器中而不是视图中。

Have any of you used a similar approach?

你们有没有用过类似的方法?

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

您是否意识到将这样的内容添加到官方ASP.NET MVC版本中?

5 个解决方案

#1


18  

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

“显然,你仍然需要在服务器端验证你禁用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.

只是对此评论的更新。服务器端验证与禁用JavaScript的用户无关。相反,出于安全原因需要它,并且需要进行无法在客户端上执行的复杂验证。表单应始终具有服务器端验证。客户端验证只是为了方便起见。

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


1  

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.

查看JQuery Validation插件。开始使用基本验证非常容易 - 实际上是一行JS以及向输入控件添加类名。它也非常强大。您可以扩展以执行任何操作。

#3


0  

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

LiveValidation是另一个有用的javascript库,可以提供帮助。在这里查看一个示例(使用ASP.NET MVC):

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

#4


0  

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.

看看这篇博客文章。它描述了如何使用xVal自动生成客户端验证规则,以及如何自动实现远程客户端验证。

#5


0  

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

看起来这个领域将在ASP.NET MVC 2中看到很多改进

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

#1


18  

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

“显然,你仍然需要在服务器端验证你禁用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.

只是对此评论的更新。服务器端验证与禁用JavaScript的用户无关。相反,出于安全原因需要它,并且需要进行无法在客户端上执行的复杂验证。表单应始终具有服务器端验证。客户端验证只是为了方便起见。

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


1  

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.

查看JQuery Validation插件。开始使用基本验证非常容易 - 实际上是一行JS以及向输入控件添加类名。它也非常强大。您可以扩展以执行任何操作。

#3


0  

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

LiveValidation是另一个有用的javascript库,可以提供帮助。在这里查看一个示例(使用ASP.NET MVC):

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

#4


0  

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.

看看这篇博客文章。它描述了如何使用xVal自动生成客户端验证规则,以及如何自动实现远程客户端验证。

#5


0  

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

看起来这个领域将在ASP.NET MVC 2中看到很多改进

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