@bachdgvn/vue-otp-input

中文文档
1.0.8发布于 2020/06/16MITCommonJS近 30 天下载 1.7万

A fully customizable, one-time password input component for the web built with Vue 2.x.

原始使用文档
文档目录5 节
  1. Installation
  2. Props
  3. Methods
  4. Events
  5. Changelog

vue-otp-input

A fully customizable, OTP(one-time password) input component built with Vue 2.x.

Gifphy

Live Demo

Installation

To install the latest stable version:

npm install --save @bachdgvn/vue-otp-input

Import to main.js:

import OtpInput from "@bachdgvn/vue-otp-input";

Vue.component("v-otp-input", OtpInput);

Code example:

<template>
  <div style="display: flex; flex-direction: row;">
    <v-otp-input
      ref="otpInput"
      input-classes="otp-input"
      separator="-"
      :num-inputs="4"
      :should-auto-focus="true"
      :is-input-num="true"
      @on-change="handleOnChange"
      @on-complete="handleOnComplete"
    />

    <button @click="handleClearInput()">Clear Input</button>
  </div>
</template>

<script>
export default {
  name: 'App',

  methods: {
    handleOnComplete(value) {
      console.log('OTP completed: ', value);
    },
    handleOnChange(value) {
      console.log('OTP changed: ', value);
    },
    handleClearInput() {
      this.$refs.otpInput.clearInput();
    },
  },
};
</script>

<style lang="less">
  .otp-input {
    width: 40px;
    height: 40px;
    padding: 5px;
    margin: 0 10px;
    font-size: 20px;
    border-radius: 4px;
    border: 1px solid rgba(0, 0, 0, 0.3);
    text-align: center;
    &.error {
      border: 1px solid red !important;
    }
  }
  .otp-input::-webkit-inner-spin-button,
  .otp-input::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
  }
</style>

Props

Name
Type Required Default Description
num-inputs number true 4 Number of OTP inputs to be rendered.
separator component
false Provide a custom separator between inputs by passing a component. For instance, <span>-</span> would add - between each input
input-classes className (string) false none Style applied or class passed to each input.
input-type string false "tel" Input type. Optional value: "password", "number", "tel".
should-auto-focus boolean false false Auto focuses input on inital page load.

Methods

Name
Description
clearInput() Use with $refs for clearing all otp inputs, see code example section.

Events

Name
Description
on-change Return OTP code was changing when we made a change in inputs.
on-complete Return OTP code typed in inputs.

Changelog

  • v1.0.8 - Fix #30: Support input type: "password"
  • v1.0.7 - Fix #23: Not accepting numbers from numeric keypad in external keyboard
  • v1.0.6 - Add feature to disallow certain characters like "." or "e".
  • v1.0.5 - Support clearInput() methods for clearing all otp inputs.
  • v1.0.4 - Support @on-change event and fix bug for firing @on-complete every time we press keyboard.
  • v1.0.3 - Fix fist and last character not being modified and pasting OTP codes.
  • v1.0.2 - Update first stable version.

版本与发布记录

@bachdgvn/vue-otp-input 共发布 9 个版本,首次发布于 2019/02/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.8latest2020/06/166 年前
1.0.72020/04/276 年前
1.0.62020/02/196 年前
1.0.52020/02/096 年前
1.0.42020/02/016 年前
1.0.32020/02/016 年前

关于 @bachdgvn/vue-otp-input 的常见问题

@bachdgvn/vue-otp-input 是什么?

A fully customizable, one-time password input component for the web built with Vue 2.x.

如何安装 @bachdgvn/vue-otp-input?

在项目目录运行 npm install @bachdgvn/vue-otp-input,或使用 pnpm add / yarn add / bun add。

@bachdgvn/vue-otp-input 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bachdgvn__vue-otp-input 是否存在。

@bachdgvn/vue-otp-input 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bachdgvn/vue-otp-input 可以商用吗?

@bachdgvn/vue-otp-input 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bachdgvn/vue-otp-input 的下载量有多少?

根据 npm 官方下载统计,@bachdgvn/vue-otp-input 在 2026/10/08 – 2026/11/06 这 30 天内被下载 16,677 次,日均约 556 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bachdgvn/vue-otp-input 还在维护吗?

最新版本 1.0.8 发布于 2020/06/16(6 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/02/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。