shadcn.io is an independent project built on shadcn/ui
Components
Free
Input OTP for React Aria
A one-time code field with one box per character, paste that fills every box, and digits-only or alphanumeric rules. For React on React Aria.
On this page9 sections
An input OTP collects a short code, such as the six digits of a sign-in code sent by text message, with one box per character. Typing moves on to the next box, and pasting the whole code fills them in one go.
Use it when the code has a fixed length. A secret of any length, such as a password, belongs in a Password Input, and free-form text in an Input.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
The slots are drawn over one real input that holds the whole code, so a screen reader hears a single text field, with autocomplete set to one-time-code and a number pad on phones. Name it with a label whose htmlFor matches the id on InputOTP, as the Form demo does, or with an aria-label on InputOTP. Attributes on a slot only style it, since the slots are drawn div elements.
KeyAction
Tab
Moves focus into the field and out of it again, since the whole code is one input.
Arrow Left
Moves to the previous slot.
Arrow Right
Moves to the next slot, as far as the first empty one.
Backspace
Deletes the character in the active slot, or the one before it when the active slot is empty. Characters after it move one slot left.
Delete
Deletes the character in the active slot. Characters after it move one slot left.
Home
Moves to the first slot.
End
Moves to the first empty slot, or the last slot once every slot is filled.
Ctrl+V
Pastes a code, cut to the number of slots. With a pattern, a paste that doesn't match it is refused.
Demos
Pattern
Six slots under the label Digits Only refuse letters, the right rule for a code sent by text message. pattern is set to REGEXP_ONLY_DIGITS from the input-otp package, and without it any character goes in. When the issuer mixes letters and digits, allow both, as the Alphanumeric demo does.
Separator
Six slots in three pairs, with a minus sign between the pairs. The separator isn't a character of the code, and the index on each slot keeps counting from 0 to 5 across the groups. Group the slots the way the code is printed in the message, such as 12-34-56, so people can check one against the other.
Disabled
The code 123456 is filled in and disabled is set, so the slots fade and can't be focused or changed. Use it to show a code that has already been used, or a field that opens only after another step, such as choosing where to send the code. Don't disable the field to show an error, since people need to fix a wrong code.
Controlled
The code lives in your own state, and the line under the slots repeats it, or asks for the code while it's empty. The value is one string. Control it when you check the code as it's typed, enable a Verify button once it's complete, or clear it after a failed attempt.
Invalid
Every slot carries aria-invalid and the code starts as six zeros, so the red borders show before anyone types. The slots are drawn over one real input, so the demo sets aria-invalid on InputOTP too, which passes it to that input for a screen reader. Use it after the server rejects a code, next to a message that says so.
Four digits
Four slots that take digits only, with no separator, for a four-digit PIN or a short code. Match the slot count to what the issuer sends, because two empty slots at the end make people think the code they got was cut short.
Alphanumeric
Letters and digits both go in, split into two groups of three, for a code that mixes them, such as a backup code from an email. pattern is set to REGEXP_ONLY_DIGITS_AND_CHARS, and inputMode to text so a phone shows letters. A digits-only field refuses the first letter of such a code, which looks to people as if the field is broken.
Form
A Verify your login card names the email the code went to, then shows the Verification code label, the slots with required, a Resend Code button and a full-width Verify button. The body and footer sit in a form, and InputOTP has a name, so Verify sends the whole code under it. Resend has type set to button, so pressing it doesn't submit.
Auto submit
Six slots under One-Time Password check the code as soon as the last character goes in, with no button to press, and a line under the field then shows the code it took. The check runs from onComplete. Pick it for a sign-in code, where waiting for a click only slows people down.
RTL
The label reads رمز التØÙ‚Ù‚, Arabic for verification code, and follows the field's right-to-left direction. The slots stay left to right, because the component lays them out that way in any direction and dir set to ltr on InputOTP keeps the caret and arrow keys in step. A code is read and pasted from its first character, so reversed slots would scramble it.
FAQ
How do I control an Input OTP value in React?
Hold the code in state as one string, pass it as value and update it in onChange, which receives the new string. Set maxLength to the number of slots, which the input-otp package requires, and give the slots the indexes 0 up to one less than that.
How do I submit an Input OTP automatically when the code is complete?
Pass onComplete to InputOTP, and the input-otp package calls it with the code once the last slot is filled. From there, call requestSubmit on the form, or send the code to your server yourself, as the Auto submit demo does.
While the check runs, disable the field and show that it's working, so a second code can't be typed over the first.
How do I accept only digits in an Input OTP?
Pass pattern to InputOTP. The input-otp package exports REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS and REGEXP_ONLY_DIGITS_AND_CHARS for the usual cases, and without a pattern any character goes in. Phones show a number pad by default, so set inputMode to text on InputOTP when the code has letters, as the Alphanumeric demo does.
Put an InputOTPSeparator between two InputOTPGroup parts, as the Separator demo does. It draws a minus sign with the separator role and isn't part of the code, so index keeps counting across groups: two groups of three are slots 0 to 2 and 3 to 5.
To draw something else, such as a dot, edit InputOTPSeparator in components/ui/input-otp, which is your file once installed.
How do I show an error on an Input OTP?
Set aria-invalid on each InputOTPSlot, which turns its border red, and on InputOTP, which passes it to the real input a screen reader reads, since the slots themselves are only drawn. The Invalid demo sets both.
Say what went wrong in text next to the field, and clear the error once the person starts typing a new code.
How do I submit an Input OTP with a form?
Give InputOTP a name. The slots are drawn over one real input under that name, so the form submits the whole code as one string. Setting required on InputOTP works as it does on any input.
The Form demo puts a verification card in a form and names the code. Its resend button has type set to button, so pressing it doesn't submit the code.
Can an Input OTP fill in a code from a text message or a paste?
Yes. The field sets autocomplete to one-time-code, the value browsers look for when they offer a code that just arrived by text message. Pasting a whole code fills the slots from the one that has focus, and a paste that breaks the digits-only or letters rule is refused.
On this base one real input sits under the slots, so autofill and paste go into it like any text field.
How do I use an Input OTP on a right-to-left page?
Keep the code left to right. The component already lays out its slots that way on any page, and setting dir to ltr on InputOTP, as the RTL demo does, makes the caret and the arrow keys agree with them. The label around it follows the page's direction as usual.
Don't reverse the indexes: slot 0 is the code's first character, on the left, wherever the field sits.