Reactive Form Validation Example in Angular 12

In this Angular tutorial, we will learn how to add Reactive Form validation in the angular app.

This Angular tutorial is compatible with version 4+ including latest version 12, 11, 10, 9, 8 ,7, 6 & 5.

In addition, we will get to know how to add form controls in the application and validate them on submit.

How to add validation in Reactive Form in the Angular app

We simply need to follow the given steps for implementing the lazy loading image in Angular 12 app


Step 1 is to Create the New Angular App

Step 2 is to Import the Form Module 

Step 3 is to Add Code on View File

Step 4 is to Use Component ts File

Step 5 is to Start the Angular App

Now, let’s learn in detail

Step 1- Create the New Angular App

First of all, open your terminal and execute the given command on it to install an angular app:

ng new validate-reactiveform-app

Step 2 – Import the Module

Next is to open app.module.ts file and import HttpClientModule, Formsmodule and ReactiveFormsModule. It si given below. 

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
  declarations: [
  imports: [
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }

Step 3: Add Code on View File

In next step, create simple Reactive Form with Input File element. For this, visit src/app/app.component.html and update the following code into it.

<h1>Angular 12 Reactive Forms Validation Example -</h1>
<form [formGroup]="form" (ngSubmit)="submit()">
    <div class="form-group">
        <label for="name">Name</label>
        <div *ngIf=" &&" class="alert alert-danger">
            <div *ngIf="">Name is required.</div>
            <div *ngIf="">Name should be 3 character.</div>
    <div class="form-group">
        <label for="email">Email</label>
        <div *ngIf=" &&" class="alert alert-danger">
            <div *ngIf="">Email is required.</div>
            <div *ngIf="">Please, enter valid email address.</div>
    <div class="form-group">
        <label for="body">Body</label>
        <div *ngIf="f.body.touched && f.body.invalid" class="alert alert-danger">
            <div *ngIf="f.body.errors.required">Body is required.</div>
    <button class="btn btn-primary" type="submit">Submit</button>

Step 4: Use Component ts File

Now, follow this step in which we visit src/app directory and open app.component.ts. After this, add the following code like formGroup and formControl element on component.ts file:

import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators} from '@angular/forms';
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {
  form = new FormGroup({
    name: new FormControl('', [Validators.required, Validators.minLength(3)]),
    email: new FormControl('', [Validators.required,]),
    body: new FormControl('', Validators.required)
  get f(){
    return this.form.controls;

Step 5: Start the Angular App

Lastly, we reach the final step of implementation where we execute the following command on the terminal to start angular app:

ng serve


Now, we reach the end of this tutorial where we have learnt how to add validation in the Reactive Form.




Leave a Reply

Your email address will not be published. Required fields are marked *