I've been attempting to implement a regular expression with the Validators.pattern() for Angular 6 FormBuilder, but for some reason, the validation is not working as expected. I even tested the RegEx on https://codepen.io/devtips/pen/gzqKKP and it was accepted there.
The pattern I came up with is
/^[(0-9)]*[\s][\d]{1,5}-?[\d]{1,5}$/
, and I'm trying to validate formats like (000) 000-0000
and (000) 0000-0000
. However, despite this, the input field 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]]
});
}
Could my expression be incorrectly written? It's worth mentioning that I have implemented a mask on the input field to restrict input and include characters like ()
and the dash.