Vue 2 Input Mask Plugin
di-vue-mask is a Vue.js plugin that provides input masking capabilities for HTML input elements, specifically designed for Vue 2 applications. It is currently at version 1.2.1 and has not been updated in approximately 5 years. This library differentiates itself by building upon `String-Mask` for its core masking logic and offering a variety of predefined masks such as `v-mask-date`, `v-mask-phone`, `v-mask-decimal`, `v-mask-cpf`, `v-mask-cnpj`, `v-mask-cep`, and `v-mask-cc`, along with a generic `v-mask` directive. Its release cadence is inactive, with the last significant update in v1.1.0 focusing on compatibility. A key characteristic is its strict dependency on Vue 2, meaning it is not compatible with Vue 3 or later versions.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'use')
cause Attempting to use `di-vue-mask` (a Vue 2 plugin) in a Vue 3 application. Vue 3's plugin registration API (`app.use()`) is different from Vue 2's (`Vue.use()`), and the internal directives are incompatible.fixFor Vue 3 projects, use a Vue 3 compatible masking library like `maska`, `vue-imask`, or `vue-3-mask-updated`. This package is exclusively for Vue 2 applications. -
Error: Failed to resolve directive: mask
cause The `di-vue-mask` plugin has not been correctly registered with Vue, or it's being used in a Vue 3 environment where its directives are not recognized.fixEnsure `import VueMask from 'di-vue-mask'; Vue.use(VueMask);` is called before your Vue instance is created. Verify that your project is indeed running Vue 2.
Warnings
- breaking This package is strictly compatible only with Vue 2. It will not work with Vue 3 or later versions due to significant API changes in Vue's core. Consider `maska`, `vue-imask`, `vue-3-mask-updated`, or `ertu-forms` for Vue 3 compatible solutions.
- gotcha When using `di-vue-mask` on custom Vue components that do not inherently support `v-model`, you must use the `v-mask-model` directive instead of `v-model` to ensure proper masking and data binding.
- gotcha The mask definitions rely on specific characters (`0`, `9`, `#`, `A`, `S`, `U`, `L`, `$`). Misinterpreting these characters can lead to masks not behaving as expected. For example, `0` is any number (required), `9` is any number (optional), and `A` is any alphanumeric character.
- gotcha This package is built upon the `String-Mask` library. While generally stable, any underlying issues or limitations in `String-Mask` could potentially manifest in `di-vue-mask`.
Install
-
npm install di-vue-mask -
yarn add di-vue-mask -
pnpm add di-vue-mask
Imports
- VueMask
const VueMask = require('di-vue-mask')import VueMask from 'di-vue-mask'
- Vue.use(VueMask)
new Vue({ components: { VueMask } })Vue.use(VueMask)
Quickstart
import Vue from 'vue';
import VueMask from 'di-vue-mask';
// Register the plugin globally
Vue.use(VueMask);
new Vue({
el: '#app',
data: {
phoneNumber: ''
},
template: `
<div id="app">
<label for="phoneInput">Enter Phone Number (BR):</label>
<input type="text" id="phoneInput" v-model="phoneNumber" v-mask-phone.br>
<p>Raw value: {{ phoneNumber }}</p>
<p>Try entering digits like '11987654321' to see the mask in action.</p>
</div>
`
});