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>
3
  • Try the sample in here :) possible duplicate of JQuery - is at least one checkbox checked Commented May 16, 2013 at 3:06
  • Post a jsfiddle.com sample. Will get you answers easier Commented May 16, 2013 at 3:09
  • The pseudo class 'checked' usually works Commented May 16, 2013 at 3:11

1 Answer 1

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);
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

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.
Did you get alert or not? If you got, so there is no problem. We can handle it...
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
It seems that your jQuery version is old. Try using latest version of jQuery or change on method to bind: $('#check').bind('click', function () {.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.