I need to check whether password and confirm password fields have same value using reactive form angular 4+. I did see a lot of answers on the same here,Angular 4 form validating for repeat password ,Comparing fields in validator with angular 4, but none seemed to work for me.
Facing issue , how to combine confirm Email and confirm password validator in reactive approach.
Is worked fine either confirm Email or Confirm Password.
this.addEmpForm = new FormGroup({
'name': new FormControl(null, Validators.required),
'email': new FormControl(null, [Validators.required, Validators.email]),
'cemail': new FormControl(null, [Validators.required, Validators.email]),
'password': new FormControl(null, Validators.required),
'cpassword': new FormControl(null, Validators.required)
}, this.pwdMatchValidator);
emailMatchValidator(frm: FormGroup) {
return frm.get('password').value === frm.get('cpassword').value
? null : { 'mismatch': true };
}
pwdMatchValidator(frm: FormGroup) {
return frm.get('password').value === frm.get('cpassword').value
? null : { 'mismatch': true };
}
HTML
<span class="help-block"
*ngIf="addEmpForm.get('cemail').touched && cemail?.errors
|| addEmpForm.get('cemail').touched
&& addEmpForm.errors?.mismatch">
Email doesn't match
</span>