1. 程式人生 > 實用技巧 >angular密碼和確認密碼的校驗

angular密碼和確認密碼的校驗

確認密碼

<input nz-input formControlName="password" (ngModelChange)="passwordChange($event)" [(ngModel)]="resetPassWordVo.password" required type="password" autocomplete="off">
<input nz-input formControlName="confirmPassword" (ngModelChange)="passwordChange($event)" [(ngModel)]="resetPassWordVo.confirmPassword" required type="password" autocomplete="off">
passwordChange(value: string){

    const confirmPasswordErrors = this.userPassForm.get('confirmPassword').errors;
    const passwordErrors = this.userPassForm.get('password').errors;
    if (confirmPasswordErrors && (confirmPasswordErrors.hasOwnProperty('pattern') || confirmPasswordErrors.hasOwnProperty('password') )){
      return;
    }
    if ( passwordErrors && (passwordErrors.hasOwnProperty('pattern') || passwordErrors.hasOwnProperty('password'))){
      return;
    }
    if (!this.resetPassWordVo.password || this.resetPassWordVo.password === '' ){
      return;
    }
    if (!this.resetPassWordVo.confirmPassword || this.resetPassWordVo.confirmPassword === '' ){
      return;
    }

    // 上邊的校驗通過後
    if (this.resetPassWordVo.password === value || this.resetPassWordVo.confirmPassword === value){
      this.userPassForm.get('password').setErrors(null);
      this.userPassForm.get('confirmPassword').setErrors(null);
    }else{
      this.userPassForm.get('password').setErrors({ passwordDiff: '兩次輸入的密碼不一致!'});
      this.userPassForm.get('confirmPassword').setErrors({ passwordDiff: '兩次輸入的密碼不一致!'});
    }

  }

個人部落格 蝸牛