3
我想检查两个密码是否相等。我没有收到任何错误消息,但没有收到错误消息“密码必须匹配”。在角度2中的密码确认
下面是我的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>
我对我是多么建设我的表格组件类:
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})
我的密码匹配功能:
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 };
}
功能是被叫,我也得到了返回值...但为什么我的确认密码输入控件不显示错误或a激活其span标签。