All Projects → probil → V Mask

probil / V Mask

Licence: mit
🔡 Tiny input mask library for Vue.js (directive)

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to V Mask

Vue Touch Ripple
👆 Touch ripple component for @vuejs
Stars: ✭ 443 (-29.68%)
Mutual labels:  vue2
Stresstestplatform
基于Jmeter实现的在线压测和管理Jmx的平台。
Stars: ✭ 515 (-18.25%)
Mutual labels:  vue2
Vue Img Inputer
🏞 A graceful image type inputer / uploader
Stars: ✭ 548 (-13.02%)
Mutual labels:  vue2
Vue Jstree
A Tree Plugin For Vue2.0+
Stars: ✭ 469 (-25.56%)
Mutual labels:  vue2
Pixel Web
一个 Vue 微博客户端
Stars: ✭ 500 (-20.63%)
Mutual labels:  vue2
Vuetron
A tool for testing and debugging your Vue + Vuex applications. 是一個可以幫助您 Vue.js 的項目測試及偵錯的工具, 也同時支持 Vuex及 Vue-Router.
Stars: ✭ 531 (-15.71%)
Mutual labels:  vue2
Vue Datasource
A vue.js component to create dynamic tables
Stars: ✭ 420 (-33.33%)
Mutual labels:  vue2
Istock
👉一个基于spring boot 实现的java股票爬虫(仅支持A股),如果你❤️请⭐️ . V2升级版正在开发中!
Stars: ✭ 622 (-1.27%)
Mutual labels:  vue2
Vue Star Rating
⭐️ A simple, highly customisable star rating component for Vue 2.x. / 3.x
Stars: ✭ 509 (-19.21%)
Mutual labels:  vue2
Vuesax
New Framework Components for Vue.js 2
Stars: ✭ 5,293 (+740.16%)
Mutual labels:  vue2
Vue2 Calendar
vue 2.x calendar component
Stars: ✭ 477 (-24.29%)
Mutual labels:  vue2
Dashboard
A dashboard scaffolding based on Vue.js 3.0 created by Vite.
Stars: ✭ 497 (-21.11%)
Mutual labels:  vue2
Vuelayers
Web map Vue components with the power of OpenLayers
Stars: ✭ 532 (-15.56%)
Mutual labels:  vue2
Vue Drag Drop
A lightweight Vue wrapper that abstracts away the wonkier parts of the Drag and Drop Browser API
Stars: ✭ 444 (-29.52%)
Mutual labels:  vue2
Vue Blog
A single-user blog built with vue2, koa2 and mongodb which supports Server-Side Rendering
Stars: ✭ 586 (-6.98%)
Mutual labels:  vue2
Vue Gl
Vue.js components rendering 3D WebGL graphics reactively with three.js
Stars: ✭ 434 (-31.11%)
Mutual labels:  vue2
Vue Quasar Admin
Vue 2.0 admin-dashboard based on Quasar-Framework
Stars: ✭ 516 (-18.1%)
Mutual labels:  vue2
Cordova Template Framework7 Vue Webpack
Framework7 - Vue - Webpack Cordova Template with Webpack Dev Server and Hot Module Replacement
Stars: ✭ 630 (+0%)
Mutual labels:  vue2
Vue Apollo
🚀 Apollo/GraphQL integration for VueJS
Stars: ✭ 5,529 (+777.62%)
Mutual labels:  vue2
Vue Demi
🎩 Creates Universal Library for Vue 2 & 3
Stars: ✭ 544 (-13.65%)
Mutual labels:  vue2

🔡 Vue input mask

npm npm bundle size npm GitHub license Vue2 jsDelivr Tested with TestCafe

Tiny input mask library for vue.js based on text-mask-core (~5kb) exposed as directive. No dependencies

🎨 Playground on the Web

✔️ Browser Support

Chrome Firefox Safari Opera Edge IE iOS Safari Android WebView Android WebView
74+ ✔️ 66+ ✔️ 12+ ✔️ 46+ ✔️ 17+ ✔️ 11+ ✔️ 12+ ✔️ 67+ ✔️ 8.2+ ✔️

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.

💿 Installation

This version requires Vue 2.X. If you are looking for Vue 1.X, check it here.

npm install v-mask

Initialization

ES2015 (Webpack/Rollup/Browserify/etc)

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)

UMD (Browser)

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/v-mask/dist/v-mask.min.js"></script>
<script>
// As a plugin
Vue.use(VueMask.VueMaskPlugin);

// Or as a directive
Vue.directive('mask', VueMask.VueMaskDirective);
</script>

🚀 Usage

<input type="text" v-mask="'####-##'" v-model="myInputModel">
<!-- OR -->
<input type="text" v-mask="nameOfVariableWithMask" 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

<span>{{ '9999999999' | VMask('(###) ###-####') }}</span>

⚙️ 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:

import Vue from 'vue'
import VueMask from 'v-mask'
Vue.use(VueMask)

<your_component>.vue:

<template>
  <input type="text" v-mask="'####-##'" v-model="myInputModel">
</template>
<script>
  export default {
    data: () => ({
      myInputModel: ''
    })
  }
</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 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:

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
  }
})

<your_component>.vue:

<template>
  <input type="text" v-mask="'###-DDD-###-DDD'" v-model="myInputModel">
</template>
<script>
  export default {
    data: () => ({
      myInputModel: ''
    })
  }
</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 excursions or static characters to v-mask.

app.js:

import Vue from 'vue'
import VueMask from 'v-mask'
Vue.use(VueMask)

<your_component>.vue:

<template>
  <input type="text" v-mask="mask" v-model="myInputModel">
</template>
<script>
  export default {
    data: () => ({
      mask: ['(', /\d/, /\d/, /\d/, ') ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/],
      myInputModel: ''
    })
  }
</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 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:

import Vue from 'vue'
import VueMask from 'v-mask'
Vue.use(VueMask)

<your_component>.vue:

<template>
  <input type="text" v-mask="mask" v-model="myInputModel" placeholder="00:00-23:59">
</template>
<script>
  /**
   * Generate a time mask based on input value (23:59)
   * @param {string} value
   */
  export function timeMask(value) {
    const hours = [
      /[0-2]/,
      value.charAt(0) === '2' ? /[0-3]/ : /[0-9]/,
    ];
    const minutes = [/[0-5]/, /[0-9]/];
    return value.length > 2
      ? [...hours, ':', ...minutes]
      : hours;
  }

  /**
   * Generate a time range mask based on input value (00:00-23:59)
   * @param {string} value
   */
  export function timeRangeMask(value) {
    const numbers = value.replace(/[^0-9]/g, '');
    if (numbers.length > 4) {
      return [...timeMask(numbers.substring(0, 4)), '-', ...timeMask(numbers.substring(4))];
    }
    return [...timeMask(numbers)];
  }

  export default {
    data: () => ({
      mask: timeRangeMask,
      myInputModel: ''
    })
  }
</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 want and pass the result to the v-mask as you would to text-mask-core.

app.js:

import Vue from 'vue'
import VueMask from 'v-mask'
Vue.use(VueMask)

<your_component>.vue:

<template>
  <input type="text" v-mask="mask" v-model="myInputModel" placeholder="$100.00">
</template>
<script>
  import createNumberMask from 'text-mask-addons/dist/createNumberMask';
  const currencyMask = createNumberMask({
    prefix: '$',
    allowDecimal: true,
    includeThousandsSeparator: true,
    allowNegative: false,
  });
  export default {
    data: () => ({
      mask: currencyMask,
      myInputModel: ''
    })
  }
</script>

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.

💉 Tests

Jest is used for unit-tests.

Unit-tests can be executed by typing this command in your terminal:

npm test

TestCafe is used of E2E testing.

E2E-tests can be executed by typing this command in your terminal:

npm test:e2e

⚓️ Semantic Versioning Policy

This plugin follows semantic versioning.

📰 Changelog

We're using GitHub Releases.

🍻 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.

🔒 License

See the LICENSE file for license rights and limitations (MIT).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].