how to create a currency mask with regex

Asked

Viewed 4,154 times

2

hello, I’m beginner with angular 4, I’m trying to make a mask for a text type field to validate a value in m².

The Mask class contains the method to create the mask, the given value can be between 1 and unlimited.

wanted the method returns something in this context: 1.000.000,00 or 10.000,00 or 1.000,00

export class Mask
{

  public static getArea():Array<string|RegExp>
    {

     return [/\d/,/\d/,/\d/,'.',/\d/,/\d/,/\d/,'.',/\d/,/\d/,/\d/,',',/\d/,/\d/];

    }
}
<input formControlName="areaImovel" [textMask]="{ mask: areaMask }" type="text" id="form-area-imovel" class="form-control">

Calling on the Component

import { Mask } from './mask';

export class EnderecoComponent implements OnInit {
  public areaMask = Mask.getArea();
  
  constructor(){}
}

2 answers

1

Use the dependency of ng2-currency-Mask:

  1. add dependency to your project:

    npm i ng2-currency-mask --save
    
  2. Import the component into your model:

    import { CurrencyMaskModule } from 'ng2-currency-mask';
    
  3. use:

<input type="text" id="form-area-imovel" formControlName="areaImovel"
 class="form-control" placeholder="0,00"
 currencyMask [options]="{ prefix: '', thousands: '.', decimal: ',', allowNegative: false }">

0

Browser other questions tagged

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