Dinachi

OTP Field

A one-time password / verification code input with auto-focus, paste handling, and validation.

View Source

#Installation

npx @dinachi/cli@latest add otp-field

#Usage

tsx
import {
  OTPField,
  OTPFieldGroup,
  OTPFieldInput,
  OTPFieldSeparator,
} from "@/components/ui/otp-field"
tsx
<OTPField length={6}>
  <OTPFieldGroup>
    <OTPFieldInput />
    <OTPFieldInput />
    <OTPFieldInput />
    <OTPFieldInput />
    <OTPFieldInput />
    <OTPFieldInput />
  </OTPFieldGroup>
</OTPField>

#Examples

#API Reference

PropTypeDefaultDescription
lengthnumber—The number of OTP input slots. Required so the field can clamp values and detect completion.
valuestring—The controlled OTP value.
defaultValuestring—The initial OTP value when uncontrolled.
onValueChange(value: string, details: { reason: 'input-change' | 'input-clear' | 'input-paste' | 'keyboard' }) => void—Callback fired when the OTP value changes.
onValueComplete(value: string, details) => void—Callback fired when all slots are filled.
onValueInvalid(value: string, details) => void—Callback fired when typed or pasted characters are rejected by validation.
validationType'numeric' | 'alphanumeric' | 'alphabetic''numeric'The type of input validation to apply to the OTP value.
normalizeValue(value: string) => string—Function that normalizes the OTP value after whitespace and validationType filtering.
maskbooleanfalseWhether the slot inputs should mask entered characters.
autoSubmitbooleanfalseWhether to submit the owning form when the OTP becomes complete.
autoCompletestring'one-time-code'The input autocomplete attribute. Applied to the first slot and hidden validation input.
inputMode'numeric' | 'text' | 'tel' | ...—The virtual keyboard hint applied to the slot inputs.
namestring—Identifies the field when a form is submitted.
formstring—Associates the hidden input with a form element id.
requiredbooleanfalseWhether the user must enter a value before submitting a form.
disabledbooleanfalseWhether the component should ignore user interaction.
readOnlybooleanfalseWhether the user should be unable to change the field value.