Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Input OTP examples · Behavior

OTP with Paste Optimization Input OTP for React

A one-time code field with a Paste Code button that keeps only the digits from the clipboard.

Framework
UI library

Codes are often copied with spaces or dashes, like 123 456. This example adds a Paste Code button that reads the clipboard, keeps only the digits, takes the first six and fills the field, so a messy copy still works in one press.

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 button read the clipboard?

With navigator.clipboard.readText() in the button's press handler. Browsers allow it after a user action, and some ask for permission the first time.

How are the digits extracted?

A regular expression, NON_DIGITS, removes everything that isn't a digit, then the text is cut to six characters and written to the field's value.

What if clipboard access is denied?

The read throws and the handler ignores it. People can still paste into the field with the keyboard or the context menu.

Does a regular paste work too?

Yes. Pasting into the field fills the slots in order. The button helps when the copied text has spaces, dashes or words around the code.

How is the button styled?

An outline Button at size sm and full width, with a ClipboardPasteIcon marked data-icon inline-start. It's type button, so it never submits a form.

How is the value kept?

In state, written on every change and by the button. The value is one string, and onChange receives the next string.