angular.module('exampleModule', [])
.controller('exampleController', function($scope) {
$scope.save = function(ngForm) {
if(ngForm.$invalid) {
$scope.invalidSubmitAttempt = true;
return;
}
alert('person saved!')
}
})
<!DOCTYPE html>
<html ng-app="exampleModule">
<head>
<link rel="stylesheet" href="style.css" />
<script src="http://code.angularjs.org/1.2.8/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="exampleController" ng-form="exampleForm">
<!-- FIRST NAME -->
<label for="firstName">First Name *</label>
<div>
<input type="text" id="firstName" name="firstName"
required="" ng-minlength="2" ng-maxlength="30"
ng-model="person.firstName" />
<div class="validation-error"
ng-show="(exampleForm.firstName.$dirty || invalidSubmitAttempt) && exampleForm.firstName.$error.required">
First Name is required.
</div>
<div class="validation-error"
ng-show="exampleForm.firstName.$dirty && exampleForm.firstName.$error.maxlength ||
exampleForm.firstName.$error.minlength">
First Name must be between 2 and 30 characters.
</div>
</div>
<button ng-click="save(exampleForm)">Save</button>
</div>
</body>
</html>
/*some optional CSS that will make your aa.formExtensions experience a little prettier*/
input.ng-dirty.ng-valid, select.ng-dirty.ng-valid {
border:1px solid Green;
}
input.ng-dirty.ng-invalid, select.ng-dirty.ng-invalid,
input.ng-invalid.aa-invalid-attempt, select.ng-invalid.aa-invalid-attempt,
input.ng-invalid.aa-had-focus, select.ng-invalid.aa-had-focus {
border:1px solid Red;
}
div.validation-icons {
margin-top: 5px;
margin-left: -40px;
}
/*requires font awesome*/
div.validation-icons i.fa-check {
color: green;
}
/*requires font awesome*/
div.validation-icons i.fa-exclamation-circle {
color: red;
}
div.validation-error {
color: red;
font-weight: bold;
width: 300px;
text-align: left;
}