Created
April 15, 2020 08:05
-
-
Save ibnumalik/f68bdc915a3304ae0a72a6fd21f0f933 to your computer and use it in GitHub Desktop.
Angular password confirmation validation
This file contains 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 } from '@angular/forms'; | |
export function MustMatch(controlName: string, matchingControlName: string) { | |
return (formGroup: FormGroup) => { | |
const control = formGroup.controls[controlName]; | |
const matchingControl = formGroup.controls[matchingControlName]; | |
if (matchingControl.errors && !matchingControl.errors.mustMatch) { | |
return; | |
} | |
if (control.value !== matchingControl.value) { | |
matchingControl.setErrors({ mustMatch: true }); | |
} else { | |
matchingControl.setErrors(null); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment