ASP.Net的CheckBoxList的jQuery验证 [英] jQuery Validation of ASP.Net CheckBoxList

查看:122
本文介绍了ASP.Net的CheckBoxList的jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开始我只是想指出,由ASP.NET用于CheckBoxLists创建的code可能是我见过的最糟糕的事情。

Before I start I'd just like to state that the code created by ASP.NET for CheckBoxLists is probably the worst thing I've ever seen.

反正

我使用jQuery验证插件来验证我的ASP.net形式。有验证某些复选框的要求。这是由一个CheckBoxList控件生成的。

I am using the jQuery validation plugin to validate my ASP.net form. There is a requirement to validate some checkboxes. These are generated by a CheckBoxList control.

<asp:CheckBoxList ID="CBContext" runat="server" RepeatColumns="2" 
              DataSourceID="sqlLibraryEnquiries" DataTextField="value" DataValueField="value" name="topic">
</asp:CheckBoxList>

本控制产生的XHTML以下可憎

This control produces the following abomination of xHTML

<table id="MainContent_CBContext" name="topic">
    <tr>
        <td>
          <input id="MainContent_CBContext_0" type="checkbox" name="ctl00$MainContent$CBContext$0" value="Business" /><label for="MainContent_CBContext_0">Business</label>
        </td>
        <td>
          <input id="MainContent_CBContext_2" type="checkbox" name="ctl00$MainContent$CBContext$2" value="Legal" /><label for="MainContent_CBContext_2">Legal</label>
        </td>
    </tr>
    <tr>
        <td>
           <input id="MainContent_CBContext_1" type="checkbox" name="ctl00$MainContent$CBContext$1" value="Business Development" /><label for="MainContent_CBContext_1">Business Development</label>
        </td>
        <td>
           <input id="MainContent_CBContext_3" type="checkbox" name="ctl00$MainContent$CBContext$3" value="Library" /><label for="MainContent_CBContext_3">Library</label>
        </td>
    </tr>
</table>

我遇到的问题是实际上得到了jQuery插件验证挂钩到复选框列表。在我的规则,所有其他领域的部分,我可以将它们与自己的名字
例如ctl00 $ $日程地址搜索Maincontent tbActions:但所有的复选框有不同的名字

The issue I am having is actually getting the jQuery Validator plugin to hook into the checkbox list. In my rules section for all the other fields I can get to them with their names for example ctl00$MainContent$tbActions: but the checkboxes all have different names.

该cb_selectone规则不点火,因为我试图验证对象没有找到。
我曾尝试以下标识符。 CBContext,ctl00 $ $日程地址搜索Maincontent CBContext,MainContent_CBContext和复选框。

The cb_selectone rule isn't firing because the object I am trying to validate is never found. I have tried the following identifiers. CBContext, ctl00$MainContent$CBContext, MainContent_CBContext and checkboxes.

$("#Form1").validate({

     rules: {
     //WHAT GOES HERE???? --------->>    CBContext or ctl00$MainContent$CBContext or MainContent_CBContext or checkboxes all don't work: {
            cb_selectone: true
         }
      }
});

感谢您的帮助。

SM

推荐答案

好吧,我解决了它......

Ok I Solved it......

我所做的是创造,获取匹配MainContent_CBContext的正则表达式,该类型的输入的所有对象的新方法验证。这将返回所有复选框的数组。

What I did was create a new validator method that gets all the objects of type input that match a regex of MainContent_CBContext. This returns an array of all the checkboxes.

然后循环轮阵,如果attr为检查检查。如果其中的任何然后设置返回为真。

Then loop round the array and check if the attr is checked. If any of them are then set the return as true.

$.validator.addMethod('cb_selectone', function (value, element) {
     if (debug) {
         $.jGrowl("Adding Validation");
     }
     var chkGroup = $("input[id^=MainContent_CBContext]");
     if (chkGroup.length > 0) {
         for (var i = 0; i < chkGroup.length; i++) {
             if ($(chkGroup[i]).attr('checked')) {
                 if (debug) {
                    // alert(i + $(chkGroup[i]).val());
                     $.jGrowl("Running loop " + i + " = " + $(chkGroup[i]).val());
                 }
                 return true;
             }
         }
         return false;
     }
     return false;
 }, 'Please select at least one option');

我被困在部分是找到一个对象关火addMethod code。
最后我只是用...

The part I was stuck on was finding an object to fire off the addMethod code. In the end I just used...

ctl00$MainContent$CBContext$2: {
   cb_selectone: true
}

这意味着标签放在该字段旁边,这纯粹是化妆品。重要的是验证器code终于绑定到一个真正的对象,正确解雇了。

This meant that the label is placed next to this field, it's purely cosmetic. The important thing is the validator code was finally bound to a real object and fired correctly.

SM

这篇关于ASP.Net的CheckBoxList的jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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