Angularjs form validation - Every form needs validation to prevent our web application from malicious users. If our form doesn’t have a proper validation, that might be one of the main cause of security vulnerabilities.

Without proper validations expose our web application or website attacks such as SQL injections, header injections, cross-site scripting. In order to get valid data into the database and protect from bots, we need proper validations.

Here we go with AngularJs form Validations:

AngularJs offers client-side validation. AngularJs can observe the state of the form and input fields like input, textarea, select and lets you notify the user about the current state.

AngularJs can hold the information of where users have been touched or modified or not, and we can use standard HTML5 attributes to validate input or you can make your own validation functions.

AngularJs is continuously updating the state of both the form and the input fields.

For input fields we have the following states:

$invalid

It represents the field content is not valid

$valid

It represents the field content is valid

$untouched

It represents the field has not been touched yet

$touched

It represents the field has been touched

$pristine

It represents the field has not been modified yet

$dirty

It represents the field has been modified

All these are properties of the input field and all will give true or false as output. Based on true or false we have to write validation messages.

For forms we have the following states:

$pristine

It represents, No fields have been modified yet.

$dirty

It represents, One or more have been modified.

$invalid

It represents, The form content is not valid.

$valid

It represents, The form content is valid.

$submitted

It represents, The form is submitted.

All these are properties of the form and will give true or false as output.

Here we will develop a form called the customer with a few basic fields of the First name, Middle name, Last name, Email, Phone number, **Address, **and gender. In this blog, we will cover validation examples with these fields.

Required:

To specify that, the input field should be filled up means we have to use the required HTML5 attribute. In this example, field First Name is required.

<!DOCTYPE html>

<html>

<head>

   <meta charset='utf-8'>

   <meta http-equiv='X-UA-Compatible' content='IE=edge'>

   <title>Customer Form</title>

   <meta name='viewport' content='width=device-width, initial-scale=1'>

   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

</head>

<body>

   <div ng-app="myApp" ng-controller="formCtrl">

   <form name="myForm">

       <div class="row">

           <label>First Name:</label>

           <input type="text" ng-model="model.firstName" name="firstName" required>

           <div ng-if="myForm.firstName.$invalid && (myForm.firstName.$dirty || myForm.firstName.$touched )">

               <p style = "color:red" ng-if="myForm.firstName.$error.required"> First Name is required..</p>

           </div>

       </div>

   </form>

   </div>

     

   <script>

       var app = angular.module('myApp', []);

       app.controller('formCtrl', function($scope) {

           $scope.model = { };

           //$scope.model.firstName = "Chandra Sekhar";

       });

   </script>

</body>

</html>

Output:

We can also put validation for minimum length and maximum length in input by using minlength and maxlength as follows.

<form name="myForm">

   <div class="row">

       <label>First Name:</label>

       <input type="text" ng-model="model.firstName" name="firstName" minlength="3" maxlength="20" required>

       <div ng-if="myForm.firstName.$invalid && (myForm.firstName.$dirty || myForm.firstName.$touched )">

           <p style = "color:red" ng-if="myForm.firstName.$error.required"> First Name is required..</p>

           <p style ="color: red" ng-if="myForm.firstName.$error.minlength"> First Name should contain min 3 characters..</p>

           <p style ="color: red" ng-if="myForm.firstName.$error.maxlength"> First Name should not exceed 20 characters..</p>

       </div>

   </div>

</form>

Here I have given minlength as 3 and maxlength as 20. If we cross the length limits then alerts will fire.

Output:

Number Validation:

Now we will do validation for field Phone number. This field is required, minlengthand maxlength should 10.

<div class="form-group col-md-3">

<label>Mobile Number *</label>

<input type="number" class="form-control" ng-model="model.mobileNumber" name="mobileNumber" minlength="10" maxlength="10" required>

<div ng-if="myForm.mobileNumber.$invalid && (myForm.mobileNumber.$dirty || myForm.mobileNumber.$touched )">

   <p style = "color:red" ng-if="myForm.mobileNumber.$error.required" class="alert alert-danger"> Mobile Number is required..</p>

   <p style ="color: red" ng-if="myForm.mobileNumber.$error.minlength" class="alert alert-danger"> Mobile Number should contain min 10 characters..</p>

   <p style ="color: red" ng-if="myForm.mobileNumber.$error.maxlength" class="alert alert-danger"> Mobile Number should not exceed 10 characters..</p>

</div>

</div>

Output:

Email Validation:

We can do email validation by using input type email. Here, this field is required.

<div class="form-group col-md-3">

   <label>Email *</label>

   <input type="email" class="form-control" ng-model="model.emailId" name="emailId" required>

   <div ng-if="myForm.emailId.$invalid && (myForm.emailId.$dirty || myForm.emailId.$touched )">

       <p style = "color:red" ng-if="myForm.emailId.$error.required" class="alert alert-danger"> Email Id is required..</p>

       <p style="color: red" ng-if="myForm.emailId.$invalid" class="alert alert-danger"> Invalid Email Id</p>

   </div>

</div>

Output:

Note: All the above outputs of the above images are for validation firing and please don’t forget to combine all these codes, anyway I will provide the entire code below with all fields mentioned above.

Please find below code:

<!DOCTYPE html>
<html>
<head>
   <meta charset='utf-8'>
   <meta http-equiv='X-UA-Compatible' content='IE=edge'>
   <title> Customer Form </title>
   <meta name='viewport' content='width=device-width, initial-scale=1'>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
   <link rel="stylesheet" href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.min.css">
</head>
<body>
   <h2>Customer Form</h2>
   <br />
   <div ng-app="myApp" ng-controller="formCtrl">
       <div class="container">
           <form name="myForm">
               <div class="row">
                   <div class="form-group col-md-3">
                       <label>First Name *</label>
                       <input type="text" class="form-control" ng-model="model.firstName" name="firstName" minlength="3" maxlength="20" required>
                       <div ng-if="myForm.firstName.$invalid && (myForm.firstName.$dirty || myForm.firstName.$touched )">
                           <p style = "color:red" ng-if="myForm.firstName.$error.required" class="alert alert-danger"> First Name is required..</p>
                           <p style ="color: red" ng-if="myForm.firstName.$error.minlength" class="alert alert-danger"> First Name should contain min 3 characters..</p>
                           <p style ="color: red" ng-if="myForm.firstName.$error.maxlength" class="alert alert-danger"> First Name should not exceed 20 characters..</p>
                       </div>
                   </div>
                   <div class="form-group col-md-3">
                       <label>Middle Name</label>
                       <input type="text" class="form-control" ng-model="model.middleName" name="middleName" minlength="2" maxlength="20">
                       <div ng-if="myForm.middleName.$invalid && (myForm.middleName.$dirty || myForm.middleName.$touched )">
                           <p style ="color: red" ng-if="myForm.middleName.$error.minlength" class="alert alert-danger"> Middle Name should contain min 2 characters..</p>
                           <p style ="color: red" ng-if="myForm.middleName.$error.maxlength" class="alert alert-danger"> Middle Name should not exceed 20 characters..</p>
                       </div>
                   </div>
                   <div class="form-group col-md-3">
                       <label>Last Name *</label>
                       <input type="text" class="form-control" ng-model="model.lastName" name="lastName" minlength="3" maxlength="20" required>
                       <div ng-if="myForm.lastName.$invalid && (myForm.lastName.$dirty || myForm.lastName.$touched )">
                           <p style = "color:red" ng-if="myForm.lastName.$error.required" class="alert alert-danger"> Last Name is required..</p>
                           <p style ="color: red" ng-if="myForm.lastName.$error.minlength" class="alert alert-danger"> Last Name should contain min 3 characters..</p>
                           <p style ="color: red" ng-if="myForm.lastName.$error.maxlength" class="alert alert-danger"> Last Name should not exceed 20 characters..</p>
                       </div>
                   </div>
                   <div class="form-group col-md-3">
                       <label> Mobile Number * </label>
                       <input type="number" class="form-control" ng-model="model.mobileNumber" name="mobileNumber" minlength="10" maxlength="10" required>
                       <div ng-if="myForm.mobileNumber.$invalid && (myForm.mobileNumber.$dirty || myForm.mobileNumber.$touched )">
                           <p style = "color:red" ng-if="myForm.mobileNumber.$error.required" class="alert alert-danger"> Mobile Number is required..</p>
                           <p style ="color: red" ng-if="myForm.mobileNumber.$error.minlength" class="alert alert-danger"> Mobile Number should contain min 10 characters..</p>
                           <p style ="color: red" ng-if="myForm.mobileNumber.$error.maxlength" class="alert alert-danger"> Mobile Number should not exceed 10 characters..</p>
                       </div>
                   </div>
                   <div class="form-group col-md-3">
                       <label>Email *</label>
                       <input type="email" class="form-control" ng-model="model.emailId" name="emailId" required>
                       <div ng-if="myForm.emailId.$invalid && (myForm.emailId.$dirty || myForm.emailId.$touched )">
                           <p style = "color:red" ng-if="myForm.emailId.$error.required" class="alert alert-danger"> Email Id is required..</p>
                           <p style="color: red" ng-if="myForm.emailId.$invalid" class="alert alert-danger"> Invalid Email Id</p>
                       </div>
                   </div>
                   
               </div>
               <div class="col-md-3">
                       <button type="submit" class="btn btn-success">Submit</button>
                   </div>
           </form>
       </div>
   </div>
     
   <script>
       var app = angular.module('myApp', []);
       app.controller('formCtrl', function($scope) {
           $scope.model = { };
           //$scope.model.firstName = "Chandra sekhar";
       });
   </script>
</body>
</html>

Now, If you run this code in your machine, the entire form will look like below:

If all validations are fired, then our form will look like this.

If you’ve any doubts, please let us know through comment!!

#angular-js #angular

AngularJs Form Validation
2 Likes35.10 GEEK