Pattern: -
"^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$"
OR
"^\\(?([2-9][0-8][0-9])\\)?[-. ]?([2-9][0-9]{2})[-.]?([0-9]{4})$"
Valid
1. (123) 123-1234
2. 123-123-1234
Component.ts
import { FormControl, FormGroup, Validators } from '@angular/forms';
form: FormGroup;
createForm() {this.form = new FormGroup({
Phone: new FormControl('',Validators.pattern("^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$"))
});
}
ngOnInit() {
this.createForm();
import { MatFormFieldModule } from '@angular/material/form-field'this.createForm();
}
API reference
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { MatInputModule } from "@angular/material/input";
<mat-form-field>
<input matInput placeholder="Phone" formControlName="Phone">
<mat-error *ngIf="form.get('Phone').hasError('pattern')">
Phone is invalid.
</mat-error>
</mat-form-field>
Component.html
<mat-form-field>
<input matInput placeholder="Phone" formControlName="Phone">
<mat-error *ngIf="form.get('Phone').hasError('pattern')">
Phone is invalid.
</mat-error>
</mat-form-field>
No comments:
Post a Comment