AngularJS Form Validation

Getting Started

I've used CDN for Bootstrap and Angular JS in this tutorial so you need internet connection for them to work.

Form and Input State

This our the states in Angular JS.
  • $untouched The field has not been touched yet
  • $touched The field has been touched
  • $pristine The field has not been modified yet
  • $dirty The field has been modified
  • $invalid The field content is not valid
  • $valid The field content is valid
Reference: w3schools.com

Validation Rules used in this tutorial

  • email - The input should in email format
  • url - The input should be a url
  • required - The input field should not be empty
  • ng-minlength - Input must be greater than or equal to the set value
  • ng-maxlength - Input must be less than or equal to the set value
  • ng-pattern - The input should look like the set pattern (in this case of this tutorial, the input should follow to set REGEX format)

index.html

This is our index that contains our form and validations.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="app">
  3.     <meta charset="utf-8">
  4.     <title>AngularJS Form Validation</title>
  5.     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  7.     <style type="text/css">
  8.         .errortext{
  9.                 color:red;
  10.         }
  11.     </style>
  12. </head>
  13. <body ng-controller="myController">
  14. <div class="container">
  15.     <h1 class="page-header text-center">AngularJS Form Validation</h1>
  16.     <div class="col-md-6 col-md-offset-3">
  17.         <div class="login-panel panel panel-primary">
  18.                 <div class="panel-heading">
  19.                     <h3 class="panel-title"> Validation Form</h3>
  20.                 </div>
  21.                 <div class="panel-body">
  22.                         <form role="form" name="myForm" novalidate>
  23.                         <fieldset>
  24.                                 <div class="form-group">
  25.                                         <input type="text" class="form-control" name="username" placeholder="Username" ng-minlength="10" ng-maxlength="30" ng-pattern="/^[a-zA-Z0-9_]*$/" ng-model="user.username" required autofocus>
  26.                                         <div class="errortext" ng-show="myForm.username.$dirty && myForm.username.$invalid">
  27.                                                 <span ng-show="myForm.username.$error.required">Username is required</span>
  28.                                                 <span ng-show="myForm.username.$error.minlength">Username must contain atleast 10 characters</span>
  29.                                                 <span ng-show="myForm.username.$error.maxlength">Username should not be greater than 30 characters</span>
  30.                                                 <span ng-show="myForm.username.$error.pattern && !myForm.username.$error.minlength && !myForm.username.$error.maxlength">Only letters, numbers and underscore allowed</span>
  31.                                         </div>
  32.                                         </div>
  33.                                 <div class="form-group">
  34.                                         <input type="password" class="form-control" name="password" placeholder="Password" ng-model="user.password" required>
  35.                                         <div class="errortext" ng-show="myForm.password.$dirty && myForm.password.$invalid">
  36.                                                 <span ng-show="myForm.password.$error.required">Password is required</span>
  37.                                         </div>
  38.                                         </div>
  39.                                         <div class="form-group">
  40.                                         <input type="password" class="form-control" name="repassword" placeholder="Re-type Password" ng-model="user.repassword" required>
  41.                                         <div class="errortext" ng-show="myForm.repassword.$dirty && myForm.repassword.$invalid || myForm.repassword.$dirty && user.repassword != user.password">
  42.                                                 <span ng-show="myForm.repassword.$error.required">Re-type password is required</span>
  43.                                                 <span ng-show="!myForm.repassword.$error.required && user.repassword != user.password">Password did not match</span>
  44.                                         </div>
  45.                                         </div>
  46.                                         <div class="form-group">
  47.                                         <input  type="email" class="form-control" name="email" placeholder="Email" ng-model="user.email" required>
  48.                                         <div class="errortext" ng-show="myForm.email.$dirty && myForm.email.$invalid">
  49.                                                 <span ng-show="myForm.email.$error.required">Email is required</span>
  50.                                                 <span ng-show="myForm.email.$error.email">Invalid email format</span>
  51.                                         </div>
  52.                                     </div>
  53.                                     <div class="form-group">
  54.                                         <input type="url" class="form-control" name="url" placeholder="Website" ng-model="user.website" required>
  55.                                          <div class="errortext" ng-show="myForm.url.$dirty && myForm.url.$invalid">
  56.                                                 <span ng-show="myForm.url.$error.required">Website is required</span>
  57.                                                 <span ng-show="myForm.url.$error.url">Input a valid website</span>
  58.                                         </div>
  59.                                     </div>
  60.                                     <div class="form-group">
  61.                                         <input type="text" class="form-control" name="firstname" placeholder="Firstname" ng-model="user.firstname" required>
  62.                                         <div class="errortext" ng-show="myForm.firstname.$dirty && myForm.firstname.$invalid">
  63.                                                 <span ng-show="myForm.firstname.$error.required">Firstname is required</span>
  64.                                         </div>
  65.                                     </div>
  66.                                     <div class="form-group">
  67.                                         <input  type="text" class="form-control" name="lastname" placeholder="Lastname" ng-model="user.lastname" required>
  68.                                          <div class="errortext" ng-show="myForm.lastname.$dirty && myForm.lastname.$invalid">
  69.                                                 <span ng-show="myForm.lastname.$error.required">Lastname is required</span>
  70.                                         </div>
  71.                                     </div>
  72.                                 <button type="button" class="btn btn-lg btn-primary btn-block" ng-disabled="myForm.$invalid || user.repassword != user.password" ng-click="submit()"><span class="glyphicon glyphicon-check"></span> Validate</button>
  73.                         </fieldset>
  74.                         </form>
  75.                 </div>
  76.             </div>
  77.                 <div class="alert alert-success text-center" ng-show="valid">
  78.                         <button type="button" class="close" ng-click="close()"><span aria-hidden="true">&times;</span></button>
  79.                         <span class="glyphicon glyphicon-check"></span> Form Validated
  80.                 </div>
  81.         </div>
  82. </div>
  83. <script src="angular.js"></script>
  84. </body>
  85. </html>

angular.js

This contains our angular js scripts.
  1. var app = angular.module('app', []);
  2. app.controller('myController', function($scope){
  3.         $scope.valid = false;
  4.         $scope.submit = function(){
  5.                 $scope.valid = true;
  6.         }
  7.         $scope.close = function(){
  8.                 $scope.valid = false;
  9.         }
  10. });
That ends this tutorial. Happy Coding :)

Add new comment