0

I have been trying to validate that a check is checked, but so far have been unable to get anything to work. So far I have some Jquery that makes sure no more than one checkbox is selected.

I would appreciate any assistance to get this to work with my current code.

<form id="gradform" name="form1" method="post" action="index.php">
    <input type='checkbox' name='may' id='may' value='1' class='unique' data-group='mog'>
    <input type='checkbox' name='august' id='august' value='2' class='unique' data-group='mog'>
    <input type='checkbox' name='december' id='december' value='3' class='unique' data-group='mog'>
    <input type="submit" name="submit" value="submit" id="submit">
</form>
$(document).ready(function() {
    $('input.unique').click(function() {
        if (!$(this).prop('checked')) {
            return;
        }
        var group = $(this).data('group');
        if (group) {
            $('input[data-group="' + group + '"]:checked').prop('checked', false);
            $(this).prop('checked', true);
        }
    });
});

Here is my JS Fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

2 Answers2

4

You can use the condition that checks for the length of checked elements:

if($('#gradform :checkbox:checked').length){
   //checkbox is checked
}
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
3

You can use jQuery $.fn.is() method:

Description: Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

if($('#gradform :checkbox').is(':checked')){
   //At least on checkbox is checked
}
A. Wolff
  • 74,033
  • 9
  • 94
  • 155