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
lengthnumberThe number of OTP input slots. Required so the field can clamp values and detect completion.
valuestringThe controlled OTP value.
defaultValuestringThe initial OTP value when uncontrolled.
onValueChange(value: string, details: { reason: 'input-change' | 'input-clear' | 'input-paste' | 'keyboard' }) => voidCallback fired when the OTP value changes.
onValueComplete(value: string, details) => voidCallback fired when all slots are filled.
onValueInvalid(value: string, details) => voidCallback 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) => stringFunction 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.
namestringIdentifies the field when a form is submitted.
formstringAssociates 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.