AngularJS Form

Creating a form and submitting a form is the most important part of any web application. Form allows programmers to get the feedback from the user. Forms are also used to get the user information. But the important point here is that if the information submitted by the user is in the proper format or not. So, we need to check (validate) each and every field submitted by a user.

The aim of this section is to set some validations on the user input value and display an error message on the wrong input.

HTML Code

  1. <div ng-app="mainApp" ng-controller="studentController">  
  2.     <form name="studentForm" novalidate class="form">  
  3.         <h2 style="margin-left:75px;"><u>Angular JS Form</u></h2>  
  4.         <table border="0">  
  5.             <tr>  
  6.                 <td>Enter your first name:</td>  
  7.                 <td> <input name="firstname" type="text" ng-model="firstName" required> <span style="color:red" ng-show="studentForm.firstname.$dirty &&  
  8. studentForm.firstname.$invalid">  
  9. <span ng-show="studentForm.firstname.$error.required">  
  10. First Name is required.</span> </span>  
  11.                 </td>  
  12.             </tr>  
  13.             <tr>  
  14.                 <td>Enter your last name: </td>  
  15.                 <td> <input name="lastname" type="text" ng-model="lastName" required> <span style="color:red" ng-show="studentForm.lastname.$dirty  
  16. && studentForm.lastname.$invalid">  
  17. <span ng-show="studentForm.lastname.$error.required">  
  18. Last Name is required.</span> </span>  
  19.                 </td>  
  20.             </tr>  
  21.             <tr>  
  22.                 <td>Enter your email id: </td>  
  23.                 <td> <input name="email" type="email" ng-model="email" length="100" required> <span style="color:red" ng-show="studentForm.email.$dirty &&  
  24. studentForm.email.$invalid">  
  25. <span ng-show="studentForm.email.$error.required">  
  26. Email is required.</span> <span ng-show="studentForm.email.$error.email">  
  27. Invalid email address.</span> </span>  
  28.                 </td>  
  29.             </tr>  
  30.             <tr>  
  31.                 <td colspan="2"> <button ng-disabled="studentForm.firstname.$dirty &&  
  32. studentForm.firstname.$invalid || studentForm.lastname.$dirty &&  
  33. studentForm.lastname.$invalid || studentForm.email.$dirty &&  
  34. studentForm.email.$invalid" ng-click="submit()" class="btn">  
  35. Submit  
  36. </button> <button ng-click="reset()">Reset</button> </td>  
  37.             </tr>  
  38.         </table>  
Script.js
  1. <script>  
  2.     var mainApp = angular.module("mainApp", []);  
  3.     mainApp.controller('studentController'function($scope) {  
  4.         $scope.reset = function() {  
  5.             $scope.firstName = "";  
  6.             $scope.lastName = "";  
  7.             $scope.email = "";  
  8.         }  
  9.         $scope.reset();  
  10.         $scope.submit = function() {  
  11.             var firstName = $scope.firstName;  
  12.             var lastName = $scope.lastName;  
  13.             var email = $scope.email;  
  14.             alert('Information Submitted by You:\n First Name: ' + firstName + '\n Last Name: ' + lastName + '\n Email Id: ' + email);  
  15.         }  
  16.     });  
  17. </script>  
CSS
  1. <style>  
  2.     table,  
  3.     th,  
  4.     td {  
  5.         border: 1px solid grey;  
  6.         border-collapse: collapse;  
  7.         padding: 5px;  
  8.     }  
  9.   
  10.     table tr:nth-child(odd) {  
  11.         background-color: #98c7ba;  
  12.     }  
  13.   
  14.     table tr:nth-child(even) {  
  15.         background-color: #ffffff;  
  16.     }  
  17.   
  18.     .form {  
  19.         margin-left: 500px;  
  20.         margin-top: 100px;  
  21.     }  
  22.   
  23.     .btn {  
  24.         margin-left: 95px;  
  25.     }  
  26. </style>  

 

Output

Explanation of validations states

  • $dirty: True if the user has already interacted with the form.
  • $error: It states the exact error.
  • $Invalid: It states that the entered value is invalid

There are many more validation states available in AngularJS.

  • $untouched
  • $touched
  • $pristine
  • $valid
  • $submitted

Note

All these validation states are of Boolean Type no matter if the value is True or False.

You can also download the complete source project. For more information about AngularJS, visit this blog.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now