html - Jquery input validation dependent on checkbox value -
i trying avoid validation mess , using inline validation. have checkbox followed 3 input boxes. if checkbox checked, input fields have become "required". else not.
here code:
<div class="col-xs-12"> <div class="form-group"> <label class="checkbox-inline"> <input type="checkbox" id="vi" name="vi" checked="checked"> have apple </label> </div> </div> <div class="col-xs-6"> <div class="form-group"> <label>apple condition?</label> <input type="input" class="form-control" id="applecond" validate="required:'checkbox[name=vi][value=yes]:checked'"> </div> </div> <div class="col-xs-6"> <div class="form-group"> <label>sourced ?</label> <input type="input" class="form-control" id="applesource" validate="required:'checkbox[name=vi][value=yes]:checked'"> </div> </div> <div class="col-xs-6"> <div class="form-group"> <label>quantity</label> <input type="input" class="form-control" id="applequantity" validate="required:'checkbox[name=vi][value=yes]:checked'"> </div> </div> </div>
i sure doing wrong syntax. can point correct syntax?
well can done follows. default make checkbox unchecked , write following jquery
it
$(document).ready(function(){ var atleastoneischecked = $('input[name="vi"]:checked').length > 0; if(atleastoneischecked){ $("input").attr("required", "true"); } else { // else need case } });
Comments
Post a Comment