Ngx-mask: Mask according to the length of the text

Created on 29 Aug 2018  路  8Comments  路  Source: JsDaddy/ngx-mask

Hi,

is it possible to add a mask according to the length of the text entered?

As in:

1234 = "12-34"
123456 = "12345-6"

enhancement help wanted question

Most helpful comment

@NepipenkoIgor Yes, exactly. It's Brazilian taxpayer doc id standard.

All 8 comments

@felipefsv Hi . 小ould you describe your pattern ?

@NepipenkoIgor Hello!

eg: if input is "92036365019" (11 digits) mask applied should be: "920.363.650-19". If input is "22137884000163" (14 digits) mask should be "22.137.884/0001-63". It's a matter of individual or business id code.

@felipefsv Condition mask interesting . Do we have only two conditions ?

@NepipenkoIgor Yes, exactly. It's Brazilian taxpayer doc id standard.

@NepipenkoIgor I'm interested in this possibility,too.

For the ones that still have interest, I did it this way:

On the component.ts

document: string;
documentMask: string;
this.documentForm = this.formBuilder.group({
      document_number: [this.document, Validators.required]});
currentDocumentMask(event) {
    this._processDocumentMask(this.documentForm['controls']document_number.value);
}
_processDocumentMask(documentNumber: string) {
    const cpfMask = '000.000.000-009';
    const cnpjMask = '00.000.000/0000-00';
    if (documentNumber && documentNumber.length <= 11) {
      this.documentMask = cpfMask;
    } else {
      this.documentMask = cnpjMask;
    }
}

On the HTML file

<form [formGroup]="documentForm">
 <input
                placeholder="CPF / CPNJ"
                type="text"
                [mask]="documentMask"
                (keyup)="currentMask($event)"
                formControlName="document_number">
</form>

@lhenriquelpc why not open a PR with it?

@lhenriquelpc why not open a PR with it?

@christopheelkhoury, do you mean a PR to improve the documentation?

Was this page helpful?
0 / 5 - 0 ratings