Masks - angular 2

-2

I need to implement masks of the CNPJ or CPF in a field, that is, if it is more than 14 characters, change the mask to CNPJ.

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

CNPJ, CPF, Phone, Cellular, single I've seen in npm.

What do you use?

    
asked by anonymous 09.05.2018 / 03:19

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]/];
         }
       }

Non-html component

<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 cell phone. Using the angle-2-text-mask.

    
10.05.2018 / 13:06