Skip to content

VerificationCode 验证码输入

专业的 OTP/验证码输入组件,采用分离式输入格子设计,每个字符独立显示。

基础用法

基础的验证码输入,默认 6 位数字。

vue
<template>
  <ex-verification-code v-model="code" @complete="handleComplete" />
</template>

<script setup>
import { ref } from 'vue'

const code = ref('')

const handleComplete = (value) => {
  console.log('验证码输入完成:', value)
}
</script>

不同长度

通过 length 属性设置验证码长度。

vue
<template>
  <ex-verification-code v-model="code4" :length="4" />
  <ex-verification-code v-model="code6" :length="6" />
  <ex-verification-code v-model="code8" :length="8" />
</template>

输入模式

支持纯数字和字母数字两种输入模式。

纯数字模式(默认)

字母数字模式

vue
<template>
  <ex-verification-code v-model="numericCode" input-mode="numeric" />
  <ex-verification-code v-model="alphanumericCode" input-mode="alphanumeric" />
</template>

不同尺寸

提供三种尺寸:smallmedium(默认)、large

vue
<template>
  <ex-verification-code v-model="code" size="small" />
  <ex-verification-code v-model="code" size="medium" />
  <ex-verification-code v-model="code" size="large" />
</template>

赛博朋克风格变体

提供三种赛博朋克风格变体:neon(霓虹)、ghost(幽灵)、filled(填充)。

vue
<template>
  <ex-verification-code v-model="code" variant="neon" />
  <ex-verification-code v-model="code" variant="ghost" />
  <ex-verification-code v-model="code" variant="filled" />
</template>

密码遮罩

启用 mask 属性可以隐藏输入的字符,适用于敏感验证码场景。使用 show-mask-toggle 显示切换按钮。

vue
<template>
  <ex-verification-code 
    v-model="code" 
    mask 
    :mask-delay="300"
    show-mask-toggle
  />
</template>

自动提交

启用 auto-submit 属性,输入完成后自动触发提交事件。

vue
<template>
  <ex-verification-code 
    v-model="code" 
    auto-submit 
    :auto-submit-delay="500"
    @submit="handleSubmit"
  />
</template>

<script setup>
const handleSubmit = (value) => {
  console.log('自动提交:', value)
}
</script>

分隔符

使用 separator-positions 属性在指定位置显示分隔符,常用于银行卡号、手机号等格式。

-
vue
<template>
  <ex-verification-code 
    v-model="code" 
    :separator-positions="[3]"
    separator="-"
  />
</template>

状态

支持多种状态:禁用、只读、错误、成功、警告、加载中。

禁用状态

1
2
3
4

只读状态

1
2
3
4

错误状态

1
2
3
4
验证码错误

成功状态

1
2
3
4

警告状态

1
2
3
4
vue
<template>
  <ex-verification-code v-model="code" disabled />
  <ex-verification-code v-model="code" readonly />
  <ex-verification-code v-model="code" status="error" error-message="验证码错误" />
  <ex-verification-code v-model="code" status="success" />
  <ex-verification-code v-model="code" status="warning" />
</template>

编程式控制

通过 ref 获取组件实例,调用暴露的方法进行编程式控制。

vue
<template>
  <ex-verification-code ref="codeRef" v-model="code" />
  <ex-button @click="handleFocus">聚焦</ex-button>
  <ex-button @click="handleClear">清空</ex-button>
  <ex-button @click="handleSetValue">设置值</ex-button>
</template>

<script setup>
import { ref } from 'vue'

const code = ref('')
const codeRef = ref(null)

const handleFocus = () => {
  codeRef.value?.focus()
}

const handleClear = () => {
  codeRef.value?.clear()
}

const handleSetValue = () => {
  codeRef.value?.setValue('123456')
}
</script>

API

Props

参数说明类型可选值默认值
model-value / v-model绑定值string''
length验证码长度number1-126
input-mode输入模式stringnumeric / alphanumericnumeric
size尺寸stringsmall / medium / largemedium
variant样式变体stringneon / ghost / filledneon
disabled是否禁用booleanfalse
readonly是否只读booleanfalse
status状态stringdefault / success / warning / errordefault
mask是否启用密码遮罩booleanfalse
show-mask-toggle是否显示遮罩切换按钮booleanfalse
mask-delay遮罩延迟(毫秒)number300
auto-submit是否自动提交booleanfalse
auto-submit-delay自动提交延迟(毫秒)number500
autofocus是否自动聚焦booleanfalse
separator-positions分隔符位置number[][]
separator分隔符字符string'-'
placeholder占位符string''
label标签文本string''
required是否必填booleanfalse
help-text帮助文本string''
error-message错误信息string''
loading是否加载中booleanfalse
cell-width格子宽度string / number
cell-gap格子间距string / number
custom-class自定义类名string''
custom-style自定义样式object
aria-label无障碍标签string''

Events

事件名说明回调参数
update:model-value值变化时触发(value: string)
input输入时触发(value: string)
change值改变时触发(value: string)
complete所有格子填满时触发(value: string)
submit提交时触发(value: string)
focus聚焦时触发(event: FocusEvent, index: number)
blur失焦时触发(event: FocusEvent)
clear清空时触发

Exposes

方法名说明参数返回值
focus聚焦到第一个空格子
blur失焦
clear清空所有格子
setValue设置值(value: string)
getValue获取当前值string
getElement获取容器元素HTMLDivElement | null
submit手动触发提交

键盘操作

按键说明
0-9 / a-z输入字符
Backspace删除当前格子内容或移动到前一个格子
Delete删除当前格子内容并左移后续内容
ArrowLeft移动到前一个格子
ArrowRight移动到后一个格子
Home移动到第一个格子
End移动到最后一个填充的格子

最佳实践

短信验证码场景

使用默认的 6 位数字模式,启用自动提交功能,提升用户体验。

敏感验证码

启用 mask 模式保护用户隐私,可配合 show-mask-toggle 让用户自行切换显示。

移动端优化

  • 组件自动使用数字键盘(inputmode="numeric"
  • 支持 OTP 自动填充(autocomplete="one-time-code"
  • 触摸目标尺寸符合无障碍标准(最小 44x44px)

无障碍

  • 完整的 ARIA 属性支持
  • 键盘导航友好
  • 屏幕阅读器友好
  • 支持 prefers-reduced-motion 媒体查询