ion-input only ionic3 bounded numbers

Asked

Viewed 673 times

0

Good morning guys, I’m new to Ionic, I did a search through the site but I couldn’t find an answer to my question. See if you can help me:

I want to delimit a number-only data entry.

The maxlength attribute does not work in number type only in tel type. However, in tel type it is possible to insert letters and special characters such as # and *.

Also, in the type number it is possible to insert point.

I would like only numbers, with maximum number of 5. Someone would know to help me?

  • You can make a Regex to limit that. https://answall.com/questions/25605/express%C3%A3o-regular-accept-only-n%C3%Bameros-e-ou-letras-em-java

2 answers

1

Do so on your ion-input:

<ion-input type="tel" pattern="[0-9]*"></ion-input>

Works in the:

  • iOS 9/10+
  • Android 5+
  • It didn’t work buddy. It keeps accepting dots, comma, junk, asterisk etc.

  • sorry, I put the wrong type, use the "tel"

0


I got it. Here’s my solution: In the file.ts

formatar()
    {
       var login = this.login.rg;

       if(this.login.rg != undefined){
        login = login.replace(/\D/g, '');
        this.login.rg = login;
        console.log(this.login.rg);
       }

    }

In the . html file:

<ion-input type="tel"  (keyup)="formatar()" maxlength="5" size="4" clearInput placeholder="Digite seu RG (Somente números)" pattern="[0-9]" [(ngModel)]="login.rg" name="rg"></ion-input>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.