Tiny input mask library for Vue.js based on text-mask-core (~5kb) exposed as directive. No dependencies - add persian and arabic numbers support
npm install v-mask-persiansh
npm install v-mask
`
Initialization
$3
`javascript
import Vue from "vue";
// As a plugin
import VueMask from "v-mask";
Vue.use(VueMask);
// Or as a directive
import { VueMaskDirective } from "v-mask";
Vue.directive("mask", VueMaskDirective);
// Or only as a filter
import { VueMaskFilter } from "v-mask";
Vue.filter("VMask", VueMaskFilter);
`
$3
`html
`
:rocket: Usage
`html
`
Notice: v-model is required starting from v1.1.0, because a lot of bugs with HTMLElement event listeners and sync with Vue internals.
There is no reason to support using this lib for using without v-model but open the door for using on custom inputs.
$3
`html
{{ '9999999999' | VMask('(###) ###-####') }}
`
:gear: Configuration
Library provides several ways to apply the mask.
The first and the easiest one is to use default placeholders.
$3
This approach is good for simple cases. No configuration is required.
app.js:
`js
import Vue from "vue";
import VueMask from "v-mask";
Vue.use(VueMask);
`
:
`vue
`
Entering 56f473d4 in the input field will produce value 5647-34 in myInputModel variable.
Here is a list placeholders you can utilize by default:
| Placeholder | Format |
| ----------- | ------------------------------ |
| # | Number (0-9) |
| A | Letter in any case (a-z,A-Z) |
| N | Number or letter (a-z,A-Z,0-9) |
| X | Any symbol |
| ? | Optional (next character) |
$3
While default placeholders are easy to use and straightforward in reality we have to deal with more complex cases where validation can be a bit more complex and unpredictable. In such cases it makes sense to define custom placeholders specific to the project or the domain.
To define them you should pass them as an object while installing plugin. Where:
- key is the character in a mask
- value is regular expression used to verify entered symbol
You can disable any default placeholder by passing placeholder as a key and null as a value.
Any valid string character can be used as a placeholder (e.g. Cyrillic or Arabic)
app.js:
`js
import Vue from "vue";
import VueMask from "v-mask";
Vue.use(VueMask, {
placeholders: {
"#": null, // passing null removes default placeholder, so # is treated as character
D: /\d/, // define new placeholder
Я: /[\wа-яА-Я]/, // cyrillic letter as a placeholder
},
});
`
:
`vue
`
Entering 123456 in that input field will produce value ###-123-###-456 in myInputModel variable.
$3
In some cases you might not want to define global placeholders either because you are dealing with unique input or you are facing conflicts for placeholders in several places.
In such cases you can supply array of per-char regular excursions or static characters to v-mask.
app.js:
`js
import Vue from "vue";
import VueMask from "v-mask";
Vue.use(VueMask);
`
:
`vue
`
In this example entering 5555551234 in the input field will produce value (555) 555-1234 in myInputModel variable.
Notice: Keep in mind that library always verifies _one_ character per regular expression. Trying verify multiple charters in the same RegExp won't work.
$3
If custom placeholder and array of RegExps can't fit your needs there is one more way you can use to mask a value.
The idea beneath is that you can write a function that is used by library to format the output.
This approach is super powerful but also more complex to write and understand so try previous ones first.
The function will be given a value from the input. It should return array of per-char regular expressions & static characters:
app.js:
`js
import Vue from "vue";
import VueMask from "v-mask";
Vue.use(VueMask);
`
:
`vue
type="text"
v-mask="mask"
v-model="myInputModel"
placeholder="00:00-23:59"
/>
`
In this example entering 02532137 in the input field will produce valid time range 02:53-21:37 in myInputModel variable.
$3
Library supports Text Mask Addons, they are basically pre-generated functions (describe above) for advanced functionality like currency masking.
The usage is simple. Configure the addon as want and pass the result to the v-mask as you would to text-mask-core.
app.js:
`js
import Vue from "vue";
import VueMask from "v-mask";
Vue.use(VueMask);
`
:
`vue
type="text"
v-mask="mask"
v-model="myInputModel"
placeholder="$100.00"
/>
`
In this example:
- entering 1000000.00 in the input field will produce $1,000,000.00 in myInputModel variable
- while entering 100 in the input field will produce $100
View the createNumberMask documentation for a full list of options available.
:syringe: Tests
Jest is used for unit-tests.
Unit-tests can be executed by typing this command in your terminal:
`bash
npm test
`
TestCafe is used of E2E testing.
E2E-tests can be executed by typing this command in your terminal:
`bash
npm test:e2e
`
:anchor: Semantic Versioning Policy
This plugin follows semantic versioning.
:newspaper: Changelog
We're using GitHub Releases.
:beers: Contributing
We're more than happy to see potential contributions, so don't hesitate. If you have any suggestions, ideas or problems feel free to add new issue, but first please make sure your question does not repeat previous ones.
Notice: You should make your changes only in src folder, don't try to edit files from dist as it compiled from src` by babel and shouldn't be changes manually.