JavaScript - Simple Form Validation Using AngularJS

In this tutorial we will create a Simple Form Validation Using AngularJS. This code will automatically validate any entered characters in the form when applied. The code itself use a angular directives that has a built-in function to validate a form field by the use of ng-show, just adding a specific parameter in the form in can maximize the validation proccess. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download Bootstrap, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body ng-controller="myController">
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">JavaScript - Simple Form Validation Using AngularJS</h3>
  16.                 <hr style="border-top:1px dotted #ccc;" />
  17.                 <div class="col-md-2"></div>
  18.                 <div class="col-md-8">
  19.                         <form name="signupfrm">
  20.                                 <div class="form-group">
  21.                                         <label>Firstname</label>
  22.                                         <input type="text" class="form-control" name="firstname" ng-model="member.firstname" required="required"/>
  23.                                         <center><span class="text-success" ng-show="signupfrm.firstname.$error.required">Required Field</span></center>
  24.                                 </div>
  25.                                 <div class="form-group">
  26.                                         <label>Lastname</label>
  27.                                         <input type="text" class="form-control" name="lastname" ng-model="member.lastname" required="required"/>
  28.                                                 <center><span class="text-success" ng-show="signupfrm.lastname.$error.required">Required Field</span></center>
  29.                                 </div>
  30.                                 <div class="form-group">
  31.                                         <label>Username</label>
  32.                                         <input type="text" class="form-control" name="username" ng-model="member.username" required="required"/>
  33.                                                 <center><span class="text-success" ng-show="signupfrm.username.$error.required">Required Field</span></center>
  34.                                 </div>
  35.                                 <div class = "form-group">
  36.                                         <label>Password</label>
  37.                                         <input type="password" class="form-control" name="password" ng-model="member.password" ng-minlength="8"  required="required"/>
  38.                                                 <center><span class = "text-success" ng-show="signupfrm.password.$error.required">Required Field</span></center>
  39.                                                 <center><span class = "text-danger" ng-show="signupfrm.password.$error.minlength">Password Too Short</span></center>
  40.                                 </div>
  41.                                 <br />
  42.                                 <center><button class="btn btn-primary" ng-click="register();" ng-disabled="signupfrm.$invalid"><span class = "glyphicon glyphicon-save"></span> Register</button></center>
  43.                         </form>
  44.                 </div> 
  45.         </div>
  46. <script src="js/angular.js"></script>
  47. <script src="js/script.js"></script>
  48. </body>
  49. </html>

Creating the Main Function

This code contains the main function of the application. This code will automatically validate a form field when entered. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js.
  1. var app = angular.module("myModule", [])
  2.                                                             .controller("myController", function($scope){
  3.                                                                         $scope.register = function(){
  4.                                                                                 alert("Successfully register");
  5.                                                                                 window.location="index.html";
  6.                                                                         }
  7.                                                                 });
There you have it we successfully created a Simple Form Validation using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment