0

I have angular2 form as:

this.registerForm = formBuilder.group({
        'name': ['', Validators.required],
        'email': ['', Validators.compose([Validators.pattern("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"), Validators.required])],
        'password': ['', Validators.compose([Validators.minLength(6), Validators.required])],
        'repassword': ['', Validators.compose([Validators.minLength(6), Validators.required])]
    });

But I am unable to figure out how to check repassword with password and throw error msg.

** I followed answer of Angular 2 form validating for repeat password but unable to include ControlGroup.

Please help

Community
  • 1
  • 1
Narendra Vyas
  • 717
  • 4
  • 9
  • 26

1 Answers1

0

You need to implement your custom validator to compare password and repassword fields.

import { AbstractControl } from '@angular/forms';

function comparePassword(c: AbstractControl) {
    return c.get('password').value === c.get(' repassword').value
}

this.myForm = this.fb.group({
    name: ['', Validators.compose([Validators.required])],
    email: [''],
    passwordgroup: this.fb.group({
        password: ['', Validators.compose([Validators.required])],
        repassword: ['', Validators.compose([Validators.required])]
    },  comparePassword)
});
Ali Shahzad
  • 5,163
  • 7
  • 36
  • 64