-
-
Save anonymoussc/3bc4e5bb327f51915026 to your computer and use it in GitHub Desktop.
Using Validation Variables to Control Element Visibility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html ng-app="exampleApp"> | |
<head> | |
<title>Forms</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script> | |
<script> | |
angular.module("exampleApp", []) | |
.controller("defaultCtrl", function($scope) { | |
$scope.addUser = function(userDetails) { | |
$scope.message = userDetails.name + " (" + userDetails.email + ") (" + userDetails.agreed + ")"; | |
}; | |
$scope.message = "Ready"; | |
}); | |
</script> | |
<style> | |
form .ng-invalid-required.ng-dirty { | |
background-color : lightpink; | |
} | |
form .ng-invalid-email.ng-dirty { | |
background-color : lightgoldenrodyellow; | |
} | |
form .ng-valid.ng-dirty { | |
background-color : lightgreen; | |
} | |
span.summary.ng-invalid { | |
color : red; | |
font-weight : bold; | |
} | |
span.summary.ng-valid { | |
color : green; | |
} | |
div.error { | |
color : red; | |
font-weight : bold; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="todoPanel" class="panel" ng-controller="defaultCtrl"> | |
<form name="myForm" novalidate ng-submit="addUser(newUser)"> | |
<div class="well"> | |
<div class="form-group"> | |
<label>Email:</label> | |
<input name="userEmail" type="email" class="form-control" required ng-model="newUser.email"> | |
<div class="error" ng-show="myForm.userEmail.$invalid && myForm.userEmail.$dirty"> | |
<span ng-show="myForm.userEmail.$error.email"> | |
Please enter a valid email address | |
</span> | |
<span ng-show="myForm.userEmail.$error.required"> | |
Please enter a value | |
</span> | |
</div> | |
</div> | |
<button type="submit" class="btn btn-primary btn-block" ng-disabled="myForm.$invalid"> | |
OK | |
</button> | |
</div> | |
</form> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment