我正在尝试开发一个联系表格,我希望用户输入长度在 10-12 之间的电话号码值。
值得注意的是,同样的验证正在 消息 字段上工作,它唯一给我带来麻烦的 数字 字段。
我有如下代码:
HTML:
<form [formGroup]="myForm" (ngSubmit)="myFormSubmit()">
<input type="number" formControlName="phone" placeholder="Phone Number">
<input type="text" formControlName="message" placeholder="Message">
<button class="button" type="submit" [disabled]="!myForm.valid">Submit</button>
</form>
TS:
this.myForm = this.formBuilder.group({
phone: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(12)]],
message: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(100)]]
});`
原文由 Sangwin Gawande 发布,翻译遵循 CC BY-SA 4.0 许可协议
像下面这样使用它并完美地工作:
自定义验证服务: