From 6fd78e7830d4522f8f2da628bc87751070650f75 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 11 Jun 2024 00:26:00 +0300 Subject: [PATCH] feat: add password match validator for password and retype fields --- src/services/password-match.validator.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/services/password-match.validator.ts 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; + } + }; +}