shadcn.io is an independent project built on shadcn/ui
Components
Free
Input OTP for Svelte
A one-time code field with one box per character, paste that fills every box, and digits-only or alphanumeric rules. For Svelte on Ark UI.
On this page9 sections
UI library: Ark UI
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.
Each slot is its own input, announced as "pin code 1 of 6" and so on, with autocomplete set to one-time-code so a phone can offer the code from a text message. Only one slot is in the tab order, and the keys below move between the others. Inside a Field, each slot reads the FieldLabel before its own name, and a click on the label puts focus in the first slot. To change the words "pin code", pass translations with an inputLabel function.
KeyAction
Arrow Left
Moves focus to the previous input.
Arrow Right
Moves focus to the next input, as far as the first empty one.
Backspace
Deletes the value in the current input and moves focus to the previous input.
Delete
Deletes the value in the current input.
Ctrl+V
Pastes the value into the input fields.
Enter
When every slot is filled and InputOTP has a name, submits the form it belongs to.
Home
Moves focus to the first slot.
End
Moves focus to the last filled slot, or to the first slot while every slot is empty.
Tab
Moves focus into the field at the first empty slot, or the last slot once every slot is filled, then out of the field.
Demos
Pattern
Six slots under the label Digits Only refuse letters, the right rule for a code sent by text message. type is set to numeric, which Ark UI also uses when you leave it out. 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 an array with one string per slot. 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. Each slot is a real input, so a screen reader hears the error on whichever one has focus. 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. type is set to alphanumeric, and each slot sets its input mode 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 onValueComplete. 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 Svelte?
Hold the code in state as an array with one string per slot, pass it as value and update it in onValueChange. Its details also carry valueAsString, the whole code as one string, which is what you send to the server.
How do I submit an Input OTP automatically when the code is complete?
Set autoSubmit on InputOTP, and Ark UI submits the surrounding form as soon as the last slot is filled. Give it a name too, so the code is in the form data. To run your own check instead, use onValueComplete, which receives the code as valueAsString, 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?
Digits only is already the default: Ark UI's type is numeric unless you set it to alphanumeric or alphabetic, and any other character typed or pasted is refused. For a rule of your own, pass a regular expression string as pattern, which takes the place of type. Phones show a number pad while otp is on, as it is by default here, so set inputMode to text on each InputOTPSlot 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 invalid on InputOTP after the server rejects a code, which puts aria-invalid on every slot and turns their borders red. Setting aria-invalid on each InputOTPSlot, as the Invalid demo does, has the same effect.
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. Ark UI keeps a hidden input under that name holding the whole code as one string, so the form submits 123456 rather than six fields. Setting required on InputOTP makes that input required.
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 Ark UI each slot is a separate input with that attribute, and a code dropped into one slot is spread across the rest.
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.
API Reference
InputOTP
<div>data-slot="input-otp"
PropType
value?string[]
The controlled value of the the pin input.
defaultValue?string[]
The initial value of the the pin input when rendered.
Use when you don't need to control the value of the pin input.