简体   繁体   English

角度2确认密码

[英]password confirmation in angular 2

I am trying to check whether the two passwords are equal. 我想检查两个密码是否相等。 I am not getting any error message when there are not but i am not getting the error message "Password must match" . 我没有收到任何错误消息,但没有,但我没有收到错误消息“密码必须匹配”

Below is my DOM: 以下是我的DOM:

<div class="form-group" [ngClass]="{'has-error': formName.get('password').touched && 
                formName.get('password').hasError('invalidPassword')}">
    <label class="control-label">Password</label>
    <input type="text" class="form-control" formControlName="password" name="password" required />
</div>

<div class="form-group" [ngClass]="{'has-error': formName.get('confirmpassword').touched 
        && formName.get('confirmpassword').hasError('mismatchedPasswords')}">
    <label class="control-label">confirm password</label>
    <input type="text" class="form-control" formControlName="confirmpassword" name="confirmpassword" required />
    <span class="help-block" *ngIf="formName.get('confirmpassword').touched 
            && formName.get('confirmpassword').hasError('mismatchedPasswords')">
            Password must match
        </span>
</div>

My Component class on how i am building my form: 关于我如何构建表单的我的Component类:

   this.formName = this.fb.group({
        name: ["", [Validators.required]],            
        password: ["",[Validators.required, ValidationHelper.passwordValidator]],
        confirmpassword: ["",Validators.required],
        info: this.fb.group({
            acc: ["",[Validators.required, ValidationHelper.creditCardValidator]]
        })
   },{ validator: ValidationHelper.matchPass})

And my password matching function: 我的密码匹配功能:

static matchPass = (control: AbstractControl) : {[key: string]: boolean} => {    
    let password = control.get('password');
    let confirmPassword = control.get('confirmpassword');
    return password.value === confirmPassword.value ? null : { 'mismatchedPasswords': true };        
}  

The function is getting called, i am also getting the return value... but why is my confirm password input control not showing error or activitating its span tag. 函数被调用,我也得到返回值...但为什么我的确认密码输入控件没有显示错误或激活其span标记。

The issue is with these lines in your template: 问题在于模板中的这些行:

formName.get('confirmpassword').hasError('mismatchedPasswords')

You're applying the validator to the group formName , but checking for the error on the confirmpassword formControl. 您正在将验证器应用于组formName ,但检查确认confirmpassword formControl上的错误。

Try using this in the two places where you check for mismatchedPasswords in your template. 尝试在您检查模板中mismatchedPasswords的两个地方使用此功能。

formName.hasError('mismatchedPasswords')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM