We now have a youtube channel. Subscribe!

AngularJS | Forms

AngularJS Forms


Hello folks! welcome back to a new edition of our tutorial on AngularJS. In this tutorial, we will be studying about AngularJS Forms.

AngularJS enhances filling and validation of forms. We can make use of ng-click event to handle the click button and utilize $dirty and $invalid flags to do validation in a seamless manner. Make use of novalidate with a form declaration to disable any browser-specific validation. The form controls heavily utilizes AngularJS events. Let's take a look at some of the events.

AngularJS Events

AngularJS makes available multiple events that are associated with the HTML controls. For instance, the ng-click directive is mainly associated with a button. Following are the events supported in AngularJS -

  • ng-click
  • ng-dbl-click
  • ng-change
  • ng-keydown
  • ng-keyup
  • ng-keypress
  • ng-mousenter
  • ng-mouseleave
  • ng-mousedown
  • ng-mouseup
  • ng-mousemove
  • ng-mouseover
  • ng-keypress

ng-click Event

Reset data of a form using on-click directive of a button.

<input name = "firstname" type = "text" ng-model = "firstName" required>
<input name = "lastname" type = "text" ng-model = "lastName" required>
<input name = "email" type = "email" ng-model = "email" required>
<button ng-click = "reset()">Reset</button>

<script>
   function studentController($scope) { 
      $scope.reset = function() {
         $scope.firstName = "Kennedy";
         $scope.lastName = "Nkpara";
         $scope.email = "KennedyNkpara@webdesigntutorialz.com";
      }   
      
      $scope.reset();
   }
</script>


Validate Data

The following can be used to track error -

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

Example

The following example below illustrates all the above mentioned directives -

Create a file testAngularJS.html.

<html>
   <head>
      <title>Angular JS Forms</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      
      <style>
         table, th , td {
            border: 1px solid grey;
            border-collapse: collapse;
            padding: 5px;
         }
         table tr:nth-child(odd) {
            background-color: #f2f2f2;
         }
         table tr:nth-child(even) {
            background-color: #ffffff;
         }
      </style>
      
   </head>
   <body>
      
      <h2>AngularJS Sample Application</h2>
      <div ng-app = "mainApp" ng-controller = "studentController">
         
         <form name = "studentForm" novalidate>
            <table border = "0">
               <tr>
                  <td>Enter first name:</td>
                  <td><input name = "firstname" type = "text" ng-model = "firstName" required>
                     <span style = "color:red" ng-show = "studentForm.firstname.$dirty && studentForm.firstname.$invalid">
                        <span ng-show = "studentForm.firstname.$error.required">First Name is required.</span>
                     </span>
                  </td>
               </tr>
               
               <tr>
                  <td>Enter last name: </td>
                  <td><input name = "lastname"  type = "text" ng-model = "lastName" required>
                     <span style = "color:red" ng-show = "studentForm.lastname.$dirty && studentForm.lastname.$invalid">
                        <span ng-show = "studentForm.lastname.$error.required">Last Name is required.</span>
                     </span>
                  </td>
               </tr>
               
               <tr>
                  <td>Email: </td><td><input name = "email" type = "email" ng-model = "email" length = "100" required>
                     <span style = "color:red" ng-show = "studentForm.email.$dirty && studentForm.email.$invalid">
                        <span ng-show = "studentForm.email.$error.required">Email is required.</span>
                        <span ng-show = "studentForm.email.$error.email">Invalid email address.</span>
                     </span>
                  </td>
               </tr>
               
               <tr>
                  <td>
                     <button ng-click = "reset()">Reset</button>
                  </td>
                  <td>
                     <button ng-disabled = "studentForm.firstname.$dirty &&
                        studentForm.firstname.$invalid || studentForm.lastname.$dirty &&
                        studentForm.lastname.$invalid || studentForm.email.$dirty &&
                        studentForm.email.$invalid" ng-click="submit()">Submit</button>
                  </td>
               </tr>
					
            </table>
         </form>
      </div>
      
      <script>
         var mainApp = angular.module("mainApp", []);
         
         mainApp.controller('studentController', function($scope) {
            $scope.reset = function() {
               $scope.firstName = "Kennedy";
               $scope.lastName = "Nkpara";
               $scope.email = "KennedyNkpara@webdesigntutorialz.com";
            }
            
            $scope.reset();
         });
      </script>
      
   </body>
</html>

Output

Open the file testAngularJS.html in any web browser to see the result -

AngularJS Sample Application

Enter first name:
Enter last name:
Email:


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about AngularJS Includes.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain