angular/forms 整数验证器

angular/forms integer validator

如何验证我的数字输入字段只接受整数而不接受任何类型的小数(逗号/点)?

代码

Component

import { FormGroup, FormBuilder, Validators } from '@angular/forms';

this.savingData = this.formBuilder.group({
  amount: ['', Validators.required], // only accept integer 123000
});

HTML

<ion-input type="number" min="1" inputmode="numeric" formControlName="amount" placeholder="{{ 'SAVINGS.amount' | translate }}" ></ion-input>

有什么想法吗?

尝试使用 \d+ 这样的模式。它在 Validators.pattern()

你可以试试Angular反应式pattern validator

this.savingData = this.formBuilder.group({
  amount: ['', [Validators.required, Validators.pattern("^[0-9]*$")]], // only numbers
});