需要一个更好的方法来验证ASP.NET复选框使用jquery验证插件? [英] Need a better way to validate ASP.NET checkboxes using a jquery validation plugin?

查看:101
本文介绍了需要一个更好的方法来验证ASP.NET复选框使用jquery验证插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:



我想使用jquery验证插件验证一些ASP.NET复选框(位于 http://bassistance.de/jquery-plugins/jquery-plugin-validation/



所有内容:



无法设置名称属性的ASP.NET复选框(或者是?)。系统会在控制项呈现时自动设置,并可使用

 <%= emailCheckBox.UniqueID%> ; 

如下两个复选框:

 < asp:CheckBox runat =serverID =emailAcceptCheckBox/>电子邮件< br /> 
< asp:CheckBox runat =serverID =phoneAcceptCheckBox/>电话< br />

会呈现给:

 < INPUT id =ctl00_MainContentPlaceHolder_emailAcceptCheckBoxtype =checkboxname =ctl00 $ MainContentPlaceHolder $ emailAcceptCheckBox> 
< INPUT id =ctl00_MainContentPlaceHolder_phoneAcceptCheckBoxtype =checkboxname =ctl00 $ MainContentPlaceHolder $ phoneAcceptCheckBox>

也许混合使用ASP.NET与jquery验证插件是一件麻烦,但我更喜欢jquery验证插件和它的输入和其他字段工作正常。



问题是,jquery验证插件要使用复选框的名称属性分组复选框。此名称属性应等于所有复选框和一个组。



我所做的:



我使用jquery向所有复选框添加了一个规则:

  $(#[id * ='AcceptCheckBox '])。each(function(){$(this).rules(add,{minchecked:1});}); 

并为复选框添加了自己的规则检查:

  jQuery.validator.addMethod(minchecked,
function(value,element,param){
var noOfChecked = $(#[id * = 'AcceptCheckBox']:checked)。length;
return noOfChecked> = param;
},
Error);

(此规则适用于ID包含AcceptCheckBox的复选框,但在此示例中可以) / p>

当我调用:

  var result = $(#aspnetForm ).validate()。form(); 

如果我的验证成功或失败,它会完美返回!


$ b b

BUT:



1)我不觉得这是一个非常干净,漂亮的解决方案。任何更好的建议?



2)当我运行validate()。form()它将通过所有我的复选框,并为每个复选框检查所有其他复选框。这真的没有必要。

解决方案

总的来说,你的代码对我来说看起来不错。我想你已经干净地处理了asp:checkbox限制(你不能为asp:复选框指定'name'属性)。



就我个人而言,我会使用普通HTML复选框。

 < input type =checkboxrunat =serverID =emailAcceptCheckBoxname =acceptCheckBoxvalue = emailAccepted/>< label for =emailAcceptCheckBox>电子邮件< / label>< br / 
< input type =checkboxrunat =serverID =phoneAcceptCheckBoxname =acceptCheckBoxvalue =phoneAccepted/>< label for =phoneAcceptCheckBox>电子邮件< / label& < br />

(我添加了一个标签标签,以便用户可以点击标签/取消选中该复选框。)



以上将简化您的jQuery代码。



在您的服务器端代码中,检查Request.Form [acceptCheckBox]以查看检查内容和未检查内容。


Problem:

I want to validate some ASP.NET-checkboxes using the jquery validation plugin (found at: http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

What it's all about:

It's not possible to set the name attribute of ASP.NET checkboxes (or is it?). It'll automatically be set, when the control is rendered and can be retrieved using

<%= emailCheckBox.UniqueID %>

So two checkboxes as the following:

<asp:CheckBox runat="server" ID="emailAcceptCheckBox" />Email<br />
<asp:CheckBox runat="server" ID="phoneAcceptCheckBox" />Phone<br />

will render to:

<INPUT id="ctl00_MainContentPlaceHolder_emailAcceptCheckBox" type="checkbox" name="ctl00$MainContentPlaceHolder$emailAcceptCheckBox">
<INPUT id="ctl00_MainContentPlaceHolder_phoneAcceptCheckBox" type="checkbox" name="ctl00$MainContentPlaceHolder$phoneAcceptCheckBox">

Maybe it's a mess to mix ASP.NET with the jquery validation plugin, but I prefer the jquery validation plugin and it works fine with inputs and other fields.

Problem is, that the jquery validation plugin wants to group the checkboxes using the name-attribute of checkboxes. This name attributes should be equal to all checkboxes and a group.

What I've done:

I added a rule to ALL checkboxes using jquery:

$("#[id*='AcceptCheckBox']").each(function() { $(this).rules("add", { minchecked: 1 }); });

And added my own rule checking for checkboxes:

jQuery.validator.addMethod("minchecked",
 function(value, element, param) {
     var noOfChecked = $("#[id*='AcceptCheckBox']:checked").length;
          return noOfChecked >= param;
     },
 "Error");

(This rule will apply for checkboxes whose IDs include AcceptCheckBox, but it's okay in this example).

When I call:

var result = $("#aspnetForm").validate().form();

It returns perfectly if my validation succeeded or failed!

BUT:

1) I don't feel that this is a very clean and nice solution. Any better advice?

2) When I run validate().form() it'll go through ALL my checkboxes and for each of them check all the other checkboxes. That's really not necessary. How can I avoid this?

解决方案

Overall, your code does look fine to me. I think you have cleanly handled the asp:checkbox limitation (you can't specify 'name' attribute for asp:checkbox).

Personally, I would use regular HTML checkbox.

<input type="checkbox" runat="server" ID="emailAcceptCheckBox" name="acceptCheckBox" value="emailAccepted" /><label for="emailAcceptCheckBox">Email</label><br />
<input type="checkbox" runat="server" ID="phoneAcceptCheckBox" name="acceptCheckBox" value="phoneAccepted" /><label for="phoneAcceptCheckBox">Email</label><br />

(I have added a label tag so that user can click on the label (bigger target) to check/uncheck the checkbox.)

Above will simplify your jQuery code.

On your server side code, check Request.Form["acceptCheckBox"] to see what is checked and what is not.

这篇关于需要一个更好的方法来验证ASP.NET复选框使用jquery验证插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆