Installations
npm install v-mask
Score
99.5
Supply Chain
100
Quality
76.1
Maintenance
100
Vulnerability
100
License
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
14.17.6
NPM Version
6.14.15
Statistics
903 Stars
1,719 Commits
132 Forks
20 Watching
15 Branches
18 Contributors
Updated on 28 Nov 2024
Bundle Size
6.36 kB
Minified
2.48 kB
Minified + Gzipped
Languages
JavaScript (87.36%)
HTML (12.64%)
Total Downloads
Cumulative downloads
Total Downloads
16,981,525
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
20
:abcd: Vue input mask
Tiny input mask library for vue.js based on text-mask-core (~5kb) exposed as directive. No dependencies
:art: Playground on the Web
- https://codesandbox.io/s/m3q1m5yp9x (interactive playground with webpack and ESM)
- https://jsfiddle.net/probil/c6fjjzn6/ (simple interactive playground with UMD)
- https://v-mask-demo.netlify.com/ (just preview)
:heavy_check_mark: Browser Support
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.
:cd: Installation
This version requires Vue 2.X. If you are looking for Vue 1.X, check it here.
1npm install v-mask
Initialization
ES2015 (Webpack/Rollup/Browserify/etc)
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)
UMD (Browser)
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>
:rocket: Usage
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.
Filter usage
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>
:gear: Configuration
Library provides several ways to apply the mask.
The first and the easiest one is to use default placeholders.
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) |
Custom placeholders
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 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
:
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.
Array of RegExp
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.
Function
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.
Text Mask Addons
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:
- entering
1000000.00
in the input field will produce$1,000,000.00
inmyInputModel
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:
1npm test
TestCafe is used of E2E testing.
E2E-tests can be executed by typing this command in your terminal:
1npm 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. Moreover, adding a proper tests for your PR drastically improves chances of merging.
:lock: License
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/release.yml:10
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/probil/v-mask/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/probil/v-mask/release.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
Found 0/27 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 7 are checked with a SAST tool
Reason
11 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-5j4c-8p2g-v4jx
- Warn: Project is vulnerable to: GHSA-g3ch-rx76-35fx
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
3.4
/10
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