Reputation: 13
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>
Upvotes: 1
Views: 957
Reputation:
I think that your ValChk
doesn't trigger. See the code below. It works correctly.
<input type="checkbox" class="group1" />
<input type="checkbox" class="group1" />
<input type="checkbox" class="group1" />
<input type="button" id="check" />
$(function () {
$('#check').on('click', function () {
var IsChecked = !! $('.group1:checked').length;
alert(IsChecked);
});
});
Upvotes: 3