The simple and tiny script for currency input mask
$ npm i -S @tadashi/currency
| parameter | type | required | default | description |
|---|---|---|---|---|
| input | HTMLInputElement | yes | - | Input element |
| opts | Object | no | see below | Options |
| parameter | type | required | default | description |
|---|---|---|---|---|
| keyEvent | String | no | input | Event which trigger mask |
| triggerOnBlur | Boolean | no | false | Trigger the mask when blur event occurs |
| init | Boolean | no | false | Format value when create instance |
| backspace | Boolean | no | false | Allow cleanup the input |
| maskOpts | Object | no | see below | Mask Options |
| parameter | type | required | default | description |
|---|---|---|---|---|
| empty | Boolean | no | false | Keep input empty if value is 0 |
| locales | String or Array | no | pt-BR | Same locales Intl.NumberFormat() |
| options | Object | no | {minimumFractionDigits: 2} | Same options Intl.NumberFormat() |
Return the instance of Currency from HTMLInputElement.
| parameter | type | required | default | description |
|---|---|---|---|---|
| input | HTMLInputElement | yes | - | Input element |
Return the value formatted.
| parameter | type | required | default | description |
|---|---|---|---|---|
| v | String | yes | - | Value which will be masked |
| opts | Object | no | see above | Mask Options |
Codepen example: https://codepen.io/lagden/pen/jOrZVjg?editors=1010
<input id="money" type="text" inputmode="numeric">
<script type="module">
import Currency from 'https://unpkg.com/@tadashi/currency@{version}/src/currency.js'
// Instance
const mask = new Currency(money)
// Static mode
Currency.masking(1100) // => 1.100,00
</script>MIT © Thiago Lagden