simple-mask-money.js | Implementing Money(Currency) Mask with Vanilla JavaScript

simple-mask-money.js is a money mask in vanilla JavaScript, it can format input number to currencystyle. It supports custom prefix, suffix, decimal/thousand separators as well as precision.

simple-mask-money.js - Implementing Money(Currency) Mask with Vanilla JavaScript

AuthorLinksMade withLast update
codermarcosOffice HomeJavaScriptSep 17, 2020

How to implement money mask using simple-mask-money.js?

1.Import simple-mask-money.js in html

<script src="simple-mask-money.js"></script>

2. Create a text input

<input inputmode="numeric" id="myInput" value="0,00"><br>

3.Activate the money mask using input field id

let input = SimpleMaskMoney.setMask('#myInput');

4. You also can set some configurations for this money mask

let input = SimpleMaskMoney.setMask('#myInput',{
    prefix: '',
    suffix: '',
    fixed: true,
    fractionDigits: 2,
    decimalSeparator: ',',
    thousandsSeparator: '.',
    emptyOrInvalid: () => {
      return this.SimpleMaskMoney.args.fixed
        ? `0${this.SimpleMaskMoney.args.decimalSeparator}00`
        : `_${this.SimpleMaskMoney.args.decimalSeparator}__`;
    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *