2
0
mirror of https://github.com/tenrok/maska.git synced 2026-06-02 16:04:05 +03:00
Files
maska/README.md
T
2019-09-27 23:58:32 +03:00

108 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Maska
Simple zero-dependency input mask for Vue.js and vanilla JS. [Demo and examples](https://beholdr.github.io/maska/).
- No dependencies
- Small size (~2 Kb gziped)
- Ability to define custom tokens
- Supports repeat symbols
- Works on any input (custom or native)
## Install
npm install maska
To load latest version from CDN you can use:
``` html
<script src="https://cdn.jsdelivr.net/npm/maska@latest/dist/maska.js"></script>
```
## Usage with Vue.js
If you load Vue.js via `<script>` then just add `v-maska` directive to your input:
``` html
<input v-maska="'###'">
```
You can add custom tokens by passing in object instead of string to directive:
``` html
<input v-maska="{ mask: 'Z*', tokens: { 'Z': { pattern: /[а-яА-Я]/ }}}">
```
With bundlers you can add global directive:
``` javascript
import Maska from 'maska'
Vue.use(Maska)
```
or import `maska` directive for local usage in component:
``` html
<template>
<form>
<input v-maska="'###'">
</form>
</template>
<script>
import { maska } from 'maska'
export default {
directives: { maska }
}
</script>
```
## Usage with vanilla JS
Just load script `maska.js` and init it, passing element(s) or `document.querySelector` expression:
``` javascript
var mask = Maska.create('.masked');
```
You can pass custom tokens while initialization:
``` javascript
var mask = Maska.create('.masked', {
tokens: { 'Z': { pattern: /[а-яА-Я]/ }}
});
```
You can destroy mask like that:
``` javascript
var mask = Maska.create('.masked');
mask.destroy();
```
## Mask syntax
Default tokens:
``` javascript
{
'#': { pattern: /[0-9]/ },
'X': { pattern: /[0-9a-zA-Z]/ },
'S': { pattern: /[a-zA-Z]/ },
'A': { pattern: /[a-zA-Z]/, uppercase: true },
'a': { pattern: /[a-zA-Z]/, lowercase: true },
'!': { escape: true },
'*': { repeat: true }
}
```
- Escape symbol escapes next token (mask `!#` will render `#`)
- Repeat symbol allows repeating current token until its valid (e.g. mask `#*` for all digits or `A* A*` for `CARDHOLDER 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]"`).
## Source of Inspiration
- [vue-the-mask](https://vuejs-tips.github.io/vue-the-mask/)
- [jQuery Mask Plugin](http://igorescobar.github.io/jQuery-Mask-Plugin/)