Multi Step Form In Angular.

What Is Multi Step Form?

Multi step Form is a long form broken into multi steps. It is not a multi forms rather than multi steps in a single long form. The reason behind it, short form is more easy to complete than long form. It is more comfortable for users to fill the form step by step because their mind should process fewer fields at a time.

Now a days multi step form is more popular than regular form , as it is more user friendly rather than regular form. The multi step form is the best solution when there are too many fields in a from. So breaking large form into multiple step is more user friendly.

It is used to make long form, such as checkout page , registration form etc.,less intimidating and daunting.

Why Use Multi step Form?

It is more comfortable than regular form.

It is easy to fill short form than long form.

It can increase engagement.


It reduce psychological friction.

Below are the steps to implement multi step form in Angular.

Step 1: Create New App.

ng new angular-multistep

Step 2: Import Module.

In this step, just import FormsModule, ReactiveFormsModule, and BrowserAnimationsModule module for creating this example. Open src/app/app.module.ts and add below lines.

import { BrowserModule } from ‘@angular/platform-browser’;
import {FormsModule, ReactiveFormsModule} from ‘@angular/forms’;

declarations: [
providers: [],
bootstrap: [AppComponent]
export class AppModule { }

Step 3 : Update ts file.

Add below code in src/app/app.component.ts

import { Component, OnInit } from ‘@angular/core’;
import { FormControl,FormGroup,Validators,FormBuilder } from ‘@angular/forms’;

selector: ‘app-multistep’,
templateUrl: ‘./multistep.component.html’,
styleUrls: [‘./multistep.component.css’]
export class MultistepComponent implements OnInit {

personalDetails!: FormGroup;
addressDetails!: FormGroup;
educationalDetails!: FormGroup;
personal_step = false;
address_step = false;
education_step = false;
step = 1;

constructor(private formBuilder: FormBuilder) { }

ngOnInit() {

this.personalDetails ={
name: [”, Validators.required],
email: [”, Validators.required],
phone: [”,Validators.required]

this.addressDetails ={
city: [”, Validators.required],
address: [”, Validators.required],
pincode: [”,Validators.required]

this.educationalDetails ={
highest_qualification: [”, Validators.required],
university: [”, Validators.required],
total_marks: [”,Validators.required]

get personal() { return this.personalDetails.controls; }
get education() { return this.educationalDetails.controls; }
get address() { return this.addressDetails.controls; }


this.personal_step = true;
if (this.personalDetails.invalid) { return }

this.address_step = true;
if (this.addressDetails.invalid) { return }



this.personal_step = false;
this.education_step = false;


this.education_step = true;
if (this.educationalDetails.invalid) { return }

Step 4 : Update html file.

Add below code in src/app/app.component.html.

<div class=”row”>
<div class=”col-md-12 col-md-offset-3″>
<form [formGroup]=”personalDetails” id=”msform”>
<!– progressbar –>
<ul id=”progressbar”>
<li [ngClass]=”step>=1 ? ‘active ‘ : ‘inactive'”>Personal Details</li>
<li [ngClass]=”step>=2 ? ‘active’ : ‘inactive'”>Social Profiles</li>
<li [ngClass]=”step==3 ? ‘active’ : ‘inactive'”>Account Setup</li>
<!– fieldsets –>

<fieldset *ngIf=”step == 1″ >
<h2 class=”fs-title”>Personal Details</h2>
<input type=”text” formControlName=”name” [ngClass]=”{ ‘is-invalid’: personal_step && }” placeholder=”Full Name”/>
<div *ngIf=”personal_step &&”>
<div *ngIf=””>Name is required</div>
<input type=”text” formControlName=”email” name=”email” placeholder=”Email”/>
<div *ngIf=”personal_step &&”>
<div *ngIf=””>Email is required</div>
<input type=”text” formControlName=”phone” name=”phone” placeholder=”Phone”/>
<div *ngIf=”personal_step &&”>
<div *ngIf=””>Phone is required</div>
<input (click)=”next()” type=”button” name=”next” class=”next action-button” value=”Next”/>
<form [formGroup]=”addressDetails” id=”msform”>
<fieldset *ngIf=”step == 2″ >
<h2 class=”fs-title”>Address Details</h2>
<input type=”text” formControlName=”city” placeholder=”City”/>
<div *ngIf=”address_step &&”>
<div *ngIf=””>City is required</div>
<textarea rows=”5″ cols=”5″ formControlName=”address” placeholder=”address”></textarea>
<div *ngIf=”address_step && addressDetails.controls.address.errors”>
<div *ngIf=”address.address.errors?.required”>Address is required</div>
<input type=”text” formControlName=”pincode” placeholder=”Pincode”/>
<div *ngIf=”address_step && addressDetails.controls.pincode.errors”>
<div *ngIf=”address.pincode.errors?.required”>Pincode is required</div>
<input (click)=”previous()” type=”button” name=”previous” class=”previous action-button-previous” value=”Previous”/>
<input (click)=”next()” type=”button” name=”next” class=”next action-button” value=”Next”/>
<form [formGroup]=”addressDetails” id=”msform”>
<fieldset *ngIf=”step == 3″>
<h2 class=”fs-title”>Educational Details</h2>
<input type=”text” formControlName=”highest_qualification” placeholder=”Highest Qualification”/>
<div *ngIf=”education_step && educationalDetails.controls.highest_qualification.errors”>
<div *ngIf=”education.highest_qualification.errors?.required”>Highest qualification is required</div>
<input type=”text” formControlName=”university” placeholder=”University”/>
<div *ngIf=”education_step &&”>
<div *ngIf=””>University is required</div>
<input type=”text” formControlName=”total_marks” placeholder=”Total Marks”/>
<div *ngIf=”education_step && educationalDetails.controls.total_marks.errors”>
<div *ngIf=”education.total_marks.errors?.required”>Total marks is required</div>
<input (click)=”previous()” type=”button” name=”previous” class=”previous action-button-previous” value=”Previous”/>
<input (click)=”submit()”type=”submit” name=”submit” class=”submit action-button” value=”Submit”/>


Step 3 : Add below code in app-routing.module.ts file.

import { NgModule } from ‘@angular/core’;
import { Routes, RouterModule } from ‘@angular/router’;
import { MultistepComponent} from ‘./multistep/multistep.component’

const routes: Routes = [

imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
export class AppRoutingModule { }

That’s all about multi step form in Angular. After writing above code open http://localhost:4200/multistep.

Reactive Form Validation In Angular.

The post Multi Step Form In Angular. appeared first on PHPFOREVER.

Flatlogic Admin Templates banner

Leave a Reply

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