Introduction to AngularJS Validation
AngularJS offers client-side form validation. We use AngularJS as a frontend framework that is very scalable and easy to code and developed our single-page angular web application. Apart from this the AngularJS validation also provides us some more exciting features like form validation which validates the user input to the client-side only without sending them to the backend server for validation. Also, it reduces our backend code and complexity for code maintaining. In angular, we have form validation and we can also create custom validation as well.
States of AngularJS Validation
AngularJS provides us form validation to check user input data. This is used as a client-side validation before submitting them to the server. AngularJS keeps track of all the input fields we provide users to enter their data it can be anything like select from the drop-down, input field, text-area, etc. If we have done any kind of modification on the user field it keeps notify the user about the current status of validation, it also keeps track of files that are modified, have been touched or not.
So in angular js, we have divided the states into two parts. They are specific to form and fields.
1. Input Filed State
As we provide different input fields into the form so they have a different state which is as following and all these fields have the value as Boolean true or false:
- $dirty: This state shows that the filed is modified or not.
- $untouched: This state is basically used to show that the current form filed is touched or not.
- $valid: This file is important because it shows that the input data we have provided is valid according to the input asked to enter.
- $touched: It simple used to show the field is touched.
- $invalid: This file is used to show that the input we have provided is invalid according to the input we have asked to enter.
- $pristine: This uses to show that we have not done anything on the filed yet means has not modified by the user.
2. Form State
The form state which is as follows now all the state will apply for whole form fields, not for specific one or two:
- $valid: Apply to the whole form used to show fields are valid.
- $dirty: Used to show more than one field is modified or not.
- $submitted: This state is used to show that the form is submitted.
- $pristine: Apply to the whole form to show more than one filed has not modified yet.
- $invalid: This is used to show that the form fields are invalid corresponding to the input asked to enter.
Similarly, all these form states also have Boolean values true or false. These states we can use to show some messages to the user or we can say error messages or success messages.
Following is an example to Show Validation:
Code:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<p>Now we will leave this first field to be blank to see validation</p>
<form name="myForm">
<p>Name of user:
<input name="myName1" ng-model="myName1" required>
<span ng-show="myForm.myName1.$touched && myForm.myName1.$invalid">User name cannot be blank.</span>
</p>
<p>Address of user:
<input name="myAddress1" ng-model="myAddress1" required>
</p>
</form>
<p>Here we are using ngshow to hide and show the error message if the user has touched the filed.
</p>
</body>
</html>
Output:
Examples of AngularJS Validation
In angular, we have some in-build validation like email and required which can directly be applied to the fields to validate them.
Example #1
Email Validation.
If we have user input that contains the email of the user, but we need to verify the input data so we can directly make this filed as type email. it is the feature of HTML5. below is one example to show how to use this.
Code:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<p>Please provide your email address into the input field :</p>
<form name="myForm1">
<input type="email" name="myInput" ng-model="myInput">
</form>
<p>State is :</p>
<h1>{{myForm1.myInput.$valid}}</h1>
<p>By default email state is true but once we start writing it will start validating the data whether it is valid or not.</p>
</body>
</html>
Output:
When we provide correct email format it will show below output:
Example #2
Required Validation.
This is also an HTML5 feature that makes the filed as required. below is a simple example to show how to use this.
Code:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<p>Please provide user name into the field:</p>
<form name="myForm1">
<input name="myInput1" ng-model="myInput1" required>
</form>
<p>State is :</p>
<h1>{{myForm1.myInput1.$valid}}</h1>
</body>
</html>
Output:
When the input field will remain empty it will show below output:
We can also have custom validation, which means we can build our own validation according to the requirement. But that is not easy we need to make so many things that make the code tricky to understand. For this we need to add a new directive to our application as follows.
Example #3
Custom Validation.
Code:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
<p>Please provide details ::</p>
<form name="myForm">
<input name="myInput" ng-model="myInput" required my-directive1>
</form>
<p>State is:</p>
<h1>{{myForm.myInput.$valid}}</h1>
<script>
var app = angular.module('myApp', []);
app.directive('myDirective1', function() {
return {
require: 'ngModel',
link: function(scope, element, attr, mCtrl) {
function myValidation1(value) {
if (value.indexOf("a") > -1) {
mCtrl.$setValidity('charE', true);
} else {
mCtrl.$setValidity('charE', false);
}
return value;
}
mCtrl.$parsers.push(myValidation1);
}
};
});
</script>
<p>Our input must contain the character 'a' to make this input filed valid.</p>
</body>
</html>
Output:
Here we adding a new directive named mydirective1. It has some naming convection should be camel case, but when we try to call the directive it should be separated by ‘-‘. We are also returning an object to ngModel. Now we will make one function that takes argument according to requirement and here mCTRL1 is the ngModelCotroller. Now test the input by providing some value wrong and right it will return true or false based on the value passed.
Conclusion
So AngularJS validation provides us client-side validation before submitting it to the back end. So it reduces some backend calls to improve performance. Also, we can create custom validations as well by creating a directive in AngularJS and link them with the controller and provide logic in functions.
Recommended Articles
This is a guide to AngularJS Validation. Here we discuss the introduction and states of AngularJS validation along with different examples and code implementation. You may also look at the following articles to learn more –