All for Joomla All for Webmasters

AngularJS Form Validation

AngularJS provides client-side form validation. It checks the state of the form and input fields (input, textarea, select), and lets you notify the user about the current state.

It also holds the information about whether the input fields have been touched, or modified, or not.

Following directives are generally used to track errors in an AngularJS form:

  • $dirty – states that value has been changed.
  • $invalid – states that value entered is invalid.
  • $error – states the exact error.

AngularJS Form Validation Example

  1. <!DOCTYPE html>
  2.  <html>
  3.    <head>
  4.       <title>Angular JS Forms</title>
  5.       <script src = “http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”></script>
  6.  <style>
  7.          table, th , td {
  8.             border: 1px solid grey;
  9.             border-collapse: collapse;
  10.             padding: 5px;
  11.          }
  12.          table tr:nth-child(odd) {
  13.             background-color: lightpink;
  14.          }
  15.          table tr:nth-child(even) {
  16.             background-color: lightyellow;
  17.          }
  18.       </style>
  19.    </head>
  20.    <body>
  21.       <h2>AngularJS Sample Application</h2>
  22.       <div ng-app = “mainApp” ng-controller = “studentController”>
  23.          <form name = “studentForm” novalidate>
  24.             <table border = “0”>
  25.                <tr>
  26.                   <td>Enter first name:</td>
  27.                   <td><input name = “firstname” type = “text” ng-model = “firstName” required>
  28.                      <span style = “color:red” ng-show = “studentForm.firstname.$dirty && studentForm.firstname.$invalid”>
  29.                         <span ng-show = “studentForm.firstname.$error.required”>First Name is required.</span>
  30.                      </span>
  31.                   </td>
  32.                </tr>
  33.                <tr>
  34.                   <td>Enter last name: </td>
  35.                   <td><input name = “lastname”  type = “text” ng-model = “lastName” required>
  36.                      <span style = “color:red” ng-show = “studentForm.lastname.$dirty && studentForm.lastname.$invalid”>
  37.                         <span ng-show = “studentForm.lastname.$error.required”>Last Name is required.</span>
  38.                      </span>
  39.                   </td>
  40.                </tr>
  41.                <tr>
  42.                   <td>Email: </td><td><input name = “email” type = “email” ng-model = “email” length = “100” required>
  43.                      <span style = “color:red” ng-show = “studentForm.email.$dirty && studentForm.email.$invalid”>
  44.                         <span ng-show = “studentForm.email.$error.required”>Email is required.</span>
  45.                         <span ng-show = “studentForm.email.$error.email”>Invalid email address.</span>
  46.                      </span>
  47.                   </td>
  48.                </tr>
  49.                 <tr>
  50.                   <td>
  51.                      <button ng-click = “reset()”>Reset</button>
  52.                   </td>
  53.                   <td>
  54.                      <button ng-disabled = “studentForm.firstname.$dirty &&
  55.                         studentForm.firstname.$invalid || studentForm.lastname.$dirty &&
  56.                         studentForm.lastname.$invalid || studentForm.email.$dirty &&
  57.                         studentForm.email.$invalid” ng-click=“submit()”>Submit</button>
  58.                   </td>
  59.                </tr>
  60.      </table>
  61.          </form>
  62.       </div>
  63.         <script>
  64.          var mainApp = angular.module(“mainApp”, []);
  65.          mainApp.controller(‘studentController’, function($scope) {
  66.             $scope.reset = function(){
  67.                $scope.firstName = “make”;
  68.                $scope.lastName = “for”;
  69.                $scope.email = “info@makeforsure.com”;
  70.             }
  71.              $scope.reset();
  72.          });
  73.       </script>
  74.      </body>
  75. </html>
PinIt
submit to reddit

Leave a Reply

Top