Composables

useMask

Format what the user types in an input, like a card number, a phone number or a time.

Usage

Give useMask a pattern, then bind maskedValue as the value of your input and applyMaskOnInput on its input event.

<script setup lang="ts">
const { maskedValue, unmaskedValue, isComplete, applyMaskOnInput } = useMask('#### #### #### ####')
</script>

<template>
  <input :value="maskedValue" inputmode="numeric" @input="applyMaskOnInput" />
  <p>{{ unmaskedValue }} — {{ isComplete ? 'complete' : 'not complete yet' }}</p>
</template>

Typing 4970123412341234 shows 4970 1234 1234 1234. The mask keeps the caret where it was, removes the characters the pattern refuses, reads pasted text, and lets Backspace go past a separator.

With a Text field, you don't need useMask: pass the pattern to its mask prop. With the Nuxt module, useMask is auto-imported. In a Vue app, import it from @kuina-ui/vue.

Writing patterns

Tokens

In a pattern, a token stands for one typed character. Every other character is written as it is.

TokenAccepts
#A digit
@A letter, accented or not
*A letter or a digit
!Writes the next character as it is, so !# writes a #
useMask('##/##/####')
useMask('+261 ## ## ### ##')
useMask('@@-###-@@')

Your own tokens

Pass the pattern in an object with tokens to add a token or replace one. A token has a pattern, the regular expression one character must match, and an optional transform.

useMask({
  pattern: '!#HHHHHH',
  tokens: {
    H: { pattern: /[0-9a-f]/i, transform: (character) => character.toUpperCase() },
  },
})

Typing 1f2a3b shows #1F2A3B.

Several patterns

Pass an array to switch patterns by length: the mask uses the shortest one that fits what was typed.

useMask(['## ## ## ##', '### ### ####'])

Separators added early

By default, a separator is added as soon as the group before it is complete: typing 11 in ##/##/#### shows 11/. Set eager to false to add it only when the next character comes.

useMask({ pattern: '##/##/####', eager: false })

Prepare the text first

Use preprocess to change the text before it is masked, for example to read a date pasted in another layout.

useMask({
  pattern: '##/##/####',
  preprocess: (text) => text.replace(/^(\d{4})-(\d{2})-(\d{2})$/, '$2$3$1'),
})

Values

ValueExampleDescription
maskedValue+261 34 12 345 67What the input shows.
unmaskedValue341234567Only the characters typed for the tokens, without the separators.
isCompletetrueWhether every token of the pattern is filled.

Use setMaskedValue(text) to put a value in the input from your code: it goes through the mask too.

Without an input

createMask gives the same mask outside any component, to format a value you display or check one you receive.

import { createMask } from '@kuina-ui/vue'

const cardNumberMask = createMask('#### #### #### ####')

cardNumberMask.apply('4970123412341234')
// { masked: '4970 1234 1234 1234', unmasked: '4970123412341234', completed: true }

API

useMask(maskOptions, initialText?)
createMask(maskOptions)

Mask options

maskOptions is a pattern, an array of patterns, or an object:

OptionTypeDefault
patternstring | string[]
tokensRecord<string, { pattern: RegExp, transform?: (character) => string }>
eagerbooleantrue
preprocess(text: string) => string

Returned by useMask

ValueType
maskedValueReadonly<Ref<string>>
unmaskedValueComputedRef<string>
isCompleteComputedRef<boolean>
applyMaskOnInput(inputEvent: Event) => void
setMaskedValue(text: string) => void

Returned by createMask

ValueType
apply(text: string, { eager? }) => { masked, unmasked, completed }
findCaretPosition(maskedText: string, acceptedCharacterCount: number) => number
Copyright © 2026