I'm trying to use a regular expression with the Validators.pattern() for Angular 6 FormBuilder, but the validation never goes through. I have tested the RegEx on https://codepen.io/devtips/pen/gzqKKP to see if it was accepted and it did.
The expression I wrote is /^[(0-9)]*[\s][\d]{1,5}-?[\d]{1,5}$/ and the formats I'm trying to validate are (000) 000-0000 and (000) 0000-0000 but my input always shows an invalid result.
buildAgentDetailsForm() {
this.agentDetailForm = this.fb.group({
FirstName: ['', [Validators.required, Validators.nullValidator]],
LastName: ['', [Validators.required, Validators.nullValidator]],
Email: ['', [Validators.required, Validators.email]],
Phone: [
'',
[
Validators.required,
Validators.pattern(/^[(0-9)]*[\s][\d]{1,5}-?[\d]{1,5}$/)
]
],
EmergencyPhone: ['', [Validators.required]],
Address: ['', [Validators.required]]
});
}
Is my expression written in a wrong way? Also I'm using a mask on the input field so it will restrict the input and will include the () and the dash
/^\(\d{3}\)\s\d{3,4}-\d{4}$/(as per your sample strings).