Update FormGroup code as shown below in Angular5
this.signUpForm = new FormGroup({ 'username': new FormControl(null, [Validators.required, Validators.minLength(6), Validators.maxLength(15)]), 'email': new FormControl(null, [Validators.required, Validators.email, Validators.minLength(5)]), 'password': new FormControl(null, [Validators.required]), 'confirmedPassword': new FormControl(null, [Validators.required]) }, this.pwdMatchValidator);
Add the pwdMatchValidator function to your component
pwdMatchValidator(frm: FormGroup) { return frm.get('password').value === frm.get('confirmedPassword').value ? null : {'mismatch': true}; }
Add confirmation message to your template
<span *ngIf="confirmedPassword.errors || signUpForm .errors?.mismatch"> Password doesn't match </span>
Please find below angular working material.
Templete Code component password.component.html
<form class="cahnge-pwd-form" (ngSubmit)="onSubmit()" name="passwordForm" [formGroup]="passwordForm" #formDir="ngForm"> <div fxLayout='column'> <mat-form-field> <input matInput name="password" placeholder="Password" [type]="hide ? 'text' : 'password'" formControlName="password" required> <mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility_off' : 'visibility'}}</mat-icon> <mat-error *ngIf="password.invalid && (password.dirty || password.touched || isSubmit)"> <span *ngIf="password.errors.required"> Please enter a Password. </span> <span *ngIf="password.errors.maxlength"> Please enter a Email no more than 16 characters. </span> <span *ngIf="password.errors.minlength"> Please enter a password at least 6 characters. </span> </mat-error> </mat-form-field> <mat-form-field> <input matInput name="password" placeholder="Confirm Password" [type]="confirm_hide ? 'text' : 'password'" formControlName="confirm_password" required> <mat-icon matSuffix (click)="confirm_hide = !confirm_hide">{{confirm_hide ? 'visibility_off' : 'visibility'}}</mat-icon> <mat-error *ngIf="(confirm_password.invalid && (confirm_password.dirty || confirm_password.touched || isSubmit) || passwordForm.errors?.mismatch)"> <span *ngIf="confirm_password.errors || passwordForm.errors?.mismatch"> Password doesn't match </span> </mat-error> </mat-form-field> <div fxLayout='row' fxLayoutGap="10px"> <button type="submit" mat-raised-button color="primary">Submit</button> <button type="button" (click)="formDir.resetForm(passwordForm)" mat-raised-button color="warn">Cancel</button> </div> </div> </form>
Component Code: password.component.ts
import { Component, OnInit, AfterViewInit } from '@angular/core'; import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms'; import { ToastrService } from 'ngx-toastr'; import { Router, ActivatedRoute, ParamMap } from '@angular/router'; import { PasswordService } from './password.service'; import { PasswordValidation } from './confirm'; @Component({ selector: 'app-password', templateUrl: './password.component.html', styleUrls: ['./password.component.css'] }) export class PasswordComponent implements OnInit { passwordForm: FormGroup; isSubmit: boolean; constructor(private router: Router, private passwordService: PasswordService, private toastrService: ToastrService, private route: ActivatedRoute) { } ngOnInit() { this.passwordForm = new FormGroup({ 'password': new FormControl('', [ Validators.required, Validators.minLength(6), Validators.maxLength(16), ]), 'confirm_password': new FormControl('', [ Validators.required, Validators.minLength(6), Validators.maxLength(16), ]), }, this.pwdMatchValidator); } pwdMatchValidator(frm: FormGroup) { return frm.get('password').value === frm.get('confirm_password').value ? null : {'mismatch': true}; } get password() { return this.passwordForm.get('password'); } get confirm_password() { return this.passwordForm.get('confirm_password'); } onSubmit(formvalue):boolean { this.isSubmit = true; if (this.passwordForm.invalid) { return false; } else { this.passwordService.updatePassword(this.passwordForm.value) .subscribe((res) => { if (res.status == 'success') { this.toastrService.success(res.msg); this.router.navigate(['/change-password']); } }) return true; } } }