app.component.ts
phoneFormControl = new FormControl('', [
Validators.required,
Validators.pattern("[6-9]\d{9}")
])app.component.html
<mat-form-field>
<input matInput placeholder="Phone Number" [formControl]="phoneFormControl>
<mat-error *ngIf="phoneFormControl.hasError('required')">
Phone Number is <strong>required</strong>
</mat-error>
</mat-form-field>表单提交错误
pattern:
actualValue: "9120227914"
requiredPattern: "^[6-9]d{9}$"发布于 2019-11-13 03:26:15
由于您的模式是一个string,所以应该将反斜杠转义。
所以,您需要的不是Validators.pattern("[6-9]\d{9}"),而是Validators.pattern("[6-9]\\d{9}")。
样本
readonly phoneFormControl = new FormControl('', [
Validators.required, Validators.pattern(("[6-9]\\d{9}"))
]);https://stackoverflow.com/questions/58829438
复制相似问题