Created
July 20, 2025 18:08
-
-
Save amilamen/0d9d681d211c7571186af2fce771b901 to your computer and use it in GitHub Desktop.
Password validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { FormGroup, ValidatorFn, ValidationErrors } from '@angular/forms'; | |
| export const passwordMatchValidator: ValidatorFn = (formGroup: FormGroup): ValidationErrors | null => { | |
| const passwordControl = formGroup.get('password'); | |
| const confirmPasswordControl = formGroup.get('confirmPassword'); | |
| if (!passwordControl || !confirmPasswordControl) { | |
| return null; | |
| } | |
| // If the confirm password field is empty, don't show an error yet | |
| // This is a common practice to avoid showing an error as soon as the form loads | |
| if (confirmPasswordControl.errors && !confirmPasswordControl.errors['passwordMismatch']) { | |
| return null; | |
| } | |
| if (passwordControl.value !== confirmPasswordControl.value) { | |
| // Return a validation error object | |
| return { passwordMismatch: true }; | |
| } | |
| // Pass validation, return null | |
| return null; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment