我有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