1

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
  • 5,003
  • 10
  • 59
  • 163

3 Answers3

1

You can try Angular Reactive form pattern validator

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

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

Oleg Postoev
  • 114
  • 4
-1

Validators.pattern('^[^ ][0-9 ]+[^ ]$'), allows numeric values only. Also validates a field with not allowing spaces in the beginning or ending.

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

this.savingData = this.formBuilder.group({
  amount: ['', [Validators.required, Validators.pattern('^[^ ][0-9 ]+[^ ]$')]]
});
Pallavi
  • 414
  • 1
  • 9