angular/forms integer validator

16.3k Views Asked by At

How can I validate my numeric input field to only accept integer and not any kind of decimal numbers (comma / dot)?

Code

Component

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

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

HTML

one

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

Any idea?

2

There are 2 best solutions below

4
On BEST ANSWER

You can try Angular Reactive form pattern validator

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

Try to use pattern like \d+. It is in Validators.pattern()