Convert Figma logo to code with AI

vuejs-tips logovue-the-mask

Tiny (<2k gzipped) and dependency free mask input for Vue.js

1,740
212
1,740
146

Top Related Projects

1,798

Simple zero-dependency input mask for Vue, Svelte, Alpine.js and vanilla JS.

17,957

Format input text content when you are typing...

Quick Overview

Vue-the-mask is a lightweight and flexible input mask library for Vue.js applications. It provides an easy way to format and validate user input for various types of data, such as phone numbers, dates, and credit card numbers, enhancing the user experience and data integrity in forms.

Pros

  • Simple integration with Vue.js components
  • Lightweight and performant
  • Customizable mask patterns
  • Supports both Vue 2 and Vue 3

Cons

  • Limited built-in mask patterns (requires custom patterns for complex scenarios)
  • May not handle all international input formats out of the box
  • Documentation could be more comprehensive
  • Lacks advanced features like dynamic masking

Code Examples

  1. Basic usage with a phone number mask:
<template>
  <the-mask
    v-model="phoneNumber"
    mask="(###) ###-####"
    placeholder="Enter phone number"
  />
</template>

<script>
import { TheMask } from 'vue-the-mask'

export default {
  components: { TheMask },
  data() {
    return {
      phoneNumber: ''
    }
  }
}
</script>
  1. Custom mask for a date input:
<template>
  <the-mask
    v-model="date"
    mask="##/##/####"
    placeholder="DD/MM/YYYY"
  />
</template>

<script>
import { TheMask } from 'vue-the-mask'

export default {
  components: { TheMask },
  data() {
    return {
      date: ''
    }
  }
}
</script>
  1. Using tokens for more complex masks:
<template>
  <the-mask
    v-model="customInput"
    :mask="['(##) ####-####', '(##) #####-####']"
    :tokens="customTokens"
    placeholder="Enter custom input"
  />
</template>

<script>
import { TheMask } from 'vue-the-mask'

export default {
  components: { TheMask },
  data() {
    return {
      customInput: '',
      customTokens: {
        '#': { pattern: /\d/ },
        'X': { pattern: /[0-9a-zA-Z]/ }
      }
    }
  }
}
</script>

Getting Started

  1. Install the package:

    npm install vue-the-mask
    
  2. Import and use in your Vue component:

    <template>
      <the-mask v-model="value" mask="##.##.##" />
    </template>
    
    <script>
    import { TheMask } from 'vue-the-mask'
    
    export default {
      components: { TheMask },
      data() {
        return {
          value: ''
        }
      }
    }
    </script>
    
  3. For global registration (in your main.js or similar):

    import Vue from 'vue'
    import VueTheMask from 'vue-the-mask'
    
    Vue.use(VueTheMask)
    

Competitor Comparisons

1,798

Simple zero-dependency input mask for Vue, Svelte, Alpine.js and vanilla JS.

Pros of maska

  • Supports both Vue 2 and Vue 3, offering broader compatibility
  • Provides more customization options, including custom tokens and patterns
  • Smaller bundle size, potentially improving performance

Cons of maska

  • Less mature project with fewer GitHub stars and contributors
  • Documentation is not as comprehensive as vue-the-mask

Code Comparison

maska:

<template>
  <input v-maska="'##-##-####'" />
</template>

<script>
import { maska } from 'maska'

export default {
  directives: { maska }
}
</script>

vue-the-mask:

<template>
  <the-mask :mask="['##-##-####']" />
</template>

<script>
import TheMask from 'vue-the-mask'

export default {
  components: { TheMask }
}
</script>

Both libraries offer similar functionality for input masking, but maska provides a directive-based approach, while vue-the-mask uses a component-based implementation. maska's syntax is more concise and allows for easier integration with existing input elements. However, vue-the-mask's component approach may be more familiar to some Vue developers and offers a cleaner separation of concerns in certain scenarios.

17,957

Format input text content when you are typing...

Pros of Cleave.js

  • Framework-agnostic, can be used with various JavaScript frameworks or vanilla JS
  • Supports more input types (credit cards, dates, numerals, phones)
  • Larger community and more frequent updates

Cons of Cleave.js

  • Requires more setup for Vue.js projects compared to Vue-the-mask
  • Larger file size, which may impact performance for smaller projects
  • Less Vue-specific documentation and examples

Code Comparison

Vue-the-mask:

<template>
  <input v-mask="'##/##/####'" type="text" />
</template>

<script>
import { mask } from 'vue-the-mask'
export default {
  directives: { mask }
}
</script>

Cleave.js:

<template>
  <cleave v-model="date" :options="options" />
</template>

<script>
import Cleave from 'cleave.js'
export default {
  data() {
    return {
      date: '',
      options: { date: true, datePattern: ['d', 'm', 'Y'] }
    }
  }
}
</script>

Both libraries offer input masking functionality, but Cleave.js provides more flexibility and options for various input types. Vue-the-mask is more lightweight and Vue-specific, making it easier to integrate into Vue projects. The choice between the two depends on project requirements, desired features, and the development ecosystem.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The Mask

A lightweight (2KB gzipped) and dependency free mask input created specific for Vue.js

Docs and Demo

JsFiddle

The Mask Heart

Install

yarn add vue-the-mask
or
npm i -S vue-the-mask

Usage (two flavors)

Global

import VueTheMask from 'vue-the-mask'
Vue.use(VueTheMask)

Local (inside the component)

import {TheMask} from 'vue-the-mask'
export default {
  components: {TheMask}
}

Local (as directive)

import {mask} from 'vue-the-mask'
export default {
  directives: {mask}
}

Tokens

'#': {pattern: /\d/},
'X': {pattern: /[0-9a-zA-Z]/},
'S': {pattern: /[a-zA-Z]/},
'A': {pattern: /[a-zA-Z]/, transform: v => v.toLocaleUpperCase()},
'a': {pattern: /[a-zA-Z]/, transform: v => v.toLocaleLowerCase()},
'!': {escape: true}

The Mask Money

Properties

PropertyRequiredTypeDefaultDescription
valuefalseStringInput value or v-model
masktrueString, ArrayMask pattern
maskedfalseBooleanfalseemit value with mask chars, default is raw
placeholderfalseStringSame as html input
typefalseString'text'Input type (email, tel, number, ...)
tokensfalseObjecttokensCustom tokens for mask

What about money?

We understand that money format is a totally different domain, which needs another specific component. You can use https://github.com/vuejs-tips/v-money

The Mask Money

Other Solutions

  1. https://github.com/nosir/cleave.js https://github.com/nosir/cleave.js
  2. https://github.com/text-mask/text-mask https://github.com/text-mask/text-mask
  3. https://github.com/igorescobar/jQuery-Mask-Plugin https://github.com/igorescobar/jQuery-Mask-Plugin
  4. https://github.com/fernandofleury/vanilla-masker https://github.com/fernandofleury/vanilla-masker
  5. https://github.com/angular-ui/ui-mask https://github.com/angular-ui/ui-mask
  6. https://github.com/insin/inputmask-core https://github.com/insin/inputmask-core
  7. https://github.com/niksmr/vue-masked-input https://github.com/niksmr/vue-masked-input
  8. https://github.com/probil/v-mask https://github.com/probil/v-mask
  9. https://github.com/moip/awesome-mask https://github.com/moip/awesome-mask
  10. https://github.com/the-darc/string-mask https://github.com/the-darc/string-mask
  11. https://github.com/romulobrasil/PureMask.js https://github.com/romulobrasil/PureMask.js
  12. https://github.com/devindex/vue-mask https://github.com/devindex/vue-mask

Currency

  1. https://github.com/vuejs-tips/v-money https://github.com/vuejs-tips/v-money
  2. https://github.com/plentz/jquery-maskmoney https://github.com/plentz/jquery-maskmoney
  3. https://github.com/flaviosilveira/Jquery-Price-Format https://github.com/flaviosilveira/Jquery-Price-Format
  4. https://github.com/kevinongko/vue-numeric https://github.com/kevinongko/vue-numeric

Suggest another one here

Contribution

You're free to contribute to this project by submitting issues and/or pull requests. This project is test-driven, so keep in mind that every change and new feature should be covered by tests. Your name will be added to the hall of fame ;)

The Mask Wolf

License

This project is licensed under MIT License

NPM DownloadsLast 30 Days