javascript - form validation- validate inputs dynamically created by ng-repeat -
i'm trying form validation angular. able validate input elements follows,
<tr ng-repeat="answer in answers track $index" ng-form="subquestionform"> <td> <div> <div ng-class="{ 'has-error' :((subquestionform.anstext.$invalid && issubmitted) || ((subquestionform.anstext.$invalid && subquestionform.anstext.$dirty))}"> <input name="anstext" id="anstext" type='text' ng-model="answer.anstext" class="form-control" required /> <div class="error,help-block col-md-offset-4" ng-show="((subquestionform.lhstext.$dirty || issubmitted) && ( subquestionform.lhstext.$invalid))"> <h6 class="help-block" ng-if="subquestionform.lhstext.$error.required">enter answer</h6> </div> </div> </div> </td> </tr>
this validate form such user has enter answer(i.e enter textbox created), want user should enter 2 or more . if 1 answer entered error message show otherwise form valid. tried doing taking index each failed. can throw light.
you can this:
<tr ng-repeat="answer in answers track $index" ng-form="subquestionform"> <td> <div> <input name="anstext" id="anstext" type='text' ng-model="answer.anstext" class="form-control" ng-required="$index == 0 || $index == 1 " /> </div> </td> </tr>
Comments
Post a Comment