Reputation: 151
I am starting to use Angular JS and I tryng to validate a form. But I don't know how to validate some inputs if the Checkbox is checked.
<body>
<div ng-app="">
<form name="myForm">
<input name="myName" ng-model="myName" required>
<span ng-show="myForm.myName.$touched && myForm.myName.$invalid">The name is required.</span><br>
<input name="myphone" ng-model="myphone"><!--NO REQUIRED--><br>
Check to Validate:
<input type="checkbox" ng-model="myVar"><br>
<input type="text"><!--VALIDATE IF checkbox is checked--><br>
<input type="text"><!--VALIDATE IF checkbox is checked--><br>
</form>
<input type="submit">
</div>
And how to disable the input if something is incorrect?
Upvotes: 0
Views: 76
Reputation: 1354
You can use the ng-required
directive.
<div ng-app="">
<form name="myForm">
<input name="myName" ng-model="myName" required>
<span ng-show="myForm.myName.$touched && myForm.myName.$invalid">The name is
required.</span><br>
<input name="myphone" ng-model="myphone"><!--NO REQUIRED--><br>
Check to Validate:
<input type="checkbox" ng-model="myVar"><br>
<input type="text" ng-if="myVar"> <!-- this show ups -->
<input type="text" ng-required="myVar">
<span ng-show="myForm.$invalid">
<input type="text" ng-required="myVar">
<span ng-show="myForm.$invalid">
</form>
<input type="submit">
</div>
CONTROLLER
In the controller you can check based on the model asigned
$scope.submitFunction = function(){
if($scope.myVar){
//do stuff
}
}
Upvotes: 1