I have an add/edit reactive form with phone number input field. I have added a pipe that transforms the input from 9999999999 to (999) 999-9999 and I am using a regex pattern to validate the same.
HTML
<input [value]="form.get('phone').value | phoneFormat" maxlength="14" formControlName="phone"/>
phoneFormat is a pipe that converts the input value to desired format.
Component
this.form = this.formBuilder.group({
phone: ['', [Validators.pattern(/((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/)]]
});
Issues
- I have to transform the phone value to
9999999999while saving the form. - While editing the form, the pattern validation fails initially because the phone number is not in desired format.
I am looking for a neat approach to handle this type of case.