Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Input OTP examples · Behavior

Auto-submit OTP Input OTP for React

A one-time code form that submits on its own once all six digits are entered.

Framework
UI library

Most people type the last digit and wait. This example submits as soon as the code is complete, with no button to press. It's a Field with a One-Time Password label, a description saying the form submits by itself, and a FieldError for a short code.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How does the form submit by itself?

The field's completion callback calls the same submit function as the form. onComplete fires when the code reaches maxLength.

What if the form is submitted with a short code?

The form's submit handler runs the same check as auto-submit, so a code shorter than six shows Your one-time password must be 6 characters. in FieldError instead of being sent.

How is the error shown?

invalid on Field follows the error, and FieldError shows the message under the description. The error clears on the next valid submit.

What happens after submitting?

The example shows the result in a status line under the form instead of calling an API. Replace that with your request, and show its error in FieldError when the code is rejected. Here it reads Code submitted: and the code.

How is One-Time Password tied to the field?

FieldLabel sets htmlFor to otp-auto-submit, the id on InputOTP, which it passes to its input.

When should I avoid auto-submit?

When a wrong code has a cost, such as locking the account after a few tries, or when people need to review it. Use a submit button then, as the Form with Validation example does.