uidev uidev - 7 months ago 255
Javascript Question

jquery validation plugin for checkbox- at least one selected

I am using jquery validation plugin for form fields validation. I have multiple checkboxes in the form. I tried couple of different ways for the logic 'at least one need to be selected' but nothing is working but when i try something out of validation plugin its working fine with this code. If you can tell me how i can incorporate this below code within validation plugin

<script type="text/javascript">
$(document).ready(function () {
$('.submit').click(function() {
checked = $("input[type=checkbox]:checked").length;

if(!checked) {
// <form:errors path="chk" cssClass="errors" element="div" />
$(".form-error").text("* You must check at least one checkbox.").show();
// alert("You must check at least one checkbox.");
return false;
}

});
});
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script>


$(document).ready(function(){

$("#myForm").validate({



rules: {
crcode: "required",
dtype: "required",
pview: "required",
prview: "required",


},

messages: {
crcode: "Rate code is required",
dtype: "Dwell type is required",
pview: "Public view is required",
prview: "Private view is required",


}

});
});

</script>
Here are my checkboxes
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;" >
<input type="checkbox" name="chkbox" id="fancy-checkbox-default1" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default1" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default1" class="[ btn btn-default active ]">
Video+Internet
</label>
</div>
</div>
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;">
<input type="checkbox" name="chkbox" id="fancy-checkbox-default2" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default2" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default2" class="[ btn btn-default active ]">
Video+Phone&nbsp;&nbsp;
</label>
</div>
</div>
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;">
<input type="checkbox" name="chkbox" id="fancy-checkbox-default3" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default3" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default3" class="[ btn btn-default active ]">
Video+Security
</label>
</div>
</div>

Answer

if you can tell me how I can incorporate this below code within validation plugin

$('.submit').click(function() {
    checked = $("input[type=checkbox]:checked").length;
    if (!checked) { ....

You don't.

That code makes no sense for use with jQuery Validate plugin and can be removed entirely. When using the jQuery Validate plugin, you do not need to write a wholly independent function for checkbox validation, NOR do you need a click handler of any kind.

You simply need to declare the required rule on the checkbox name and the plugin will automatically make at least one checkbox required.

$(document).ready(function(){

      $("#myForm").validate({
          rules: {
              chkbox: "required", // <- add this
              crcode: "required",
              dtype: "required",
              pview: "required",
              prview: "required",
          },
          messages: {
              chkbox: "* You must check at least one checkbox.", // <- add this
              crcode: "Rate code is required",
              dtype: "Dwell type is required",
              pview: "Public view is required",
              prview: "Private view is required",
          }
      });

});

DEMO: http://jsfiddle.net/42t2twLo/

Exact placement of this checkbox error message can be facilitated with the errorPlacement option.

errorPlacement: function(error, element) {
    if (element.is(":checkbox")) {
        error.insertBefore(element);  // custom placement example
    } else { 
        error.insertAfter(element);   // default placement
    }
}