Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🐗 A fully customisable OTP input component for Vue 3.x

NotificationsYou must be signed in to change notification settings

ejirocodes/vue3-otp-input

Repository files navigation

Vue 3 OTP Input is a 5.0 KB fully customizable OTP (one-time password) input component for OTPs, transaction pins, and passwords built with Vue 3.x and Vue Composition API..

📹 Demo

Gifphy

⚙️ Installation

Install as package

You can installvue3-otp-input via the terminal.

pnpm i vue3-otp-input

OR

Install via CDN

<script src="https://unpkg.com/vue3-otp-input"></script><script src="https://cdn.jsdelivr.net/npm/vue3-otp-input"></script>

📖 UsageDemo

1/3. Register as a Vue component locally OR

<scriptsetuplang="ts">import{ref}from"vue";importVOtpInputfrom"vue3-otp-input";constotpInput=ref<InstanceType<typeofVOtpInput>|null>(null);constbindModal=ref("");consthandleOnComplete=(value:string)=>{console.log("OTP completed: ",value);};consthandleOnChange=(value:string)=>{console.log("OTP changed: ",value);};constclearInput=()=>{otpInput.value?.clearInput();};constfillInput=(value:string)=>{console.log(value);otpInput.value?.fillInput(value);};</script>

1/3. Register as a Vue component globally

//  main.js or main.tsimport{createApp}from'vue'importAppfrom'./App.vue'importVOtpInputfrom"vue3-otp-input";constapp=createApp(App)app.component('v-otp-input',VOtpInput).mount('#app')

2/3. Use the registered component in your Vue template

<template><divstyle="display: flex; flex-direction: row"><v-otp-inputref="otpInput"input-classes="otp-input":conditionalClass="['one', 'two', 'three', 'four']"separator="-"inputType="letter-numeric":num-inputs="4"v-model:value="bindValue":should-auto-focus="true":should-focus-order="true"@on-change="handleOnChange"@on-complete="handleOnComplete":placeholder="['*', '*', '*', '*']"/></div><button@click="clearInput()">Clear Input</button><button@click="fillInput('2929')">Fill Input</button></template>

3/3 [Optional]. Some basic styling options

This css has to go into a<style> tag which does not have scoped attributed

<style>.otp-input {width:40px;height:40px;padding:5px;margin:010px;font-size:20px;border-radius:4px;border:1px solidrgba(0,0,0,0.3);text-align: center;}/* Background colour of an input field with value */.otp-input.is-complete {background-color:#e4e4e4;}.otp-input::-webkit-inner-spin-button,.otp-input::-webkit-outer-spin-button {-webkit-appearance: none;margin:0;}input::placeholder {font-size:15px;text-align: center;font-weight:600;}</style>

🍔 Props

Name
TypeRequiredDefaultDescription
valuestringfalse""v-modal:value for bind dynamic value
num-inputsnumbertrue4Number of OTP inputs to be rendered.
separatorcomponent
falseProvide a custom separator between inputs by passing a component. For instance,<span>-</span> would add- between each input
input-classesclassName (string)falsenoneStyle applied or class passed to each input.
input-typestringfalse"tel"Input type. Optional value: "password", "number", "tel", "letter-numeric".
inputmodestringfalse"numeric"This allows a browser to display an appropriate virtual keyboard. Optional value: "numeric", "text", "tel". [Learn More](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode)
should-auto-focusbooleanfalsefalseAuto focuses input on inital page load.
should-focus-orderbooleanfalsefalseAuto correct the input order. See#39
placeholderarrayfalse[]Specify an expected value for each input. Example::placeholder="['*', '*', '*', '*']". The length of this array should be equal tonum-inputs.
conditionalClassarrayfalse[] Specify a class to be applied to each input based on the value of the input. Example::conditionalClass="['one', 'two', 'three', 'four']". The length of this array should be equal tonum-inputs.
is-disabledbooleanfalsefalseDisables all the input fields.

🤺 Methods

Name
Description
clearInput()Use with ref for clearing all otp inputs, see code example section.
fillInput()Use with ref for fill otp inputs. The value should be same length as `numInputs` length. See code example section.

🐴 Events

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

🤟🏽 License

MIT

📓 Contributing

Contributions are always welcome!

Seecontributing.md for ways to get started.

Please adhere to this project'scode of conduct.

🧸 Appendix

This component is a rewite ofvue-otp-input component to support Vue 3.x. Feel free to use it in your project, report bugs and make PR 👏🏽.


[8]ページ先頭

©2009-2025 Movatter.jp