Input OTP
Accessible one-time password component with copy-paste functionality.
import { createSignal, For } from "solid-js"
import {
InputOTP,
InputOTPGroup,
InputOTPSlot,
InputOTPSeparator
} from "~/components/ui/input-otp"
export default function InputOTPDemo() {
const [value, setValue] = createSignal("")
return (
<div class="grid gap-3">
<InputOTP
aria-label="Verification code"
value={value()}
onValueChange={setValue}
maxLength={6}
>
<InputOTPGroup>
<For each={[0, 1, 2]}>
{(index) => <InputOTPSlot index={index} />}
</For>
</InputOTPGroup>
<InputOTPSeparator>โ</InputOTPSeparator>
<InputOTPGroup>
<For each={[3, 4, 5]}>
{(index) => <InputOTPSlot index={index} />}
</For>
</InputOTPGroup>
</InputOTP>
<output class="text-center text-sm text-muted-foreground">
{value() || "Enter your verification code"}
</output>
</div>
)
}About
Input OTP is built on the UI SDK OTP Input.
Installation
Install the following dependencies:
Copy and paste the following code into your project.
import type { ComponentProps } from "@solidjs/web"
import {
OTPInput as SdkOTPInput,
OTPInputGroup as SdkOTPInputGroup,
OTPInputSlot as SdkOTPInputSlot,
OTPInputSeparator as SdkOTPInputSeparator
} from "@ui-sdk/solid/otp-input"
import { cn } from "~/lib/utils"
export function InputOTP(
props: Omit<ComponentProps<typeof SdkOTPInput>, "class"> & { class?: string }
) {
return (
<SdkOTPInput
{...props}
class={cn(
"gap-2 flex items-center gap-2 has-disabled:opacity-50",
props.class
)}
/>
)
}
export function InputOTPGroup(
props: Omit<ComponentProps<typeof SdkOTPInputGroup>, "class"> & {
class?: string
}
) {
return (
<SdkOTPInputGroup
{...props}
class={cn("rounded-lg has-aria-invalid:border-destructive has-aria-invalid:ring-3 has-aria-invalid:ring-destructive/20 dark:has-aria-invalid:ring-destructive/40 flex items-center", props.class)}
/>
)
}
export function InputOTPSlot(
props: Omit<ComponentProps<typeof SdkOTPInputSlot>, "class"> & {
class?: string
}
) {
return (
<SdkOTPInputSlot
{...props}
class={cn(
"size-8 border-y border-r border-input text-sm transition-all outline-none first:rounded-l-lg first:border-l last:rounded-r-lg aria-invalid:border-destructive dark:bg-input/30 data-active:border-ring data-active:ring-3 data-active:ring-ring/50 data-active:aria-invalid:border-destructive data-active:aria-invalid:ring-destructive/20 dark:data-active:aria-invalid:ring-destructive/40 relative flex items-center justify-center data-fake-caret:after:absolute data-fake-caret:after:h-4 data-fake-caret:after:w-px data-fake-caret:after:animate-caret-blink data-fake-caret:after:bg-current data-active:z-10",
props.class
)}
/>
)
}
export function InputOTPSeparator(
props: Omit<ComponentProps<typeof SdkOTPInputSeparator>, "class"> & {
class?: string
}
) {
return (
<SdkOTPInputSeparator
{...props}
class={cn("[&_svg:not([class*='size-'])]:size-4", props.class)}
/>
)
}Update the import paths to match your project setup.
Usage
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot
} from "~/components/ui/input-otp"<InputOTP aria-label="Verification code" maxLength={6}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>InputOTP owns one native input. Give each InputOTPSlot a zero-based index. Use value, onValueChange, maxLength, and pattern from the SDK. Slots use SDK active and fake-caret states and do not create extra tab stops.
See the UI SDK otp-input API for native options and keyboard behavior.
Composition
Use the following composition to build an InputOTP:
InputOTP
โโโ InputOTPGroup
โ โโโ InputOTPSlot
โ โโโ InputOTPSlot
โ โโโ InputOTPSlot
โโโ InputOTPSeparator
โโโ InputOTPGroup
โ โโโ InputOTPSlot
โ โโโ InputOTPSlot
โ โโโ InputOTPSlot
โโโ InputOTPSeparator
โโโ InputOTPGroup
โโโ InputOTPSlot
โโโ InputOTPSlotPattern
Use the pattern prop to define a custom pattern for the OTP input.
const REGEXP_ONLY_DIGITS = /[0-9]/
import { Field, FieldLabel } from "~/components/ui/field"
import { InputOTP, InputOTPGroup, InputOTPSlot } from "~/components/ui/input-otp"
export default function InputOTPPattern() {
return (
<Field class="w-fit">
<FieldLabel for="digits-only">Digits Only</FieldLabel>
<InputOTP id="digits-only" maxLength={6} pattern={REGEXP_ONLY_DIGITS}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
</Field>
)
}Separator
Use the <InputOTPSeparator /> component to add a separator between input groups.
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot
} from "~/components/ui/input-otp"
export default function InputOTPWithSeparator() {
return (
<InputOTP maxLength={6}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
)
}Disabled
Use the disabled prop to disable the input.
import { For } from "solid-js"
import { InputOTP, InputOTPGroup, InputOTPSlot } from "~/components/ui/input-otp"
export default function InputOTPDisabled() {
return (
<InputOTP
aria-label="Disabled verification code"
defaultValue="123456"
disabled
>
<InputOTPGroup>
<For each={[0, 1, 2, 3, 4, 5]}>
{(index) => <InputOTPSlot index={index} />}
</For>
</InputOTPGroup>
</InputOTP>
)
}Controlled
Use the value and onChange props to control the input value.
import { createSignal } from "solid-js"
import { InputOTP, InputOTPGroup, InputOTPSlot } from "~/components/ui/input-otp"
export default function InputOTPControlled() {
const [value, setValue] = createSignal("")
return (
<div class="space-y-2">
<InputOTP maxLength={6} value={value()} onValueChange={setValue}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
<div class="text-center text-sm">
{value() === "" ? (
<>Enter your one-time password.</>
) : (
<>You entered: {value()}</>
)}
</div>
</div>
)
}Invalid
Use aria-invalid on the slots to show an error state.
import { createSignal } from "solid-js"
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot
} from "~/components/ui/input-otp"
export default function InputOTPInvalid() {
const [value, setValue] = createSignal("000000")
return (
<InputOTP
maxLength={6}
aria-invalid="true"
value={value()}
onValueChange={setValue}
>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
)
}Four Digits
A common pattern for PIN codes. This uses the pattern={REGEXP_ONLY_DIGITS} prop.
const REGEXP_ONLY_DIGITS = /[0-9]/
import { InputOTP, InputOTPGroup, InputOTPSlot } from "~/components/ui/input-otp"
export default function InputOTPFourDigits() {
return (
<InputOTP maxLength={4} pattern={REGEXP_ONLY_DIGITS}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
</InputOTPGroup>
</InputOTP>
)
}Alphanumeric
Use REGEXP_ONLY_DIGITS_AND_CHARS to accept both letters and numbers.
const REGEXP_ONLY_DIGITS_AND_CHARS = /[a-z0-9]/i
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot
} from "~/components/ui/input-otp"
export default function InputOTPAlphanumeric() {
return (
<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS_AND_CHARS}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
)
}Form
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { Button } from "~/components/ui/button"
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle
} from "~/components/ui/card"
import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot
} from "~/components/ui/input-otp"
export default function InputOTPForm() {
return (
<Card class="mx-auto max-w-md">
<CardHeader>
<CardTitle>Verify your login</CardTitle>
<CardDescription>
Enter the verification code we sent to your email address:{" "}
<span class="font-medium">m@example.com</span>.
</CardDescription>
</CardHeader>
<CardContent>
<Field>
<div class="flex items-center justify-between">
<FieldLabel for="otp-verification">Verification code</FieldLabel>
<Button variant="outline" size="xs">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8" />
<path d="M21 3v5h-5" />
<path d="M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16" />
<path d="M8 16H3v5" />
</svg>
Resend Code
</Button>
</div>
<InputOTP maxLength={6} id="otp-verification" required>
<InputOTPGroup class="*:data-[slot=input-otp-slot]:h-12 *:data-[slot=input-otp-slot]:w-11 *:data-[slot=input-otp-slot]:text-xl">
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
</InputOTPGroup>
<InputOTPSeparator class="mx-2" />
<InputOTPGroup class="*:data-[slot=input-otp-slot]:h-12 *:data-[slot=input-otp-slot]:w-11 *:data-[slot=input-otp-slot]:text-xl">
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
<FieldDescription>
<a href="#">I no longer have access to this email address.</a>
</FieldDescription>
</Field>
</CardContent>
<CardFooter>
<Field>
<Button type="submit" class="w-full">
Verify
</Button>
<div class="text-sm text-muted-foreground">
Having trouble signing in?{" "}
<a
href="#"
class="underline underline-offset-4 transition-colors hover:text-primary"
>
Contact support
</a>
</div>
</Field>
</CardFooter>
</Card>
)
}API Reference
See the UI SDK OTP Input API for the full API reference.