Currently, I am working on a reactive form that includes an input field for phone numbers. To ensure the format is consistent throughout, I have implemented a pipe that converts the input from 9999999999
to (999) 999-9999
, along with using a regex pattern for validation.
HTML Code Snippet:
<input [value]="form.get('phone').value | phoneFormat" maxlength="14" formControlName="phone"/>
The "phoneFormat" pipe is responsible for transforming the input.
Component Setup:
this.form = this.formBuilder.group({
phone: ['', [Validators.pattern(/((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/)]]
});
Challenges Faced:
- When saving the form, there is a need to revert the phone value back to
9999999999
. - During editing, the initial failure of pattern validation occurs because the phone number does not match the desired format.
I am seeking a more efficient approach to tackle these issues in this particular scenario.