Gathering detailed insights and metrics for v-mask
Gathering detailed insights and metrics for v-mask
Gathering detailed insights and metrics for v-mask
Gathering detailed insights and metrics for v-mask
npm install v-mask
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
903 Stars
1,719 Commits
132 Forks
20 Watching
15 Branches
18 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
JavaScript (87.36%)
HTML (12.64%)
Cumulative downloads
Total Downloads
Last day
-3.7%
12,421
Compared to previous day
Last week
-2.8%
67,518
Compared to previous week
Last month
2.9%
299,624
Compared to previous month
Last year
-6%
3,796,737
Compared to previous year
20
Tiny input mask library for vue.js based on text-mask-core (~5kb) exposed as directive. No dependencies
74+ :heavy_check_mark: | 66+ :heavy_check_mark: | 12+ :heavy_check_mark: | 46+ :heavy_check_mark: | 17+ :heavy_check_mark: | 11+ :heavy_check_mark: | 12+ :heavy_check_mark: | 67+ :heavy_check_mark: | 8.2+ :heavy_check_mark: |
We support only browsers with global usage statistics greater then 1%, last 2 version of each browser but not dead browsers. Library may work in older browser but we don't not guarantee that. You may need addition polyfills to make it work.
This version requires Vue 2.X. If you are looking for Vue 1.X, check it here.
1npm install v-mask
1import Vue from 'vue' 2 3// Prefered: as a plugin (directive + filter) + custom placeholders support 4import VueMask from 'v-mask' 5Vue.use(VueMask); 6 7// Or as a directive-only 8import { VueMaskDirective } from 'v-mask' 9Vue.directive('mask', VueMaskDirective); 10 11// Or only as a filter-only 12import { VueMaskFilter } from 'v-mask' 13Vue.filter('VMask', VueMaskFilter)
1<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script> 2<script src="https://cdn.jsdelivr.net/npm/v-mask/dist/v-mask.min.js"></script> 3<script> 4// As a plugin 5Vue.use(VueMask.VueMaskPlugin); 6 7// Or as a directive 8Vue.directive('mask', VueMask.VueMaskDirective); 9</script>
1<input type="text" v-mask="'####-##'" v-model="myInputModel"> 2<!-- OR --> 3<input type="text" v-mask="variableWithMask" v-model="myInputModel">
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.
The filter accepts a mask similarly to the directive, and might be useful when you need to render a raw value as masked without using an input (e.g. formatting currency).
1<span>{{ '9999999999' | VMask('(###) ###-####') }}</span> 2<!-- or --> 3<span>{{ variableWithRawValue | VMask(variableWithMask) </span>
Library provides several ways to apply the mask.
The first and the easiest one is to use default placeholders.
This approach is good for simple cases. No configuration is required.
app.js
:
1import Vue from 'vue' 2import VueMask from 'v-mask' 3Vue.use(VueMask)
<your_component>.vue
:
1<template> 2 <input type="text" v-mask="'####-##'" v-model="myInputModel"> 3</template> 4<script> 5 export default { 6 data: () => ({ 7 myInputModel: '' 8 }) 9 } 10</script>
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) |
While default placeholders are easy to use and straightforward, in reality we have to deal with more complex cases where validation can be tricky 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 maskvalue
is regular expression used to verify entered symbolYou 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
:
1import Vue from 'vue' 2import VueMask from 'v-mask' 3 4Vue.use(VueMask, { // (!) custom placeholders support requires registration as a plugin to 5 placeholders: { 6 '#': null, // passing `null` removes default placeholder, so `#` is treated as character 7 D: /\d/, // define new placeholder 8 Я: /[\wа-яА-Я]/, // cyrillic letter as a placeholder 9 } 10})
<your_component>.vue
:
1<template> 2 <input type="text" v-mask="'###-DDD-###-DDD'" v-model="myInputModel"> 3 <!-- or with filter --> 4 <span>{{ 123456 | VMask(mask) }}</span> 5</template> 6<script> 7 export default { 8 data: () => ({ 9 myInputModel: '' 10 }) 11 } 12</script>
Entering 123456
in that input field will produce value ###-123-###-456
in myInputModel
variable.
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 expressions or static characters to v-mask
.
app.js
:
1import Vue from 'vue' 2import VueMask from 'v-mask' 3Vue.use(VueMask)
<your_component>.vue
:
1<template> 2 <input type="text" v-mask="mask" v-model="myInputModel"> 3 <!-- or with filter --> 4 <span>{{ 5555551234 | VMask(mask) }}</span> 5</template> 6<script> 7 export default { 8 data: () => ({ 9 mask: ['(', /\d/, /\d/, /\d/, ') ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/], 10 myInputModel: '' 11 }) 12 } 13</script>
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 to verify multiple charters in the same RegExp won't work.
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
:
1import Vue from 'vue' 2import VueMask from 'v-mask' 3Vue.use(VueMask)
<your_component>.vue
:
1<template> 2 <input type="text" v-mask="timeRangeMask" v-model="myInputModel" placeholder="00:00-23:59"> 3 <!-- or with filter --> 4 <span>{{ '02532137' | VMask(timeRangeMask) }}</span> 5</template> 6<script> 7 /** 8 * Generate a time mask based on input value (23:59) 9 * @param {string} value 10 */ 11 export function timeMask(value) { 12 const hours = [ 13 /[0-2]/, 14 value.charAt(0) === '2' ? /[0-3]/ : /[0-9]/, 15 ]; 16 const minutes = [/[0-5]/, /[0-9]/]; 17 return value.length > 2 18 ? [...hours, ':', ...minutes] 19 : hours; 20 } 21 22 /** 23 * Generate a time range mask based on input value (00:00-23:59) 24 * @param {string} value 25 */ 26 export function timeRangeMask(value) { 27 const numbers = value.replace(/[^0-9]/g, ''); 28 if (numbers.length > 4) { 29 return [...timeMask(numbers.substring(0, 4)), '-', ...timeMask(numbers.substring(4))]; 30 } 31 return [...timeMask(numbers)]; 32 } 33 34 export default { 35 data: () => ({ 36 timeRangeMask, 37 myInputModel: '' 38 }) 39 } 40</script>
In this example entering 02532137
in the input field will produce valid time range 02:53-21:37
in myInputModel
variable.
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 you want and pass the result to the v-mask
as you would to text-mask-core
.
app.js
:
1import Vue from 'vue' 2import VueMask from 'v-mask' 3Vue.use(VueMask)
<your_component>.vue
:
1<template> 2 <input type="text" v-mask="currencyMask" v-model="myInputModel" placeholder="$100.00"> 3 <!-- or with filter --> 4 <span>{{ '100' | VMask(currencyMask) }}</span> 5</template> 6<script> 7 import createNumberMask from 'text-mask-addons/dist/createNumberMask'; 8 const currencyMask = createNumberMask({ 9 prefix: '$', 10 allowDecimal: true, 11 includeThousandsSeparator: true, 12 allowNegative: false, 13 }); 14 export default { 15 data: () => ({ 16 currencyMask, 17 myInputModel: '' 18 }) 19 } 20</script>
In this example:
1000000.00
in the input field will produce $1,000,000.00
in myInputModel
variable100
in the input field will produce $100
View the createNumberMask documentation for a full list of options available.
Jest is used for unit-tests.
Unit-tests can be executed by typing this command in your terminal:
1npm test
TestCafe is used of E2E testing.
E2E-tests can be executed by typing this command in your terminal:
1npm test:e2e
This plugin follows semantic versioning.
We're using GitHub Releases.
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. Moreover, adding a proper tests for your PR drastically improves chances of merging.
See the LICENSE file for license rights and limitations (MIT).
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
project is archived
Details
Reason
Found 0/27 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More