I have a function which validates a password.
CustomValidators.patternValidator(/[A-Z]{2,}/, {
hasCapitalCase: true
}),
right now it is hard coded to 2 upper Case letters but I would like to load the password requirments based on a user profile via variables in my case it would be this.paswdProfile.upperCase but if I replace the 2 with the variable it no longer works. Here is my CustomValidator Code
export class CustomValidators {
static patternValidator(regex: RegExp, error: ValidationErrors): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
if (!control.value) {
// if control is empty return no error
return null;
}
// test the value of the control against the regexp supplied
const valid = regex.test(control.value);
// if true, return no error (no error), else return error passed in the second parameter
return valid ? null : error;
};
}
here is how i use it
paswdProfile = {minChar: 10, numbers: 2, upperCase: 4, lowerCase: 3, specialChar: 2}
CustomValidators.patternValidator(/[A-Z]{ paswdProfile.upperCase ,}/, {
hasCapitalCase: true
}),
i know that i can access the value because in my template i print
Must contain at least {{ paswdProfile.upperCase }} in Capital Case!
and that shows correct value
[A-Z]{ 4 ,}), { it doesnt work either