Angular 9 Form Validation Example

Angular 9 Form Validation Example

In this post we will give you information about Angular 9 Form Validation Example. Hear we will give you detail about Angular 9 Form Validation ExampleAnd how to use it also give you demo for it if it is necessary.

This article will give you example of angular 9 form validation on submit example. Here you will learn form validation in angular 9 example. we will help you to give example of how to add form validation in angular 9. Let’s get started with angular 9 form validation example.

we will create reactive form step by step with validation like required, min length, email etc with angular 9 application.

Angular 9 has two types of forms as listed bellow:

1) Template-driven Forms

2) Reactive Forms

Here, we will see example of Reactive Forms Example

Reactive forms provide a model-driven approach to handling form inputs whose values change over the time. In Reactive forms, we need to import “ReactiveFormsModule” from angular forms library. we will use FormControl, FormGroup, FormArray, Validation classes with Reactive forms in angular 9 app.

If you have simple and basic form in your angular 9 application then i will prefer to use Reactive forms in angular. here i write simple example of Reactive forms with validation in angular 9.

You need to follow bellow step to add form validation in angular 9.

Step 1: Install Angular App

Here, in this step you need to create new ng app for this demo. if you have already created then don’t create new angular 9 app.

ng new my-new-app

Step 2: Import FormsModule

If you want to create form in angular app then you need to import FormsModule from @angular/forms library. so let’s add following code to app.module.ts file.


import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';


declarations: [



imports: [





providers: [],

bootstrap: [AppComponent]


export class AppModule { }

Also see:Angular Change Date Format in Component Example

Step 3: Form with ngModel

In this step, we will write code of html form with ngModel. so add following code to app.component.html file.

I used bootstrap class on this form. if you want to add than then follow this link too: Install Boorstrap 4 to Angular 9.


<div >

<h1>Angular 9 Form Validation Example -</h1>

<form [formGroup]="form" (ngSubmit)="submit()">

<div >

<label for="name">Name</label>






<div *ngIf=" &&" >

<div *ngIf="">Name is required.</div>

<div *ngIf="">Name should be 3 character.</div>



<div >

<label for="email">Email</label>






<div *ngIf=" &&" >

<div *ngIf="">Email is required.</div>

<div *ngIf="">Please, enter valid email address.</div>



<div >

<label for="body">Body</label>







<div *ngIf="f.body.touched && f.body.invalid" >

<div *ngIf="f.body.errors.required">Body is required.</div>



<button type="submit" [disabled]="!form.valid">Submit</button>



Step 4: updated Ts File

In ts file. we will write submit() and get all input fields values. so let’s add following code to app.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;






Now you can run your application using following command:

ng serve


Also see:Angular 9 Font Awesome | Install Font Awesome Icon Angular 9

{name: "Hardik Savani", email: "", body: "This is a test"}

I hope it can help you…

Hope this code and post will helped you for implement Angular 9 Form Validation Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

22  +    =  29

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US