Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow only number in a ion-input with type=text Ionic

I need to make the following input so it accepts only numbers:

<ion-input formControlName="quantity"></ion-input>

I know this can be achieved with type=number but I'm restricted to using the type text for other reasons. Is it possible to do so with type text? And also how could I validate a min and max amount?

like image 544
Ayane Avatar asked Oct 28 '25 14:10

Ayane


1 Answers

It can be done precisely in following way:

In your .ts file:

numericOnly(event): boolean {
   let pattern = /^([0-9])$/;
   let result = pattern.test(event.key);
   return result;
}

In your .html file:

<ion-input
        class="input"
        inputmode="numeric"
        type="number"
        (keypress)="numericOnly($event)"
      >
</ion-input>
like image 61
H S Progr Avatar answered Oct 30 '25 11:10

H S Progr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!