Skip to content

Instantly share code, notes, and snippets.

@ibnumalik
Created April 15, 2020 08:05
Show Gist options
  • Save ibnumalik/f68bdc915a3304ae0a72a6fd21f0f933 to your computer and use it in GitHub Desktop.
Save ibnumalik/f68bdc915a3304ae0a72a6fd21f0f933 to your computer and use it in GitHub Desktop.
Angular password confirmation validation
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