Skip to content

Input OTP

Download .md

An OTP input takes a one-time code, such as an email verification or a sign-in code, in six separate boxes.

Use it for every one-time code in every Sosialoka product, with these rules:

  • Six separate boxes, never one text field.
  • Digits only. Pasting the whole code from the email fills every box.
  • Send it when it is complete. When the sixth digit is in, submit the code without a click. Keep the submit button too, disabled until the code is complete, and guard the submit so it runs once.
  • Clear it when the backend refuses the code, so the caret goes back to the first box; show the backend’s message below it.
  • Focus the first box when the screen or step appears.
  • Put the label above the boxes as a p element, centered with its hint.
React

Kode verifikasi

Kami kirim kodenya ke email kamu.

Vue

Kode verifikasi

Kami kirim kodenya ke email kamu.

123
  1. 1Filled box
  2. 2Focused box
  3. 3Empty box

Six 48px boxes (size-12) on radius-md with an input border, 8px apart, in text-lg. The active box shows the ring; a refused code turns every border destructive. One real input sits behind the boxes, which is what makes typing, deleting and pasting work.

There is one style, for six digits.

React Vue Flutter Type Default Use
maxLength maxlength maxLength number none The number of digits: 6.
pattern={REGEXP_ONLY_DIGITS} :pattern="REGEXP_ONLY_DIGITS" FilteringTextInputFormatter.digitsOnly pattern any character Accepts digits only.
value / onChange v-model onChanged text "" The code typed so far.
onComplete @complete check the length in onChanged function none Runs when the last box is filled: submit here.
aria-invalid aria-invalid an error below boolean false Marks a refused code.
import { useId } from "react"
import { REGEXP_ONLY_DIGITS } from "input-otp"
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp"
export default function InputOtpDemo() {
const id = useId()
return (
<div className="grid justify-items-center gap-2 text-center">
{/* One real input sits behind the six boxes, so the label points at it through aria-labelledby. */}
<p id={`${id}-label`} className="text-sm font-medium">Kode verifikasi</p>
<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS} autoFocus aria-labelledby={`${id}-label`} aria-describedby={`${id}-hint`}>
<InputOTPGroup>
{[0, 1, 2, 3, 4, 5].map((index) => (
<InputOTPSlot key={index} index={index} />
))}
</InputOTPGroup>
</InputOTP>
<p id={`${id}-hint`} className="text-sm text-muted-foreground">Kami kirim kodenya ke email kamu.</p>
</div>
)
}
Terminal window
npx shadcn@latest add https://design.sosialoka.tech/r/react/input-otp.json
  • The real input behind the boxes is named by the label with aria-labelledby and tied to the hint with aria-describedby.
  • It uses autocomplete="one-time-code", so phones can offer the code from a message.
  • Backspace clears the previous box; the arrow keys move between boxes.