Skip to main content

Starwind UI v1.15 is now available! With new prose, sidebar, and more components

Input OTP

Installation

Usage

General Notes

The Input OTP component provides a user-friendly way to input one-time passwords and verification codes. It supports keyboard navigation, paste handling, and pattern validation.

The essential components are InputOtp, InputOtpGroup, and InputOtpSlot. The InputOtpSeparator provides visual separation between groups of slots.

With Separator

Use InputOtpSeparator to visually group slots, commonly used for phone verification codes or formatted inputs.

Alphanumeric Pattern

Use the pattern prop with REGEXP_ONLY_DIGITS_AND_CHARS to allow both letters and numbers.

Sizes

The InputOtpSlot component supports three sizes: sm, md (default), and lg.

Small

Medium (default)

Large

Disabled

Use the disabled prop to prevent user interaction.

Form Submission

The component integrates with native HTML forms using the name prop. The hidden input stores the complete OTP value.

API Reference

InputOtp

The root container that manages the OTP input state and keyboard interactions.

PropTypeDefault
maxLengthnumber6
valuestring-
defaultValuestring-
disabledbooleanfalse
patternRegExp | string\d
namestring-
idstring-
requiredboolean-
classstring-
<InputOtp maxLength={6} name="otp" pattern={REGEXP_ONLY_DIGITS_AND_CHARS}>
<InputOtpGroup>
<InputOtpSlot index={0} />
</InputOtpGroup>
</InputOtp>

Additional Notes:

  • maxLength: The total number of characters allowed in the OTP
  • pattern: A regex pattern to validate each character. Use REGEXP_ONLY_DIGITS_AND_CHARS for alphanumeric or REGEXP_ONLY_DIGITS for numbers only
  • name: Required for form submission, the value is stored in a hidden input
  • disabled: When true, prevents all user interaction

InputOtpGroup

A container for grouping slots together visually.

PropTypeDefault
classstring-
<InputOtpGroup>
<InputOtpSlot index={0} />
<InputOtpSlot index={1} />
<InputOtpSlot index={2} />
</InputOtpGroup>

InputOtpSlot

An individual input slot that displays a single character.

PropTypeDefault
indexnumber-
size"sm" | "md" | "lg""md"
classstring-
<InputOtpSlot index={0} size="lg" />

Additional Notes:

  • index: The position of this slot in the OTP sequence (0-indexed)
  • size: Controls the slot dimensions. Use "sm" for compact layouts or "lg" for emphasis

InputOtpSeparator

A visual separator between groups of slots.

PropTypeDefault
classstring-
<InputOtpSeparator />

The separator renders a dash icon by default. Use the icon slot to customize:

<InputOtpSeparator>
<span slot="icon">/</span>
</InputOtpSeparator>

Exported Constants

NameDescription
REGEXP_ONLY_DIGITSPattern that allows only digits (0-9)
REGEXP_ONLY_DIGITS_AND_CHARSPattern that allows alphanumeric characters (A-Z, a-z, 0-9)

Events

The component dispatches a custom event when the value changes:

interface InputOtpChangeEvent {
detail: {
value: string;
inputOtpId: string;
};
}

Listen for changes:

<script>
document.addEventListener("starwind-input-otp:change", (e) => {
const { value, inputOtpId } = e.detail;
console.log(`OTP ${inputOtpId} changed to: ${value}`);
});
</script>

Changelog

v1.0.1

  • Auto focus hidden input on click to automatically open keyboards on mobile devices
  • Update component to always focus the next empty index on click

v1.0.0

  • Initial release with starwind v1.15.0