1

I have an asp.net webform with a group of related checkboxes. I would like to verify that at least one checkbox is checked in the group when a submit button is clicked. I've tried to implement two different solutions with no success. When I try to return the length of the checked checkboxes by class name, I receive 0. When I try to return the ischecked value by name, I receive false. If I view the source of page, the checkboxes have the "checked" attribute. Can someone review my html and let me know if I'm doing something wrong?

  <tr>
      <td  colspan="2">
      <asp:CheckBox ID="cbga" runat="server"  CssClass="group1" name="group1[]" Text="A" />
      <asp:CheckBox ID="cbgb" runat="server" CssClass="group1"  name="group1[]" Text="B" />
     <asp:CheckBox ID="cbgc" runat="server" CssClass="group1"  name="group1[]" Text="C" />
</tr>

function ValChk(source, args) {
              var IsChecked = $(".group1:checked").length;//returns zero
           // var IsChecked = $('input[name="group1[]"]').is(':checked'); //returns false
              alert(IsChecked);    
        }

 <asp:CustomValidator ID="cvchks" runat="server" Display="dynamic"   
      ClientValidationFunction="ValChk" ErrorMessage="Please select a code"                                         
></asp:CustomValidator>
user2301181
  • 13
  • 1
  • 2
  • 6

1 Answers1

3

Working FIDDLE Demo

I think that your ValChk doesn't trigger. See the code below. It works correctly.

HTML

<input type="checkbox" class="group1" />
<input type="checkbox" class="group1" />
<input type="checkbox" class="group1" />

<input type="button" id="check" />

jQuery

$(function () {
    $('#check').on('click', function () {
        var IsChecked = !! $('.group1:checked').length;
        alert(IsChecked);
    });
});
  • I'm still having trouble determing if a checkbox is checked. Could it be related to Master pages? I tried to use the JQuery function above but I received #check).on is not a function. – user2301181 May 16 '13 at 12:50
  • Did you get `alert` or not? If you got, so there is no problem. We can handle it... –  May 16 '13 at 12:54
  • I get an alert when I used the original code I posted but it is always false or 0. When I use the code above, the alert is always false. Also, I receive the following error: Object doesn't support this property or method – user2301181 May 16 '13 at 13:45
  • It seems that your jQuery version is old. Try using latest version of jQuery or change `on` method to `bind`: `$('#check').bind('click', function () {`. –  May 16 '13 at 13:50
  • I upated to version 1.9.1 and that seemed to have corrected the error message. However, the function is still returning false when checkboxes are checked. If i check the length of the checked checkboxes, it is still zero. It does not see the checked attribute for some reason. – user2301181 May 16 '13 at 14:12