我有4个表单字段,我想检查'oldpass'和'newpass'字段是否使用反应形式相同。角2反应形式自定义验证
this.changePasswordForm = fb.group({
'oldpass': ['', Validators.compose([Validators.required, Validators.minLength(6)])],
'newpass': ['', Validators.compose([Validators.required, Validators.minLength(6)])],
'confirmpass': ['', Validators.compose([Validators.required])],
'otp': ['']
},{validator: CustomValidator.matchConfirmFields('newpass', 'confirmpass')});
而且我能够使用下面的代码验证'newpass'和'confirmpass'字段。
static matchConfirmFields(pass: string, confirmpass: string) {
return (group: FormGroup): {[key: string]: any} => {
let spass = group.controls[pass];
let sconfirmpass = group.controls[confirmpass];
if (spass.value !== sconfirmpass.value) {
return {
mismatchedPasswords: true
};
}
}
}
如何验证 'oldpass' 和 'newpass' 域类似的方式。
https://scotch.io/tutorials/how-to-implement-a-custom-validator-directive-confirm-password-in-angular-2看看这个第一 – misha130
感谢misha..but如何我是否要为“oldpass”和“newpass”调用第二个验证器。如果我误解了你的问题,可以请你帮助 – vishnu