Input OTP VInputOTP edits a code one character per box. Its v-model contains the characters without separators.
Usage <script setup lang="ts">
import { ref } from 'vue'
import { VInputOTP } from 'vectis-ui'
const code = ref('')
</script>
<template>
<VInputOTP v-model="code" :length="6" label="Verification code" />
</template> Examples Label and hint label displays a label above the boxes and names the group. hint displays help below them.
Check your phone We sent a six digit code to +33 6 12 34 56 78.
<script setup lang="ts">
import { ref } from 'vue'
import { VInputOTP, VTypography } from 'vectis-ui'
const code = ref('')
</script>
<template>
<div class="column">
<VTypography as="h3" variant="heading-4">Check your phone</VTypography>
<VTypography tone="muted">We sent a six digit code to +33 6 12 34 56 78.</VTypography>
<VInputOTP
v-model="code"
:length="6"
label="Verification code"
hint="The code lasts ten minutes. Ask for another one if it has expired."
/>
</div>
</template>
<style scoped>
.column {
display: grid;
justify-items: start;
gap: var(--vectis-space-3);
}
</style> Sizes size sets box dimensions.
<script setup lang="ts">
import { VInputOTP, VTypography } from 'vectis-ui'
const sizes = ['sm', 'md', 'lg'] as const
</script>
<template>
<div class="column">
<div v-for="size in sizes" :key="size" class="row">
<VInputOTP :length="4" :size="size" :label="`Code, ${size}`" />
<VTypography variant="caption" tone="muted">{{ size }}</VTypography>
</div>
</div>
</template>
<style scoped>
.column {
display: grid;
gap: var(--vectis-space-4);
}
.row {
display: flex;
align-items: center;
gap: var(--vectis-space-4);
}
</style> Length length sets the box count. A pattern containing # takes precedence.
<script setup lang="ts">
import { VInputOTP, VTypography } from 'vectis-ui'
</script>
<template>
<div class="column">
<div class="row">
<VInputOTP :length="4" label="Four digit code" />
<VTypography variant="caption" tone="muted">4, a PIN</VTypography>
</div>
<div class="row">
<VInputOTP :length="6" label="Six digit code" />
<VTypography variant="caption" tone="muted">6, the default</VTypography>
</div>
<div class="row">
<VInputOTP :length="8" label="Eight digit code" />
<VTypography variant="caption" tone="muted">8, a backup code</VTypography>
</div>
</div>
</template>
<style scoped>
.column {
display: grid;
gap: var(--vectis-space-4);
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--vectis-space-4);
}
</style> format accepts numeric, alphabetic or alphanumeric input. Letters are converted to uppercase.
<script setup lang="ts">
import { ref } from 'vue'
import { VInputOTP } from 'vectis-ui'
const pin = ref('')
const word = ref('')
const key = ref('')
</script>
<template>
<div class="column">
<VInputOTP
v-model="pin"
:length="6"
format="numeric"
label="Numeric code"
hint="Digits only, and a phone offers its number pad"
/>
<VInputOTP
v-model="word"
:length="5"
format="alpha"
label="Letter code"
hint="Letters only, typed in either case and kept in capitals"
/>
<VInputOTP
v-model="key"
:length="6"
format="alphanumeric"
label="Licence key"
hint="Letters and digits, again in capitals"
/>
</div>
</template>
<style scoped>
.column {
display: grid;
gap: var(--vectis-space-5);
}
</style> Pattern Each # creates a box. Other characters are displayed separators excluded from the value.
<script setup lang="ts">
import { ref } from 'vue'
import { VInputOTP } from 'vectis-ui'
const grouped = ref('')
const prefixed = ref('')
const reference = ref('')
</script>
<template>
<div class="column">
<VInputOTP
v-model="grouped"
pattern="###-###"
label="Grouped code"
:hint="`Two groups of three. Value: ${grouped || 'empty'}`"
/>
<VInputOTP
v-model="prefixed"
pattern="GT-###"
format="alphanumeric"
label="Ticket reference"
:hint="`Three characters after the prefix. Value: ${prefixed || 'empty'}`"
/>
<VInputOTP
v-model="reference"
pattern="###.###.###"
label="Payment reference"
:hint="`Nine digits in three groups. Value: ${reference || 'empty'}`"
/>
</div>
</template>
<style scoped>
.column {
display: grid;
gap: var(--vectis-space-5);
}
</style> Separators separatorIcon replaces every literal in the pattern. Use it when separators contain no meaningful text.
<script setup lang="ts">
import { VInputOTP } from 'vectis-ui'
import { chevron_right as chevronRight, close } from 'vectis-ui/icons'
</script>
<template>
<div class="column">
<VInputOTP pattern="###-###" label="Grouped code" hint="The literal, as written" />
<VInputOTP
pattern="###-###"
:separator-icon="chevronRight"
label="Grouped code with a chevron"
hint="One icon in place of the dash"
/>
<VInputOTP
pattern="###.###.###"
:separator-icon="close"
label="Payment reference with a cross"
hint="Both literals take the same icon"
/>
</div>
</template>
<style scoped>
.column {
display: grid;
gap: var(--vectis-space-5);
}
</style> Pasting and autofill Paste a full code into any box. The first box uses autocomplete="one-time-code" for autofill.
Copy GT-4F2, then paste it anywhere in the row. Copy
<script setup lang="ts">
import { ref } from 'vue'
import { VButton, VInputOTP, VTypography } from 'vectis-ui'
const SAMPLE = 'GT-4F2'
const code = ref('')
const copied = ref(false)
async function copy() {
await navigator.clipboard.writeText(SAMPLE)
copied.value = true
}
</script>
<template>
<div class="column">
<div class="row">
<VTypography as="span"
>Copy <code>{{ SAMPLE }}</code
>, then paste it anywhere in the row.</VTypography
>
<VButton variant="outline" tone="neutral" size="sm" @click="copy">
{{ copied ? 'Copied' : 'Copy' }}
</VButton>
</div>
<VInputOTP
v-model="code"
pattern="GT-###"
format="alphanumeric"
label="Ticket reference"
:hint="`Value: ${code || 'empty'}`"
/>
</div>
</template>
<style scoped>
.column {
display: grid;
gap: var(--vectis-space-4);
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--vectis-space-3);
}
</style> Reading the code Characters fill consecutive boxes. Deleting a character shifts following characters left. complete emits the completed code when it changes.
<script setup lang="ts">
import { ref } from 'vue'
import { VInputOTP, VTypography } from 'vectis-ui'
const EXPECTED = '481902'
const code = ref('')
const verdict = ref<'right' | 'wrong' | null>(null)
function verify(value: string) {
verdict.value = value === EXPECTED ? 'right' : 'wrong'
}
</script>
<template>
<div class="column">
<VInputOTP
v-model="code"
:length="6"
:invalid="verdict === 'wrong'"
label="Verification code"
hint="Try 481902"
@complete="verify"
@update:model-value="verdict = null"
/>
<VTypography variant="body-sm" tone="muted">
Value: {{ code || 'empty' }} ({{ code.length }} of 6)
</VTypography>
<VTypography v-if="verdict === 'right'" variant="body-sm" tone="success">
The code matches.
</VTypography>
<VTypography v-else-if="verdict === 'wrong'" variant="body-sm" tone="danger">
That code is not the one we sent.
</VTypography>
</div>
</template>
<style scoped>
.column {
display: grid;
justify-items: start;
gap: var(--vectis-space-3);
}
</style> Pass name, form and required for native form handling. Partial codes fail validation; an empty code is allowed without required.
<script setup lang="ts">
import { ref } from 'vue'
import { VButton, VInputOTP, VTypography } from 'vectis-ui'
const code = ref('')
const submitted = ref<string | null>(null)
/* A partial code fails native validation; required also rejects an empty code. */
function onSubmit(event: Event) {
const data = new FormData(event.target as HTMLFormElement)
submitted.value = String(data.get('code'))
}
</script>
<template>
<form class="column" @submit.prevent="onSubmit">
<VInputOTP
v-model="code"
name="code"
required
:length="6"
label="Verification code"
hint="Submitting with empty boxes is refused"
/>
<VButton type="submit">Verify</VButton>
<VTypography v-if="submitted" variant="body-sm" tone="muted">
The form sent code={{ submitted }}
</VTypography>
</form>
</template>
<style scoped>
.column {
display: grid;
justify-items: start;
gap: var(--vectis-space-3);
}
</style> States disabled prevents interaction. readonly preserves focus and copying. invalid marks the code as rejected.
<script setup lang="ts">
import { VInputOTP } from 'vectis-ui'
</script>
<template>
<div class="column">
<VInputOTP
model-value="481902"
:length="6"
disabled
label="Disabled code"
hint="Out of reach entirely"
/>
<VInputOTP
model-value="481902"
:length="6"
readonly
label="Read-only code"
hint="Shown, and copyable, but not editable"
/>
<VInputOTP
model-value="481900"
:length="6"
invalid
label="Rejected code"
hint="That code is not the one we sent"
/>
</div>
</template>
<style scoped>
.column {
display: grid;
gap: var(--vectis-space-5);
}
</style> API Props Events Types Types used in the API tables. Import exported types from vectis-ui.
export interface BuiltinIcon {
name: string
paths: readonly [string] | readonly [string, string]
} export type IconRender =
| { path: string; viewBox?: string }
| { component: Component; props?: Record<string, unknown> }
| { src: string }
| { text: string; class?: string }
| { class: string } export type IconSource = string | BuiltinIcon | IconRender