Maska
Simple zero-dependency input mask for Vue.js and vanilla JS.
- No dependencies
- Small size (~2 Kb gziped)
- Ability to define custom tokens
- Supports repeat symbols and dynamic masks
- Works on any input (custom or native)
Install
npm install maska
To load latest version from CDN you can use:
Usage with Vue.js
If you load Vue.js via <script>
then just add v-maska
directive to your input:
You can add custom tokens by passing in object instead of string to directive:
With bundlers you can add global directive:
or import maska
directive for local usage in component:
Usage with vanilla JS
Just load script maska.js
and init it, passing element(s) or document.querySelector
expression:
You can pass custom tokens while initialization:
You can destroy mask like that:
Mask syntax
Default tokens:
- Escape symbol escapes next token (mask
!#
will render#
) - Repeat symbol allows repeating current token until it’s valid (e.g. mask
#*
for all digits orA* A*
forCARDHOLDER NAME
)
You can add your own tokens by passing them in maska
directive or create
method at initialization (see above). Important: pattern
field should be JS regular expression (/[0-9]/
) or string without delimiters ("[0-9]"
).
Dynamic masks
To use several masks on single input, pass array instead of string as mask value.
You could use it with Vue directives:
and with vanilla JS attribute, but make sure that mask value is proper JSON
, so use double quotes inside array: