javascript - How to validate if there is at least one checkbox selected out of 3 checkboxes in angularjs -
ok code:
<input name="chkbx1" type="checkbox" ng-model="loanreferdata.prop1" ng-class="submitted?'ng-dirty':''" required>prop 1</input> <input name="chkbx2" type="checkbox" ng-model="loanreferdata.prop2" ng-class="submitted?'ng-dirty':''" required>prop 2</input> <input name="chkbx3" type="checkbox" ng-model="loanreferdata.prop3" ng-class="submitted?'ng-dirty':''" required>other</input> <span class="error" ng-show="((frmloanrefer.chkbx1.$dirty || submitted) && frmloanrefer.chkbx1.$error.required) || ((frmloanrefer.chkbx2.$dirty || submitted) && frmloanrefer.chkbx2.$error.required) || ((frmloanrefer.chkbx3.$dirty || submitted) && frmloanrefer.chkbx3.$error.required) "> * please select atleast 1 property required.</span>
you can add 'ng-true-value' attribute in inputs example
<input name="chkbx1" type="checkbox" ng-true-value="true" ng-model="loanreferdata.prop1" ng-class="submitted?'ng-dirty':''" required>prop 1</input> <input name="chkbx2" type="checkbox" ng-true-value="true" ng-model="loanreferdata.prop2" ng-class="submitted?'ng-dirty':''" required>prop 2</input> <input name="chkbx3" type="checkbox" ng-model="loanreferdata.prop3" ng-true-value="true" ng-class="submitted?'ng-dirty':''" required>other</input> {{loanreferdata}}
and here answer
{"prop1":"true","prop2":"true","prop3":false}
Comments
Post a Comment