0

Basically I have password and confirmation inputs, and I want the confirmation input to validate if it's the same as the password input.. You know how it works.

My problem: I can't figure out how to update the confirmation validator every time the password input is changed. My current code never updates it.

component.ts:

 export class LoginComponent
    confirmPassword = '';
    newPasswordForm = new FormControl('', [
        Validators.required, Validators.minLength(5), Validators.maxLength(256), 
        Validators.nullValidator, Validators.pattern(pwRegexp)]);
    confirmPasswordForm = new FormControl('', [
        Validators.required, Validators.pattern(this.confirmPassword)]);

and html:

    <mat-form-field>
      <input #newPassword matInput placeholder="Password" [formControl]="newPasswordForm" [type]="hide2 ? 'password' : 'text'" [(ngModel)]="meme">
      <mat-icon matSuffix (click)="hide2 = !hide2">{{hide2 ? 'visibility' : 'visibility_off'}}</mat-icon>
      <mat-error *ngIf="newPasswordForm.invalid">{{getNewPasswordErrorMessage()}}</mat-error>
    </mat-form-field>
    <mat-form-field>
      <input matInput placeholder="Confirm password" [formControl]="confirmPasswordForm" [type]="hide3 ? 'password' : 'text'">
      <mat-icon matSuffix (click)="hide3 = !hide3">{{hide3 ? 'visibility' : 'visibility_off'}}</mat-icon>
      <mat-error *ngIf="confirmPasswordForm.invalid">{{getConfirmPasswordErrorMessage()}}</mat-error>
    </mat-form-field>
jsuoma
  • 1
  • Moi! Katso tämä: https://stackoverflow.com/a/43493648/6294072 ... So, I'd go all reactive form with this one instead of "loose" formcontrols. Then you can also drop the two-way-binding and just use the form controls :) – AT82 Oct 23 '17 at 07:49
  • You need to create a custom validator, have a look at this https://scotch.io/tutorials/how-to-implement-a-custom-validator-directive-confirm-password-in-angular-2 – Kay Oct 23 '17 at 11:38

0 Answers0