Validation In Controls With Visual Interaction

Introduction

 
In this article, we will see how the form fields are validated by applying the appropriate class for visually indicating its validation. We can use our own class for giving the styles to apply to the form control or we can use the CSS framework provided by validation classes. We will use the predefined classes of bootstrap for applying the class for now.
 

Prerequisites

  • Knowledge of the basics of Angular class and components
 
Let’s see how these classes apply to our application by giving the necessary state.
 
Before we start to see how the classes are being applied, click here to create the sample application on which we will see how the classes are being applied.
 
Open app.component.html and modify the below set of HTML code for name control.
  1. <div class="form-group">  
  2.               <label for="name">Name:</label>  
  3.               <input type="text" #controlName='ngModel' required [class.is-invalid]="controlName.invalid" class="form-control" [(ngModel)]="studentModel.name" name="name">  
  4.             </div>  
Run the application and you will see that the field has been marked as required on the very initial load.
 
Validation In Controls With Visual Interaction
 
Look at the field ‘Name’; it has been marked as required. User must interact to the particular control before showing its valid/invalid/required kind of experience.
 
Modify the above control statements with below given HTML statement.
  1. <div class="form-group">  
  2.               <label for="name">Name:</label>  
  3.               <input type="text" #controlName='ngModel' required [class.is-invalid]="controlName.invalid && controlName.touched" class="form-control" [ngModel]="studentModel.name" name="name">  
  4.             </div>  
Now, run the application.
 
Validation In Controls With Visual Interaction
 
This now looks normal on initial. Once you click/touch and give invalid value or leave it blank, then only it will apply the invalid class to the control.
 
We can have a pattern matching validation. We can create any type of pattern matching that our form control must satisfy. Pattern matching can be applied for the phone number, pin code, password with at least 1 special char, and so on.
 
Let us take an example of a phone number that is having the pattern of 10 digits.
 
Change the phone section HTML code with the below given lines of code.
  1. <div class="col-sm-4">  
  2.           <div class="form-group">  
  3.               <label for="phone">Phone:</label>  
  4.               <input type="tel" #controlPhone="ngModel" required pattern="^\d{10}$" [class.is-invalid]="controlPhone.invalid && controlPhone.touched" class="form-control" [(ngModel)]="studentModel.phone" name="phone">  
  5.             </div>  
  6.       </div>  
Now, the validation is applied. Once you touch the control, the validation will only be successful if it is matching the pattern, i.e., 10 digits phone number is given.
 
Otherwise, the invalid class will be applied.
 
Validation In Controls With Visual Interaction
 
You can see the border is applied as red indicating invalid values for the desired control.
 
This is how we can define field validation in template-driven forms using the ngModel and class property that are applied conditionally.
Let us try to give the appropriate error messages if the user gives invalid value.
 
In our first field ‘Name’, the field is put as required. So, let us add an error message if found blank or invalid.
 
Add the below HTML statements for ‘Name’ control.
  1. <div class="form-group">  
  2.           <label for="name">Name:</label>  
  3.           <input type="text" #controlName='ngModel' required [class.is-invalid]="controlName.invalid && controlName.touched" class="form-control" [(ngModel)]="studentModel.name" name="name">  
  4.           <small [class.d-none]="controlName.valid || controlName.untouched"  "  class="text-danger">*Field is required</small>  
  5.         </div>  
Validation In Controls With Visual Interaction
 
Once you click and leave it blank, it will show you the proper validation message.
 
Validation In Controls With Visual Interaction
 
Let us add the validation message for the field ‘Phone’.
 
Change the below set of HTML statements for ‘phone’ control.
  1. <div class="form-group">  
  2.               <label for="phone">Phone:</label>  
  3.               <input type="tel" #controlPhone="ngModel" required pattern="^\d{10}$" [class.is-invalid]="controlPhone.invalid && controlPhone.touched" class="form-control" [(ngModel)]="studentModel.phone" name="phone">  
  4.               <small [class.d-none]="controlPhone.valid || controlPhone.untouched"  class="text-danger">*Must be 10 digit</small>  
  5.             </div>  
Run the application and click and give invalid value to phone control.
 
Validation In Controls With Visual Interaction
 
Let us try to use the error property on the ngModel.
 
Replace the code for ‘Phone’ Control.
  1. <div class="form-group">  
  2.               <label for="phone">Phone:</label>  
  3.               <input type="tel" #controlPhone="ngModel" required pattern="^\d{10}$" [class.is-invalid]="controlPhone.invalid && controlPhone.touched" class="form-control" [(ngModel)]="studentModel.phone" name="phone" />  
  4.               <div *ngIf="controlPhone.errors && (controlPhone.invalid || controlPhone.touched)">  
  5.                 <small *ngIf="controlPhone.errors.required" class="text-danger">*This field is required</small>  
  6.                 <small *ngIf="controlPhone.errors.pattern" class="text-danger">*Must be 10 digit</small>  
  7.               </div>                
  8.             </div>  
We can use ngModel name to check whether errors are occurred or not. Also, we can go and check the type of error that occurred.
 
In above code statements, you can see we have done the check for two types of error (required and pattern) that will be applied based on the type of error.
 
Validation In Controls With Visual Interaction
 
The error message will only be rendered once the error occurred.
 

Validation message in Select control

 
We will use ngModel property to apply the class and display validation message if the user is not selecting the value from the select control.
 
Just apply the html code below,
  1. <div class="form-group">  
  2.                 <label for="bloodGroup">Blood Group:</label>  
  3.                 <select required #bloodGroup="ngModel" value="Select" class="form-control" [class.is-invalid]="bloodGroup.invalid && bloodGroup.touched" [(ngModel)]="studentModel.bloodGroup" name="bloodGroup">  
  4.                   <option value="">Select Blood Group</option>  
  5.                   <option *ngFor="let group of groups" >{{group}}</option>  
  6.                 </select>  
  7.               <small class="text-danger" *ngIf="bloodGroup.invalid && bloodGroup.touched">*Please select Blood group</small>  
  8.               </div>  
This works the same and the validation will work as below if we are not selecting the appropriate value,
 
Validation In Controls With Visual Interaction
 
This is how you can apply the invalid class if the select control is touched and you're not selecting a valid value from it.


Similar Articles