I am trying to validate passwords, postal codes and phone numbers using the Angular pattern validators.
For some reason the password and postal code validation just don't seem to work, no matter which regex I try. The phone number regex works just fine though, which confuses me even more. I'm not sure where the error is, since even simple regex patterns don't seem to work, implying the error is in the formbuilder. However, it doesn't seem like I'm doing anything differently from the phone number regex.
Thanks in advance!
Regex used:
// Regex to check for valid phone number
phoneRegex = "^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$";
// Minimum eight characters, at least one letter, one number and one special character:
passRegex = "^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$";
// Postalcode regex (intended effect: 4 numbers, followed by two letters)
PostalRegex = "^\d{4}[a-zA-Z]{2}";
Formbuilder (simplified to include only relevant formcontrols):
this.userForm = this.fb.group({
phoneNumber: ['', {updateOn: 'blur', validators: [Validators.required, Validators.minLength(8), Validators.maxLength(12), Validators.pattern(this.phoneRegex)]}],
password: ['', {updateOn: 'blur', validators: [Validators.required, Validators.minLength(8), Validators.maxLength(32), Validators.pattern(this.passRegex)]}],
passwordConf: ['', {updateOn: 'blur', validators: Validators.required}],
postalCode: ['', {updateOn: 'blur', validators: [Validators.required, Validators.pattern(this.PostalRegex)]}],
}, { updateOn: 'submit', validators: this.checkPassMatch.bind(this) } as AbstractControlOptions);
}