2

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

  1. I have to transform the phone value to 9999999999 while saving the form.
  2. 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.

2 Answers 2

1

This validation is possible without using any pipe for conversion or transforming its value. By using mask validation of RxWeb.

The @rxweb/reactive-form-validators has mask validation which can fulfill your validation requirement. It will validate your input as per the required regex format and save the value in its original form eg : 9999999999

export class UserComponent implements OnInit {
    userFormGroup: FormGroup

    constructor(
        private formBuilder: FormBuilder ) { }

    ngOnInit() {
        this.userFormGroup = this.formBuilder.group({
            phoneNumber:['', RxwebValidators.mask({mask:'(999)-999 9999' })], 
        });
    }

    onSubmit(){
      console.log(this.userFormGroup.value);
    }
}
<div>
  <form  [formGroup]="userFormGroup">
    <div class="form-group">
      <label>Phone Number</label>
      <input type="text" formControlName="phoneNumber" class="form-control"  />
     <small class="form-text text-danger" *ngIf="userFormGroup.controls.phoneNumber.errors">                     {{userFormGroup.controls.phoneNumber.errors.mask.message}}<br/></small>    
    </div>
    <button [disabled]="!userFormGroup.valid" class="btn btn-primary" (click)="onSubmit()">Submit</button>
  </form>
</div>

For implementing this you need to import RxReactiveFormsModule in the main module and use RxWebValidators while formControl initialization.

Here is the stackblitz link of the above example : Open

Sign up to request clarification or add additional context in comments.

Comments

0

why you change the form for saving? just get the form value, assign it to an object and transform it as you want. but do not change the form value.

save() {
    let formValue = this.form.value;
    formValue.phoneNumber = formValue.phoneNumber.match(/(\d+)/);
    // rest of the job
}

and for the cases that you need to set form value (if it's a possible scenario) first normalize the phone number to the format that is valid then set the form value.

normalize(val) {
    let formValue = val;
    const phoneFormatPipe = new phoneFormat();
    formValue.phoneNumber = phoneFormatPipe.transform(formValue.phoneNumber);
    this.form.patchValue(formValue);
}

1 Comment

1. I am just transforming the value before saving. The solution that I am looking for is to skip this transforming overhead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.