简体   繁体   English

jQuery Validation插件和ASP.NET验证器Web窗体

[英]jQuery Validation plugin and ASP.NET validators Web Forms

I would really like use the jQuery Validation plugin in my ASP.NET Web Forms application (not MVC). 我真的想在我的ASP.NET Web窗体应用程序(而不是MVC)中使用jQuery Validation插件

I value jQuery validation for the Richer UI experience I can provide the end user and I have some requirements to highlight the invalid fields using a red border (css). 我重视我可以为最终用户提供的Richer UI体验的jQuery验证,并且我有一些要求使用红色边框(css)突出显示无效字段。

I value asp.net validators because they run the validations not only on the client but on the server so that I don't open various security vulnerabilities to any user who is clever enough to turn off javascript in their browser. 我重视asp.net验证器,因为它们不仅在客户端而且在服务器上运行验证,因此我不会为任何聪明到能够在浏览器中关闭javascript的用户打开各种安全漏洞。

So I am looking for a nice clean way to integrate these two technologies. 所以我正在寻找一种很好的清洁方式来集成这两种技术。

The best I can come up with is to have all ASP.NET validators set to enableclientscript=false and repeat the validation rules on the client in jQuery and on the server as asp.net validators but I can already see some challenges with this approach. 我能想到的最好的方法是将所有ASP.NET验证器设置为enableclientscript = false,并在jQuery和服务器上重复客户端上的验证规则作为asp.net验证器,但我已经看到了这种方法的一些挑战。

In the end I found the lowest friction way to achieve highlighting with asp.net validators and jquery was not to use the jQuery Validation plugin but to use the simple line of jquery which follows (note that the exact syntax will vary depending on how you layout your forms): 最后,我找到了使用asp.net验证器实现突出显示的最低摩擦方式,jquery不使用jQuery Validation插件,而是使用后面的简单jquery行(请注意,确切的语法将根据布局方式而有所不同你的表格):

<script type='text/javascript'>


        $("input[@type=submit]").click(function() {
        $('span.validationerror:hidden').parent().parent().find('input').removeClass('inputError');
        $('span.validationerror:visible').parent().parent().find('input').addClass('inputError');                        

        });

</script>

You can use asp.net and jquery validators together with no problem... even on the same control... you just need to set CssClass for the control so that jquery can find it... and then you add OnClientClick="return $('#aspnetForm').valid();" 你可以一起使用asp.net和jquery验证器......即使在同一个控件上......你只需要为控件设置CssClass,这样jquery就可以找到它......然后你添加OnClientClick="return $('#aspnetForm').valid();" to your submit button(s) . 到你的submit button(s)

Be sure and include the usual jquery library and jquery.validate.js and add a document ready function of course, etc. 确保并包含通常的jquery库和jquery.validate.js,并添加文档就绪函数,当然等。

If you are serious about validation, I would look into something like Peter Blum's validators ( http://www.peterblum.com/Home.aspx ) as they should be able to do everything you want and more. 如果你认真对待验证,我会研究类似Peter Blum的验证器( http://www.peterblum.com/Home.aspx ),因为他们应该能够做你想做的一切,甚至更多。 They don't use JQuery, but remember the whole point of JQuery is to save developer time - the end user doesn't know or care whether you are using JQuery or not if the overall effect is the same. 他们不使用JQuery,但要记住JQuery的重点是节省开发人员的时间 - 如果整体效果相同,最终用户不知道或不关心你是否使用JQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM