簡體   English   中英

如何使用正則表達式 /^[1-9]\d*$/ 自定義驗證兩個文本框?

[英]How to custom validate two text boxes with a regular expression /^[1-9]\d*$/?

我正在嘗試在某些條件下為 2 個文本框編寫自定義驗證器。

請參閱我在Stackblitz Link 中編寫的代碼,以在某些條件下驗證文本框

請幫助我 go 哪里錯了。

您可以在textbox1textBox2控件驗證器中添加它

this.registerForm = this.formBuilder.group(
  {
    textbox1: ['', [Validators.required, Validators.pattern("^[1-9]\d*$/"]],
    textbox2: ['', [Validators.required, Validators.pattern("^[1-9]\d*$/"]],
  }
];

現在從formGroup中刪除驗證器, Validators.pattern將確保在兩個字段上都應用驗證。

現在在*ngIf條件中添加errors.pattern以查看該模式是否匹配。

<div *ngIf="f.textbox1.errors.pattern">should not start with 0</div>
...
<div *ngIf="f.textbox2.errors.pattern">should not start with 0</div>

更新了 Stackblitz

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM