OTP 输入框

通过单个无障碍输入框收集一次性密码、PIN 码和验证码,并将其渲染为独立的数字槽位。

Requires JS
Layer: components

概览

OTP(一次性密码)输入框是双重身份验证、验证码和 PIN 输入的常见模式。Bootstrap 的 OTP 输入框围绕单个真实的文本字段构建,并在视觉上渲染为独立的数字槽位。辅助技术和自动填充与文本输入框交互。槽位显示其值和选区。

  • 一个无障碍控件:整个字段由一个 <input> 支撑,因此辅助技术朗读的是一个字段,而不是每个数字一个字段
  • 浏览器自动填充:支持 autocomplete="one-time-code" 用于短信/电子邮件验证码自动填充
  • 粘贴支持:粘贴完整验证码——甚至是像 123-456 这样带格式的验证码——多余的字符会被自动去除
  • 点击编辑:点击任意槽位跳转到该位置;输入会就地覆盖该数字,而不是将其他数字向后推
  • 键盘编辑:使用方向键、选择文本、删除字符或撤销输入
  • 掩码和类型:可选地掩码值,并将输入限制为数字、字母数字或纯字母

OTP 输入框基于表单控件构建,并共享其尺寸 tokens。

工作原理

将单个 <input> 包裹在带有 .otp 的容器中,并添加 data-bs-otp 以启用 JavaScript。插件读取输入框的 maxlength 来确定渲染的槽位数量,通过透明的覆盖样式隐藏真实输入框,并为每个字符绘制一个 .otp-slot。使用 .otp-sm 或 .otp-lg 匹配小号或大号表单控件尺寸。使用 aria-label 或关联的 <label> 为输入框提供无障碍名称。

HTML
<div class="otp" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6" inputmode="numeric" autocomplete="one-time-code" aria-label="Verification code">
</div>

连接的输入框

添加 .otp-connected 将槽位合并为具有共享边框的单一连贯字段。

HTML
<div class="otp otp-connected" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6" aria-label="Verification code">
</div>

四位 PIN

设置输入框的 maxlength 来控制槽位数量——对于 4 位 PIN 等较短的验证码可以使用更少的槽位。

HTML
<div class="otp otp-connected" data-bs-otp>
  <input type="text" class="otp-input" maxlength="4" aria-label="PIN">
</div>

带分隔符

使用 data-bs-groups 将诸如 123-456 的验证码拆分为组。结合 .otp-connected 可连接每组内的槽位。分隔符是装饰性的。

HTML
<div class="otp otp-connected" data-bs-otp data-bs-groups="[3,3]">
  <input type="text" class="otp-input" maxlength="6" aria-label="Verification code">
</div>

字母数字验证码

OTP 输入框默认接受数字。设置 data-bs-type="alphanumeric" 接受字母和数字,或设置 data-bs-type="alpha" 只接受字母。

HTML
<div class="otp otp-connected" data-bs-otp data-bs-type="alphanumeric">
  <input type="text" class="otp-input" maxlength="6" aria-label="Alphanumeric code">
</div>

掩码

设置 data-bs-mask="true" 为每个字符显示一个点。文本输入框仍然支持数字键盘和自动填充。

HTML
<div class="otp otp-connected" data-bs-otp data-bs-mask="true">
  <input type="text" class="otp-input" maxlength="6" aria-label="Masked code">
</div>

禁用

在 input 上添加 disabled 属性以阻止交互。

HTML
<div class="otp otp-connected" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6" aria-label="Verification code" disabled>
</div>

验证

向容器添加 .is-valid 或 .is-invalid 以显示验证状态。当输入框有错误时,在其上设置 aria-invalid="true"。使用原生 pattern 属性验证完整验证码。

HTML
<div class="otp otp-connected is-valid mb-3" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6" value="123456" aria-label="Verification code">
</div>
<div class="otp otp-connected is-invalid" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6" value="123" aria-label="Verification code" aria-invalid="true">
</div>

配合表单标签

将 <label> 与输入框关联,并使用 aria-describedby 添加帮助文本。为输入框提供 name,以便随表单提交验证码。使用 pattern 检查长度和允许的字符。添加 required 以防止空提交。

Enter the 6-digit code sent to your phone.
HTML
<div class="vstack gap-2">
  <label class="form-label" for="otpCode">Verification code</label>
  <div class="otp" data-bs-otp>
    <input type="text" class="otp-input" id="otpCode" name="code" placeholder="000000" pattern="[0-9]{6}" maxlength="6" inputmode="numeric" autocomplete="one-time-code" aria-describedby="otpHelp" required>
  </div>
  <div id="otpHelp" class="form-text">Enter the 6-digit code sent to your phone.</div>
</div>

如果密码管理器徽章与最后一个槽位重叠,请使用 .otp-badge-space。该类会在输入框末尾预留空间。

用法

通过 data 属性

在包裹单个 <input> 的容器上添加 data-bs-otp,即可自动初始化 OTP 输入框行为。

属性描述
data-bs-otp在容器上初始化 OTP 输入框。
data-bs-length槽位数量。默认为输入框的 maxlength。
data-bs-typenumeric(默认)、alphanumeric 或 alpha。
data-bs-mask当为 true 时,在每个槽位中用点掩码输入的值。
data-bs-groups组大小数组(例如 [3,3]),用于在组之间插入分隔符。
data-bs-separator用于组之间分隔符的字符。
HTML
          <div class="otp" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6" aria-label="Verification code">
</div>
        

通过 JavaScript

使用 JavaScript 手动初始化:

JavaScript
          import { OtpInput } from 'bootstrap'

const otpElement = document.querySelector('.otp')
const otpInput = OtpInput.getOrCreateInstance(otpElement)
        

依赖项

The OTP input plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source:

FileDescription
js/src/otp-input.tsMain otp-input component
js/src/base-component.tsBase component class
js/src/dom/data.tsElement data store
js/src/dom/event-handler.tsEvent handling utilities
js/src/dom/manipulator.tsData attribute manipulation
js/src/dom/selector-engine.tsDOM selector utilities
js/src/util/component-functions.tsShared component helpers
js/src/util/config.tsConfiguration base class
js/src/util/index.tsCore utility functions

选项

选项可以通过 data 属性或 JavaScript 传递:

名称类型默认值描述
lengthnumber, nullnull槽位数量。当为 null 时,从输入框的 maxlength 读取。
typestring'numeric'允许的字符:numeric、alphanumeric 或 alpha。为 inputmode 和 pattern 提供默认值。在输入框上设置这些属性可覆盖默认值。
maskbooleanfalse如果为 true,槽位显示点(•)而不是输入的字符。
groupsarray, nullnull用于插入分隔符的组大小(例如 [3, 3])。
separatorstring'·'组之间渲染的字符。
pasteTransformerfunction, nullnullJavaScript 选项,在组件过滤剪贴板文本之前对其进行转换。

方法

方法描述
getValue()以字符串形式返回完整的 OTP 值。
setValue(value)设置值,并根据配置的类型对其进行清理。
clear()清除值并使输入框获得焦点。
focus()使输入框获得焦点并将光标放在最后一个字符之后。
dispose()销毁组件实例并移除生成的槽位。
JavaScript
          import { OtpInput } from 'bootstrap'

const otpElement = document.querySelector('.otp')
const otpInput = OtpInput.getOrCreateInstance(otpElement)

// Read the current value
const code = otpInput.getValue()

// Set a value programmatically
otpInput.setValue('654321')

// Clear the value
otpInput.clear()
        

事件

将验证码视为凭据。不要将事件值写入日志和分析。

事件描述
complete.bs.otpInput当值变为完整验证码时触发。事件的 value 属性包含完整验证码。
input.bs.otpInput每次变化时触发。事件的 value 属性包含当前值。
JavaScript
          const otpElement = document.querySelector('.otp')

// Call your app's verification handler when the code is complete.
otpElement.addEventListener('complete.bs.otpInput', event => {
  verifyCode(event.value)
})
        

无障碍

由于该控件是单个文本输入框,它只暴露一个无障碍名称、角色和值——因此辅助技术会将其作为一个普通文本字段朗读,而不是每个数字一个编辑字段。视觉槽位是装饰性的,并标记为 aria-hidden,无需逐个数字的 aria-label。

  • 使用 aria-label 或关联的 <label> 为输入框提供单个无障碍名称,并使用 aria-describedby 添加上下文。
  • 组件会设置 autocomplete="one-time-code" 和适当的 inputmode,以便自动填充验证码并显示正确的移动键盘。在标记中同时添加这两个属性,使其在插件加载前即可工作。
  • 输入框支持复制、粘贴和自动填充。在你的验证流程中让用户粘贴或自动填充完整验证码。
  • 对于验证流程本身,为验证码设置充裕的有效期(WCAG 2.2.1 时间可调),并避免强制用户重新输入同一流程中已提供的验证码(WCAG 3.3.7 冗余输入)。

CSS

变量

OTP input use local CSS variables on .otp for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.

// stylelint-disable custom-property-no-missing-var-function
// stylelint-disable-next-line scss/dollar-variable-default
$otp-tokens: defaults(
  (
    --otp-size: var(--btn-input-lg-min-height),
    --otp-font-size: var(--btn-input-font-size),
    --otp-gap: .5rem,
    --otp-slot-fg: var(--btn-input-fg),
    --otp-slot-bg: var(--btn-input-bg),
    --otp-slot-border-width: var(--border-width),
    --otp-slot-border-color: var(--border-color),
    --otp-slot-border-radius: var(--radius-5),
  ),
  $otp-tokens
);

Sass 列表

我们使用 Sass 列表定义 OTP 输入框的尺寸,因此你可以通过获取每个尺寸关联的根变量轻松添加或修改尺寸。此列表通常应与 _form-control.scss 中的 $form-control-sizes Sass map 中定义的尺寸保持一致。

$otp-sizes: ();
// stylelint-disable-next-line scss/dollar-variable-default
$otp-sizes: defaults(
  ("sm", "lg"),
  $otp-sizes
);