AngularJS Validation: Building Trustworthy Forms - FadiZahhar/AngularJs GitHub Wiki
The client sends a final form feature request:
“Please make sure users can’t submit unless all fields are correct. Show helpful messages if anything is wrong!”
Mike (Team Lead): “Good validation prevents mistakes and builds user trust. AngularJS can validate everything right in the view!”
Sara (Junior Dev): “With AngularJS, we get live feedback, custom messages, and full control—no more submitting bad forms!”
-
Form controls like
input
andselect
automatically track state and validity if you useng-model
. -
Validation directives:
required
,ng-minlength
,ng-maxlength
,ng-pattern
,type="email"
, and more.
AngularJS form fields track:
-
$valid
/$invalid
: Is the field valid? -
$touched
/$untouched
: Has it been focused and left? -
$dirty
/$pristine
: Has its value changed?
<form name="regForm">
<input name="username" ng-model="user.username" required>
<span ng-show="regForm.username.$touched && regForm.username.$invalid">
Username is required!
</span>
<button ng-disabled="regForm.$invalid">Submit</button>
</form>
- If the user touches and leaves the field empty, a message appears.
- The submit button is only enabled if the form is valid.
Password must be at least 6 characters:
<input name="password" ng-model="user.password" ng-minlength="6" required>
<span ng-show="regForm.password.$touched && regForm.password.$error.minlength">
Password must be at least 6 characters!
</span>
Custom pattern (letters only):
<input name="nickname" ng-model="user.nickname" ng-pattern="/^[a-zA-Z]+$/" required>
<span ng-show="regForm.nickname.$touched && regForm.nickname.$error.pattern">
Only letters allowed!
</span>
AngularJS validates emails out of the box:
<input type="email" name="email" ng-model="user.email" required>
<span ng-show="regForm.email.$touched && regForm.email.$invalid">
Please enter a valid email!
</span>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<style>
input.ng-invalid.ng-touched { border: 1px solid red; }
input.ng-valid.ng-touched { border: 1px solid green; }
</style>
</head>
<body>
<div ng-app="formApp" ng-controller="formCtrl">
<form name="regForm">
<p>
Username:
<input name="username" ng-model="user.username" required>
<span ng-show="regForm.username.$touched && regForm.username.$invalid">
Username is required!
</span>
</p>
<p>
Email:
<input type="email" name="email" ng-model="user.email" required>
<span ng-show="regForm.email.$touched && regForm.email.$invalid">
Please enter a valid email!
</span>
</p>
<p>
Password:
<input type="password" name="password" ng-model="user.password" ng-minlength="6" required>
<span ng-show="regForm.password.$touched && regForm.password.$error.minlength">
Must be at least 6 characters!
</span>
</p>
<button ng-disabled="regForm.$invalid">Submit</button>
</form>
<h4>Live Preview:</h4>
<p>Username: {{ user.username }}</p>
<p>Email: {{ user.email }}</p>
</div>
<script>
angular.module('formApp', []).controller('formCtrl', function($scope) {
$scope.user = {};
});
</script>
</body>
</html>
-
AngularJS adds classes to fields:
-
.ng-invalid
,.ng-valid
-
.ng-touched
,.ng-untouched
-
.ng-dirty
,.ng-pristine
-
-
Use these in CSS to highlight fields as users interact!
- Sara: “Users see right away if something’s missing or wrong.”
- Lina (Designer): “The form looks and feels modern. Validation is part of the user experience!”
- Mike: “It’s less work for us, fewer bugs, and happier users.”
Add a “Confirm Password” field that only lets you submit if it matches the first password. Hint: Compare
user.password
anduser.confirm
in a custom validation message.
- AngularJS gives you live validation, user feedback, and robust form states—with almost no extra code.
- Use built-in directives and properties for instant, reliable validation.
- Smart, user-friendly forms are the key to a trustworthy app!
Lina: “Our forms help users every step of the way. Ready for even more—maybe routing or submitting data next?”
Try these examples in the [W3Schools AngularJS TryIt Editor](https://www.w3schools.com/angular/angular_tryit.asp?filename=tryangular_validation).