Introduction
As a web developer creating a form is a very common task for us. AngularJS enhances the user experience and validation. AngularJS makes two-way binding easy, in other words bind data from a model object ($scope) to form an input control and vice-versa.
In the following example we have created a simple form with one field and bound it with User.firstName.
- <body ng-app="myapp">
- <div ng-controller="MyFromController" >
- <form>
- <table border="1px solid">
- <tr>
- <td>First name :</td>
- <td>
- <input type="text" name="firstName" ng-model="User.firstName">
- </td>
- </tr>
- </table>
- </form>
- <div>
- Hello {{User.firstName}} !!!
- </div>
- </div>
- <script>
- angular.module("myapp", [])
- .controller("MyFromController", function ($scope)
- {
- $scope.User = {};
- $scope.User.firstName = "Ratnesh";
- });
- </script>
- </body>
Now we will add some more fields of various types to our form to enter employee details. Here we have added a radio for gender as:
- <input type="radio" ng-model="User.Gender" value="Male">Male
- <input type="radio" ng-model="User.Gender" value="Female">Female
One select box for “position” as:
- <select ng-model="User.Position" ng-options="obj.id as obj.name for obj in Positions">
- <option value="">--Select--</option>
- </select>
One Multi select box for Languages as:
- <select multiple="true" ng-model="User.Languages" ng-options="obj.id as obj.name for obj in Languages"></select>
We have also added some input fields for Age, email and so on. We will use them to understand the validation later in this article. So our form looks such as:
ValidationAngularJS comes with a set of validation directives. It validates the form's control value before assigning to the $scope properties. If any control's value is invalid, it is not assigned into the $scope and the corresponding $scope property is cleared.
Some of the validation directives that we will use in our employee details page are as follows.
- ng-minlength to validate the length of the data entered
- ng-maxlength to validate the length of the data entered
- ng-pattern to validate the value of an input field against a regular expression
- ng-required to make a field requuired
We have set our validation rule for each control. So our HTML file would look as:
- <!DOCTYPE html>
- <html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
- <style type="text/css">
- table.myTable { border-collapse:collapse; }
- table.myTable td, table.myTable th { border:1px solid black;padding:2px; }
- </style>
- </head>
- <body ng-app="myapp">
- <div ng-controller="MyFromController" >
- <form>
- <table class="myTable">
- <tr>
- <td colspan=3>
- <h3>Employee Details</h3>
- </td>
- </tr>
- <tr>
- <td>Emp ID :</td>
- <td>
- <input name="EmployeeID" ng-model="User.EmpID" required="">
- </td>
- <td></td>
- </tr>
- <tr>
- <td>First name :</td>
- <td>
- <input type="text" name="firstName" ng-model="User.firstName" required ng-minlength="5" ng-maxlength="8">
- </td>
- <td></td>
- </tr>
- <tr>
- <td>Last name :</td>
- <td>
- <input type="text" name="lastName" ng-model="User.lastName" ng-minlength="5" ng-maxlength="8">
- </td>
- <td></td>
- </tr>
- <tr>
- <td>Age :</td>
- <td>
- <input type="text" name="age" ng-model="user.age" type="number"/>
- </td>
- <td></td>
- </tr>
- <tr>
- <td>E-mail :</td>
- <td>
- <input name="email" ng-model="User.Email" type="email">
- </td>
- <td></td>
- </tr>
- <tr>
- <td>Gender :</td>
- <td>
- <input type="radio" ng-model="User.Gender" value="Male" >Male
- <input type="radio" ng-model="User.Gender" value="Female">Female
- </td>
- <td></td>
- </tr>
- <tr>
- <td>Position :</td>
- <td>
- <select ng-model="User.Position" ng-options="obj.id as obj.name for obj in Positions">
- <option value="">--Select--</option>
- </select>
- </td>
- <td></td>
- </tr>
- <tr>
- <td>Languages :</td>
- <td>
- <select multiple="true" ng-model="User.Languages" ng-options="obj.id as obj.name for obj in Languages"></select>
- </td>
- <td></td>
- </tr>
- <tr>
- <td></td>
- <td></td>
- <td>
- <input type="submit" ng-click="update(User)" value="Save" />
- </td>
- </tr>
- </table>
- </form>
- </div>
- <script>
- angular.module("myapp", [])
- .controller("MyFromController", function ($scope) {
- $scope.User = {};
- $scope.Positions = [
- { id: "1", name: "Developer" }
- , { id: "2", name: "Team Lead" }
- , { id: "3", name: "Project Manager" }
- ];
- $scope.Languages = [
- { id: "1", name: "C#" }
- , { id: "2", name: "Vb.Net" }
- , { id: "3", name: "Java" }
- ];
-
- $scope.update = function (user) {
- alert(JSON.stringify(user));
- };
-
-
- });
- </script>
- </body>
- </html>
If we submit our form with all valid values then we would get an alert that shows all the values in a JSON string as:
Now change the values for some field as invalid and submit as in the following:
We can see that all the invalid values and properties have been removed from the user object by Angular. Until now we have not shown any message to our form user about validation. For this we need to use form and control state. If we had added a name attribute to our form then it would be added to $scope as a property. In our case we would add “EmpForm” as name. Now we access the model property with the form name. For example we can access the firstName as:
We have changed the form tag as:
- <form name="EmpForm" ng-submit="submit()" novalidate>
And defined a submit as:
- $scope.submit = function() { $scope.submitted = true; };
We have written some code to show a custom message for first name as follows:
- <tr>
- <td>First name :</td>
- <td>
- <input type="text" name="firstName" ng-model="User.firstName" required ng-minlength="5" ng-maxlength="8">
- </td>
- <td>
- <span ng-if="submitted">
- <span ng-if="EmpForm.firstName.$error.required">Required</span>
- <span ng-if="EmpForm.firstName.$error.minlength">should be more than 5</span>
- <span ng-if="EmpForm.firstName.$error.maxlength">should be less than 8</span>
- </span>
- </td>
- </tr>
This code will show a message as “Required” if the user submits the form with a blank first name. If the user violates length in either side then he would get a different message.