0

I am having a dynamic checkbox which is working on foreach. I want to add a validation for that. If I have added 'required' in my input type, it is asking to select all the checkboxes, Is there any other option to validate at least one checkbox is checked.

My Form

<?php 
foreach($category_list as $list) {
  if(!empty($prop_cat_check)){
    $checked = null;
    if(in_array($list->cat_id,$prop_cat_check)){
        $checked = 'checked';
      }
    }                       
    ?>                      
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>" 
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" >
<?php echo $list->cat_title; 
}
} ?> 
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
saravana
  • 311
  • 4
  • 14
  • 2
    Possible duplicate of [JQuery - is at least one checkbox checked](http://stackoverflow.com/questions/2941746/jquery-is-at-least-one-checkbox-checked) – lukassteiner Jun 14 '16 at 13:46

1 Answers1

1

try this code

    <?php 
$k==0;
foreach($category_list as $list) {
  if(!empty($prop_cat_check)){
    $checked = null;
    if(in_array($list->cat_id,$prop_cat_check)){
        $checked = 'checked';
      }
    }                       
    ?>                      
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>" 
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" <?php if($k==0) echo 'required';?>>
<?php echo $list->cat_title; 
$k++;
}
} ?> 
aniket ashtekar
  • 290
  • 1
  • 11
  • It works only when the first checkbox has checked,if we checked any other in the list it still says the first checkbox should check :( – saravana Jun 14 '16 at 14:21