Template Driven Form Validation In Angular
In this article, We will learn how to validate forms using templates in Angular. When filling out a web form, it is highly typical for people to make mistakes. The validations are used at this point. The validation module must check that the user has filled out the form fields correctly and completely. We need to show the users the validation error messages and disable the submit button till the validation is complete. In this tutorial, we'll learn how to use the Angular built-in validators and how to manage validations in Template-driven forms in Angular.
Template-driven Form Validation
Validation directives are used to provide validation in Template-driven forms. There are various built-in validators in the Angular Forms Module. You can also make your own Validator from scratch.
Template
Consider the template-driven form below. It has form fields for firstname, lastname, email, gender, and isToc.
Add the following code under the app.componennt.html file.
<form contactform="" ngsubmit="" onsubmit=""> <p> <label>First Name </label> <input contact.firstname="" id="firstname" name="firstname" ngmodel="" type="text" /> </p> <p> <label>Last Name </label> <input contact.lastname="" id="lastname" name="lastname" ngmodel="" type="text" /> </p> <p> <label>email </label> <input contact.email="" id="email" name="email" ngmodel="" type="text" /> </p> <p> <label>Geneder </label> <input contact.gender="" id="gender" name="gender" ngmodel="" type="radio" value="male" /> Male <input contact.gender="" id="gender" name="gender" ngmodel="" type="radio" value="female" /> Female </p> <p> <label>Accept TOC</label> <input contact.istoc="" id="isToc" name="isToc" ngmodel="" type="checkbox" /> </p> <p> <button type="submit">Submit</button> </p> </form>
Add the following code under the app.componennt.ts file.
import { Component, ViewChild, ElementRef, OnInit } from '@angular/core'; import { NgForm } from '@angular/forms'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'Template driven forms'; @ViewChild('contactForm',null) contactForm: NgForm; contact:contact; ngOnInit() { this.contact = { firstname:"", lastname:"", gender:"male", isToc:true, email:"", }; } onSubmit() { console.log(this.contactForm.value); } } export class contact { firstname:string; lastname:string; gender:string; isToc:boolean; email:string; }
Disabling the Browser validation
<form contactform="" ngsubmit="" novalidate="" onsubmit="">
Built-in Validators
Adding in Built-in Validators
Required Validation
<input contact.firstname="" id="firstname" name="firstname" ngmodel="" required="" type="text" />
Minlength Validation
<input contact.firstname="" id="firstname" minlength="10" name="firstname" ngmodel="" required="" type="text" />
Maxlength Validation
<input contact.lastname="" id="lastname" maxlength="15" name="lastname" ngmodel="" required="" type="text" />
Pattern Validation
<input contact.lastname="" id="lastname" maxlength="15" name="lastname" ngmodel="" pattern="^[a-zA-Z]+$" required="" type="text" />
Email Validation
The control value for this Validator must be a genuine email address. This is what we do with the email field.<input contact.email="" email="" id="email" name="email" ngmodel="" required="" type="text" />
Disable Submit button
<button contactform.valid="" disabled="" type="submit">Submit</button>
Displaying the Validation/Error messages
<input contact.firstname="" firstname="ngModel" id="firstname" minlength="10" name="firstname" ngmodel="" required="" type="text" />
<div ngif="!firstname?.valid && (firstname?.dirty || firstname?.touched)"> Invalid First Name </div>
Why check dirty and touched?
Error message
<div ngif="!firstname?.valid && (firstname?.dirty || firstname?.touched)"> Invalid First Name <div ngif="firstname.errors.required"> First Name is required </div> <div ngif="firstname.errors.minlength"> First Name Minimum Length is {{firstname.errors.minlength?.requiredLength}} </div> </div>
<form #contactForm="ngForm" (ngSubmit)="onSubmit(contactForm)" novalidate>
<p>
<label>First Name </label>
<input contact.firstname="" firstname="ngModel" id="firstname" minlength="10" name="firstname" ngmodel="" required="" type="text" />
</p>
<div class="error" ngif="!firstname?.valid && (firstname?.dirty || firstname?.touched)">
<div ngif="firstname.errors.required">
First Name is required
</div>
<div ngif="firstname.errors.minlength">
First Name Minimum Length is {{firstname.errors.minlength?.requiredLength}}
</div>
</div>
<p>
<label>Last Name </label>
<input contact.lastname="" id="lastname" lastname="ngModel" maxlength="15" name="lastname" ngmodel="" pattern="^[a-zA-Z]+$" required="" type="text" />
</p>
<div class="error" ngif="!lastname?.valid && (lastname?.dirty || lastname?.touched)">
<div ngif="lastname.errors.required">
Last Name is required
</div>
<div ngif="lastname.errors.maxlength">
Last Name Minimum Length is {{lastname.errors.maxlength?.requiredLength}}
</div>
<div ngif="lastname.errors.pattern">
Only characters are allowed
</div>
</div>
<p>
<label>email </label>
<input contact.email="" email="ngModel" id="email" name="email" ngmodel="" required="" type="text" />
</p>
<div class="error" ngif="!email?.valid && (email?.dirty || email?.touched)">
<div ngif="email.errors.required">
Email is required
</div>
<div ngif="email.errors.email">
Invalid Email Address
</div>
</div>
<p>
<label>Geneder </label>
<input contact.gender="" gender="ngModel" id="gender" name="gender" ngmodel="" required="" type="radio" value="male" />
Male
<input contact.gender="" gender="ngModel" id="gender" name="gender" ngmodel="" required="" type="radio" value="female" /> Female
</p>
<div class="error" ngif="!gender?.valid && (gender?.dirty || gender?.touched)">
<div ngif="gender.errors.required">
Gender is required
</div>
</div>
<p>
<label>Accept TOC</label>
<input contact.istoc="" id="isToc" istoc="ngModel" name="isToc" ngmodel="" required="" type="checkbox" />
</p>
<div class="error" ngif="!isToc?.valid && (isToc?.dirty || isToc?.touched)">
<div ngif="isToc.errors.required">
Please accept the TOC
</div>
</div>
<p>
<button contactform.valid="" disabled="" type="submit">Submit</button>
</p>
<p>{{contactForm.valid}} </p>
</form>