【发布时间】:2019-10-18 12:25:42
【问题描述】:
我正在尝试使用 2 个验证器来验证用户的输入,这些验证器的模式与 Validators.compose([]) 相结合
但是然后在HTML中我试图验证但它不起作用
代码如下:
组件.ts
...
searchForm: FormGroup;
constructor(
private data: DataSharingService,
private router: Router
){ }
ngOnInit() {
this.createSearchForm();
this.newMessage()
}
private createSearchForm(): void {
this.searchForm = new FormGroup({
emailFormControl: new FormControl('', Validators.compose([
Validators.pattern['/^[a-zA-Z]{2}[0-9]{6}/'],
Validators.pattern['/^[0-9]{10}/']
]))
});
}
...
component.html
<form class="mainContainer" [formGroup]="searchForm">
<mat-form-field class="searchfield">
<input matInput placeholder="Type in tax payer ID or initials "
formControlName="emailFormControl"
i18n-placeholder="@@SearchPeHint">
<mat-error *ngIf="searchForm.controls['emailFormControl'].hasError('required')" i18n="@@SearchPeParamsRequired">
Tax payer ID or initials are required!
</mat-error>
</mat-form-field>
【问题讨论】:
标签: html angular typescript angular-forms