密码确认角材料

LDB*_*LDB 2 html javascript typescript angular-material angular

我正在努力通过Angular Material对用户进行身份验证。我目前正试图在确认密码与第一个输入的密码不匹配时显示正确的错误。

这是我的html:

 <mat-form-field hintLabel="Minimum 8 Characters" class="">
                            <mat-label>Password</mat-label>
                            <input 
                            matInput 
                            #input 
                            type="password"
                            formControlName="password">
                            <mat-hint align="end">{{input.value?.length || 0}}/8</mat-hint>
                        </mat-form-field>

                        <mat-form-field>
                            <mat-label>Confirm</mat-label>
                            <input 
                            matInput
                            required  
                            type="password"
                            #confirm
                            formControlName="confirmPassword">
                            <mat-error *ngIf="form.get('confirmPassword').invalid || confirmPasswordMismatch">Password does not match</mat-error>
                            </mat-form-field>
Run Code Online (Sandbox Code Playgroud)

一旦用户将注意力集中在密码确认上并且不进行任何输入而没有进行聚焦,就会显示该错误。用户输入任何内容后,即使确认与密码不匹配,错误也会消失。

这是我的TS文件:

public get confirmPasswordMismatch() {
        return (this.form.get('password').dirty || this.form.get('confirmPassword').dirty) && this.form.hasError('confirmedDoesNotMatch');
    }

this.form = new FormGroup({
            userName: new FormControl(null, [Validators.required]),
            fullName: new FormControl(null, [Validators.required]),
            email: new FormControl(null, [Validators.required, Validators.pattern(this.EMAIL_REGEX)]),
            password: new FormControl(null),
            confirmPassword: new FormControl(null, ),
        }, (form: FormGroup) => passwordValidator.validate(form));
Run Code Online (Sandbox Code Playgroud)

预期的效果是,当确认pw为空时,当用户将文本输入到pw输入中时显示错误,而当两个用户都有文本但确认与pw不匹配时显示错误。

wei*_*eld 5

我这样解决了:

模板:

  <mat-form-field>
    <input matInput type="password" placeholder="Password" formControlName="password" (input)="onPasswordInput()">
    <mat-error *ngIf="password.hasError('required')">Password is required</mat-error>
    <mat-error *ngIf="password.hasError('minlength')">Password must have at least {{minPw}} characters</mat-error>
  </mat-form-field>

  <mat-form-field>
    <input matInput type="password" placeholder="Confirm password" formControlName="password2" (input)="onPasswordInput()">
    <mat-error *ngIf="password2.hasError('required')">Please confirm your password</mat-error>
    <mat-error *ngIf="password2.invalid && !password2.hasError('required')">Passwords don't match</mat-error>
  </mat-form-field>
Run Code Online (Sandbox Code Playgroud)

零件:

export class SignUpFormComponent implements OnInit {

  minPw = 8;
  formGroup: FormGroup;

  constructor(private formBuilder: FormBuilder) { }

  ngOnInit() {
    this.formGroup = this.formBuilder.group({
      password: ['', [Validators.required, Validators.minLength(this.minPw)]],
      password2: ['', [Validators.required]]
    }, {validator: passwordMatchValidator});
  }

  /* Shorthands for form controls (used from within template) */
  get password() { return this.formGroup.get('password'); }
  get password2() { return this.formGroup.get('password2'); }

  /* Called on each input in either password field */
  onPasswordInput() {
    if (this.formGroup.hasError('passwordMismatch'))
      this.password2.setErrors([{'passwordMismatch': true}]);
    else
      this.password2.setErrors(null);
  }
}
Run Code Online (Sandbox Code Playgroud)

验证器:

export const passwordMatchValidator: ValidatorFn = (formGroup: FormGroup): ValidationErrors | null => {
  if (formGroup.get('password').value === formGroup.get('password2').value)
    return null;
  else
    return {passwordMismatch: true};
};
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 由于onPasswordInput()从任一密码字段都被调用,因此编辑第一个密码字段(从而使密码确认无效)也会导致不匹配错误显示在密码确认字段中。
  • *ngIf="password2.invalid && !password2.hasError('required')"密码确认字段的测试可确保永远不会同时显示两个错误消息(“不匹配”和“必需”)。