3.1.3
Simple, No Dependences and Compatibility with Vanilla, React, Mobile and etc...
Only 1kb (gzip) = ♥

Maskfy 3 - Mask Simple

A Javascript library without a dependencies... Compatibility with Vanilla, React and wherever. With only 1kb (gzip) code. It's also well accepted on mobile devices

Install

Node modules

npm i maskfy

// or

yarn add maskfy

Usage

React

import { maskfy } from 'maskfy';
import { useState } from 'react';

...

const [stateValue, setStateValue] = useState('')

...

const handleInput = (e) => {
  const { value } = e.currentTarget;
  const valueWithMask = maskfy(value, { mask: '(99) 9999-9999' });
  setStateValue(valueWithMask);
}

...

<input name="phone" value={stateValue} onInput={handleInput} />

Vue

// Coming soon

Angular

// Coming soon

ES Module

import { maskfy } from 'maskfy';

const valueMask = maskfy(e.target.value, { mask: '(99) 9999-9999' });
console.log(valueMask);

CommonJS

const { maskfy } = require('maskfy');

const valueMask = maskfy(valueInput, { mask: '(99) 9999-9999' });
console.log(valueMask);

AMD

<script src="./dist/amd/index.js"></script>
<script>
  const valueMask = window.Maskfy.maskfy(valueInput, { mask: '(99) 9999-9999' });
  console.log(valueMask);
</script>

CDN

<input id="phone" />

...

<script type="module">
  import { maskfy } from 'https://cdn.jsdelivr.net/gh/figuarnieri/maskfy@master/dist/esm/index.js'

  document.querySelector('#phone').addEventListener('input', (e) => {
    const valueMask = maskfy(e.target.value, { mask: '(99) 9999-9999' });
    e.target.value = valueMask.toUpperCase();
  });
</script>

Syntax

maskfy(value);
maskfy(value, options);

Parameters

value

String to which the mask will be applied

options optional

Object used to configure the mask

Name Type Default Description
mask string '999.999.999.999' String for mask implementation
reverse boolean false Applies the mask with character reversal. Commonly used for price formatting
keybind {
  [key: String]: RegExp
}
{
  mask: '999.999.999.999,99',
  reverse: true,
  keybind: {
    A: /[A-Za-z]/,
    9: /\d/,
    '?': /./,
  },
  prefix: 'US$ ',
  suffix: ' %'
}
Object for implementing character patterns of a mask
prefix string '' Prefix applied to the masked value
suffix string '' Suffix applied to the masked value

Usage

maskfy(value, {
  mask: '999.999.999.999,99',
  reverse: true,
  keybind: {
    e: /\d/,
  },
  prefix: 'US$ ',
  suffix: ' %',
});

Examples

m² 123.45

(11) 92222-3333

01/02/2030

111.222.333-44

+55 11911112222

ABC 1D23

111.222.333.444,55

Coming soon

Source

Github | npm

License

The MIT License
created by Filipe Guarnieri

Código copiado com sucesso!