Julius Dzidzevičius
Julius Dzidzevičius

Reputation: 11000

Angular4 - cant implement password match validator

From Angular docs:

const form = new FormGroup({
  password: new FormControl('', Validators.minLength(2)),
  passwordConfirm: new FormControl('', Validators.minLength(2)),
}, passwordMatchValidator);


function passwordMatchValidator(g: FormGroup) {
   return g.get('password').value === g.get('passwordConfirm').value
      ? null : {'mismatch': true};
}

I don't know how to call that passwordMatchValidator function. Tried something like:

<div *ngIf="form.passwordMatchValidator">

But without any success...

Upvotes: 1

Views: 782

Answers (1)

AVJT82
AVJT82

Reputation: 73357

To show the div with the error message if passwords don't match, you just do:

<div *ngIf="form.hasError('mismatch')">

Upvotes: 2

Related Questions