Skip to content

VerifyCode 验证码输入框

用于输入短信验证码、密码等场景,支持自动跳格、粘贴自动填充、倒计时发送等功能。

交互式调试

🔧调试模式Playground

基础用法

vue
<template>
  <lk-verify-code v-model="code" @finish="onFinish" />
</template>

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

const code = ref('');

function onFinish(value) {
  console.log('输入完成:', value);
}
</script>

验证码长度

支持 4、6、8 位验证码长度。

vue
<lk-verify-code v-model="code4" :length="4" />
<lk-verify-code v-model="code6" :length="6" />
<lk-verify-code v-model="code8" :length="8" />

样式变体

提供三种样式变体:方框 (box)、下划线 (underline)、圆角 (rounded)。

vue
<lk-verify-code v-model="code" variant="box" />
<lk-verify-code v-model="code" variant="underline" />
<lk-verify-code v-model="code" variant="rounded" />

密码模式

使用 mask 属性将输入内容显示为圆点。

vue
<lk-verify-code v-model="code" mask />

状态

通过 status 属性设置不同状态。

vue
<!-- 错误状态 -->
<lk-verify-code v-model="code" status="error" error-message="验证码错误" />

<!-- 成功状态 -->
<lk-verify-code v-model="code" status="success" />

<!-- 禁用状态 -->
<lk-verify-code v-model="code" disabled />

倒计时发送

开启 countdown 属性显示发送验证码按钮和倒计时功能。

vue
<template>
  <lk-verify-code
    v-model="code"
    countdown
    :countdown-duration="60"
    @send="onSend"
    @resend="onResend"
    @countdown-end="onCountdownEnd"
  />
</template>

<script setup>
function onSend() {
  // 发送验证码逻辑
}

function onResend() {
  // 重新发送验证码逻辑
}

function onCountdownEnd() {
  // 倒计时结束
}
</script>

自定义颜色

vue
<lk-verify-code v-model="code" focus-color="#007AFF" error-color="#FF3B30" />

方法调用

通过 ref 调用组件方法。

vue
<template>
  <lk-verify-code ref="verifyCodeRef" v-model="code" />
  <button @click="handleSetValue">设置值</button>
</template>

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

const verifyCodeRef = ref();
const code = ref('');

function handleSetValue() {
  // 设置验证码值(支持 SMS 自动填充)
  verifyCodeRef.value.setValue('123456');
}
</script>

API

Props

参数说明类型默认值
customClass组件可视根节点自定义类名string | object | array''
customStyle组件可视根节点自定义样式string | object''
v-model绑定值string''
length验证码长度4 | 6 | 86
type输入类型'number' | 'text''number'
variant样式变体'box' | 'underline' | 'rounded''box'
mask是否密码模式booleanfalse
autofocus是否自动聚焦booleantrue
disabled是否禁用booleanfalse
status状态'default' | 'focus' | 'error' | 'success''default'
errorMessage错误提示文字string''
showCursor是否显示光标booleantrue
countdown是否开启倒计时booleanfalse
countdownDuration倒计时时长(秒)number60
sendText发送按钮文字string'获取验证码'
resendText重新发送按钮文字string'重新获取'
countdownText倒计时文字模板string'{s}s后重新获取'
gap单元格间距 (rpx)number12
cellSize单元格尺寸 (rpx)number96
fontSize字体大小 (rpx)number40
tips提示文字string''
focusColor聚焦时边框颜色string''
errorColor错误时边框颜色string''

Events

事件名说明回调参数
update:modelValue值变化时触发(value: string)
finish输入完成时触发(value: string)
send点击发送按钮时触发-
resend点击重新发送按钮时触发-
focus聚焦时触发-
blur失焦时触发-
countdownEnd倒计时结束时触发-

Methods

方法名说明参数
focus使输入框聚焦-
blur使输入框失焦-
clear清空输入内容-
setValue设置验证码值(支持 SMS 自动填充)(code: string)
startCountdown开始倒计时-
stopCountdown停止倒计时-

主题定制

组件使用以下 CSS 变量,可通过修改主题变量来自定义样式:

css
--lk-color-primary         /* 主色调 */
--lk-color-border          /* 边框颜色 */
--lk-text-primary            /* 文字颜色 */
--lk-text-secondary  /* 次要文字颜色 */
--lk-fill-tertiary   /* 填充色 */
--lk-radius-lg             /* 圆角大小 */

基于 MIT 协议开源 · 粤ICP备2022114240号-3

VerifyCode 验证码输入框

正在连接预览服务...

请先运行 pnpm run dev:h5