Masks - angular 2

Asked

Viewed 1,425 times

-2

I need to implement in a CNPJ or CPF mask field, that is, if it is above 14 characters changes the mask to CNPJ.

Same idea for phone, that is, mobile and fixed.

CNPJ, CPF, Phone, Mobile, single I’ve seen on npm.

Which one do you use ?

  • friend: usage eeses: https://github.com/assisrafael/angular-input-masks and https://github.com/the-darc/angular-br-filters.

  • But these are not for angular 1.x. x ?

1 answer

0

I did so:

  public cpfcnpjmask = function (rawValue) {
         var numbers = rawValue.match(/\d/g);
         var numberLength = 0;
         if (numbers) {
           numberLength = numbers.join('').length;
         }
         if (numberLength <= 11) {
           return [/[0-9]/, /[0-9]/, /[0-9]/, '.', /[0-9]/, /[0-9]/, /[0-9]/, '.', /[0-9]/, /[0-9]/, /[0-9]/, '-', /[0-9]/, /[0-9]/];
         } else {
           return [/[0-9]/, /[0-9]/, '.', /[0-9]/, /[0-9]/, /[0-9]/, '.', /[0-9]/, /[0-9]/, /[0-9]/, '/', /[0-9]/, /[0-9]/, /[0-9]/,  /[0-9]/, '-', /[0-9]/, /[0-9]/];
         }
       }

Component in html

<input matInput placeholder="Login" name="login" 
               [(ngModel)]="usuarioBuscar.login" [textMask]="{mask: cpfcnpjmask}" 
               matTooltip="Login do usuário" maxlength="20">

And it worked. I followed the same logic for phone and mobile. Using angular2-text-Mask lib.

Browser other questions tagged

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