Forms
One-time code
Boxes for a one-time code from an app or a text message, with paste and autofill.
Forms
Boxes for a one-time code from an app or a text message, with paste and autofill.
Change the props on the right. The code field and its code follow.
import { InputOTP, InputOTPGroup, InputOTPSlot } from 'omuk-ui/input-otp';
import { Label } from 'omuk-ui/label';
import { REGEXP_ONLY_DIGITS } from 'input-otp';
<div className="flex flex-col gap-1">
<Label htmlFor="code">Code from your authenticator app</Label>
<InputOTP id="code" maxLength={6} pattern={REGEXP_ONLY_DIGITS}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
</div>One real input under the boxes takes the code, so typing, paste and the phone’s autofill all work. pattern keeps out what does not belong: here, anything but digits.
Split a long code into groups, with a dash between them that a screen reader skips. With letters in the code, set inputMode to text, so a phone shows the full keyboard.
Set aria-invalid on the input and on each slot: the boxes take the red edge. The line below keeps its place while it is empty, so nothing moves when the error shows.
That code is not right. Codes change every 30 seconds.
InputOTP takes every prop of the input-otp library’s OTPInput and of an <input>. InputOTPGroup and InputOTPSlot take the props of a <div>.