10

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?

mafortis
  • 6,750
  • 23
  • 130
  • 288

2 Answers2

19

You can try Angular Reactive form pattern validator

this.savingData = this.formBuilder.group({
  amount: ['', [Validators.required, Validators.pattern("^[0-9]*$")]], // only numbers
});
Kamran Khatti
  • 3,754
  • 1
  • 20
  • 31
2

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

Oleg Postoev
  • 182
  • 1
  • 5