In relation to a question I asked yesterday Validate if there is a checkbox selected AngularJS
As I said yesterday, I have a checkbox list and I had to see if at least one was selected, but another problem has arisen, since with the function that, validates all the checkboxes of the form and of course now that I finished the form, I had to add 2 checkboxes plus one for the privacy policy and another one for the age of majority, which must have a separate validation, one for the age checkbox and another for privacy.
<form id="formDetails" name="formDetails" ng-submit="sendForm()">
<div class="col-xs-12 col-md-12">
<div class="form-group col-xs-12 col-sm-6 col-md-4 form-info">
<label class="checkbox-inline"><input type="checkbox" id="topic" name="topic">Children's</label>
</div>
<div class="form-group col-xs-12 col-sm-6 col-md-4 form-info">
<label class="checkbox-inline"><input type="checkbox" name="topic" id="topic"> Health & Beauty</label>
</div>
<div class="form-group col-xs-12 col-sm-6 col-md-4 form-info">
<label class="checkbox-inline"><input type="checkbox" name="topic" id="topic">Science & Nature</label>
</div>
<div class="form-group col-xs-12 col-sm-6 col-md-4 form-info">
<label class="checkbox-inline"><input type="checkbox" name="topic" id="topic">Crafts & Hobbies</label>
</div>
<div class="form-group col-xs-12 col-sm-6 col-md-4 form-info">
<label class="checkbox-inline"><input type="checkbox" name="topic" id="topic">History</label>
</div>
<div class="form-group col-xs-12 col-sm-6 col-md-4 form-info">
<label class="checkbox-inline"><input type="checkbox" name="topic" id="topic">Sports & Fitness</label>
</div>
<span class="error" ng-show="formDetails.topic.$error.required">Please select at least one topic</span>
</div>
<div class="col-xs-12 col-md-12 policy">
<div class="form-group col-xs-12 col-sm-6 col-md-12 form-info check-policy">
<label class="checkbox-inline">
<input type="checkbox" ng-model="age" id="age" name="age">I can confirm I am over 16 years of age
<span class="error" ng-show="error">Please confirm you are over 16</span>
</label>
</div>
<div class="form-group col-xs-12 col-sm-6 col-md-12 form-info">
<label class="checkbox-inline">
<input type="checkbox" ng-model="terms" id="terms" name="terms">I agree to the terms of the Privacy Policy
<span class="error" ng-show="error">Please agree to the terms of the Privacy Policy</span>
</label>
</div>
</div>
<div>
<button type="submit" class="btn btn-danger">Sign Up</button>
</div>
</form>
I tried to modify the function that happened to me by modifying the input[type=checkbox]:checked"
by input[id=age]:checked"
or the corresponding id of the checkboxes but it does not work properly since only the checkbox of the privacy policy can be checked which already validates all the checkbox but if only that one is missing, the error message appears in all the checkboxes.
JS
var app = angular.module('formApp', []);
app.controller('formCtrl', function ($scope) {
$scope.error = false;
$scope.sendForm = function()
{
if($("#formDetails input[id=topic]:checked").length > 0) {
$scope.error = false;
}
else{
$scope.error = true;
}
if($("#formDetails input[id=age]:checked").length > 0) {
$scope.error = false;
}
else{
$scope.error = true;
}
if($("#formDetails input[id=terms]:checked").length > 0) {
$scope.error = false;
}
else{
$scope.error = true;
}
}
});
How could I do it so that when I give the button validate the checkboxes, from the list id=topic
on the one hand, the checkbox of the age on the other id=age
and the one on the privacy policy on the other id=terms
Thanks