InputOtp 验证码 
输入验证码
基础用法 
使用v-model绑定数组即可
vue
<template>
  <el-input-otp v-model="value" @input="handleInput" />
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
const value = ref([5, 9, 4, 2, 0, 6])
const handleInput = (value: string, index: number) => {
  ElMessage(`input: ${value}-${index}`)
}
</script>
隐藏源代码
状态 
使用ElInput组件的原始属性即可
只读状态、禁用状态、密码
vue
<template>
  <p>只读状态、禁用状态、密码</p>
  <el-input-otp v-model="value" readonly /> <br />
  <el-input-otp v-model="value" disabled /> <br />
  <el-input-otp v-model="value" show-password /> <br />
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const value = ref([5, 9, 4, 2, 0, 6])
</script>
隐藏源代码
大小 
相比ElInput组件的size属性值,多了一个auto值
小、默认、大、铺平
vue
<template>
  <p>小、默认、大、铺平</p>
  <el-input-otp v-model="value" size="small" /> <br />
  <el-input-otp v-model="value" /> <br />
  <el-input-otp v-model="value" size="large" /> <br />
  <el-input-otp v-model="value" size="auto" /> <br />
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const value = ref([5, 9, 4, 2, 0, 6])
</script>
隐藏源代码
位数 
使用length属性来控制控件的个数
vue
<template>
  <el-input-otp v-model="value" :length="4" />
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const value = ref([])
</script>
隐藏源代码
自定义渲染 
使用默认插槽,根据抛出的index及当前输入框的value值,可以做点什么
 - 
 - 
vue
<template>
  <el-input-otp v-model="value">
    <template #default="{ index }">
      <span v-if="index === 1" style="margin-left: 8px"> - </span>
      <span v-if="index === 3" style="margin-left: 8px"> - </span>
    </template>
  </el-input-otp>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const value = ref([5, 9, 4, 2, 0, 6])
</script>
隐藏源代码
校验 
使用validator校验函数,返回false则停止输入
只允许数字输入
只允许英文字母输入
vue
<template>
  <el-space direction="vertical">
    <p>只允许数字输入</p>
    <el-input-otp v-model="value" :validator="verifyNumber" />
    <p>只允许英文字母输入</p>
    <el-input-otp v-model="value2" :validator="verifyEnglish" />
  </el-space>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const value = ref([])
const value2 = ref([])
const verifyNumber = (value: string) => !value || /^\d+$/.test(value)
const verifyEnglish = (value: string) => !value || /^[a-zA-Z]+$/.test(value)
</script>
隐藏源代码
配合 Form 
使用ElForm组件的rules属性即可
vue
<template>
  <el-form
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="验证码" prop="otp" required>
      <el-input-otp v-model="ruleForm.otp" />
    </el-form-item>
    <el-form-item>
      <el-submit type="primary" @submit="handleSubmit"> 提交 </el-submit>
      <el-reset>重置</el-reset>
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { isDef } from 'element-plus-x'
import { ElForm, ElMessage } from 'element-plus'
import type { FormRules } from 'element-plus'
const ruleForm = reactive({
  otp: [],
})
const validateOtp = (rule: any, value: any, callback: any) => {
  if (value.length === 6 && value.every((val) => isDef(val) && val !== '')) {
    return /^\d+$/.test(value.join('').trim())
      ? callback()
      : callback('只能输入数字')
  } else {
    return callback(new Error('请输入完整的内容'))
  }
}
const rules = reactive<FormRules<typeof ruleForm>>({
  otp: [{ validator: validateOtp, trigger: 'blur' }],
})
const handleSubmit = (values) => {
  ElMessage.success(JSON.stringify(values, null, 2))
}
</script>
隐藏源代码
API 
属性 
其他相关 API 属性,请参考 ElInput 即可,剩下则是 InputOtp 组件独有的 API 属性
| 属性名 | 说明 | 类型 | 默认值 | 
|---|---|---|---|
| model-value | 双向绑定值 | Array | - | 
| validator | 校验器 | Function | - | 
| size | 尺寸大小 | String | - | 
| gap | 间隙 | Number | 8 | 
Slots 
| 事件名 | 说明 | 参数 | 
|---|---|---|
| default | 默认内容 | (index: number, value: string) | 
Exposes 
| 名称 | 详情 | 类型 | 
|---|---|---|
| inputRefs | 所有 ElInput 组件实例集合 | - |