组件.html
<div class="form-group">
<label>Enter mobile</label>
<input type="text" class="form-control" formControlName="mobile" ><br>
</div>
<div *ngIf="userGroup.controls.mobile.invalid && (userGroup.controls.mobile.dirty || userGroup.controls.mobile.touched)">
<div *ngIf="userGroup.controls.mobile.errors.required">
Mobile number cannot be blank
</div>
<div *ngIf="userGroup.controls.mobile.errors.pattern">
Mobile number should be 10 digits only
</div>
</div>
组件.ts
userGroup:FormGroup;
ngOnInit() { this.userGroup = this.fb.group({
mobile:['',Validators.required,Validators.pattern(/^[0-9]{10}$/)]
});
}
对于空白,它工作正常,但对于图案,它没有显示任何错误
最佳答案
尝试这样:
mobile:['',[Validators.required,Validators.pattern(/^[0-9]{10}$/)]]
});
关于javascript - Validators.pattern 未显示手机号码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60648472/