diff --git a/src/services/password-match.validator.ts b/src/services/password-match.validator.ts new file mode 100644 index 0000000..2524683 --- /dev/null +++ b/src/services/password-match.validator.ts @@ -0,0 +1,17 @@ +import {AbstractControl, ValidationErrors, ValidatorFn} from '@angular/forms'; + +export function passwordMatchValidator(password: string, retypePassword: string): ValidatorFn { + return (control: AbstractControl): ValidationErrors | null => { + const passwordValue = control.get(password)?.value; + const retypePasswordControl = control.get(retypePassword); + + if (retypePasswordControl?.value === passwordValue) { + retypePasswordControl!.setErrors(null); + return null; + } else { + const error = {passwordsMismatch: true}; + retypePasswordControl!.setErrors(error); + return error; + } + }; +}