Forms

OTP input

Enters a one-time code, like the one sent to a phone to confirm a sign-in.

Installation

npx sinaui add otp-input

Examples

Verification code

Paste the whole code, or let the phone fill it in from the message. A wrong code shakes the boxes and clears them.

We sent a 6-digit code to your phone. (Try 314159.)

<script lang="ts">
	import OtpInput from '#lib/ui/OtpInput.svelte';

	let code = $state('');
	let error = $state<string>();
	let verified = $state(false);

	// The error goes as soon as they start typing again.
	$effect(() => {
		if (code) error = undefined;
	});

	// Stands in for your server; 314159 is the right code here.
	function check(entered: string) {
		verified = entered === '314159';
		error = verified ? undefined : "That code isn't right. Check the message and try again.";
		if (!verified) code = '';
	}
</script>

<OtpInput
	label="Verification code"
	bind:value={code}
	groups={[3, 3]}
	name="code"
	hint="We sent a 6-digit code to your phone. (Try 314159.)"
	{error}
	oncomplete={check}
/>
{#if verified}<p class="ok" role="status">Verified. Welcome back to the reading room.</p>{/if}

<style>
	.ok {
		margin: 0.75rem 0 0;
		color: color-mix(in srgb, var(--ui-accent) 80%, var(--ui-fg));
		font-weight: 500;
	}
</style>

Letters and digits

Typed in any case, shown in capitals.

Letters and digits, on your loan slip.

<script lang="ts">
	import OtpInput from '#lib/ui/OtpInput.svelte';

	let pickup = $state('');
</script>

<OtpInput
	label="Pickup code"
	bind:value={pickup}
	length={4}
	mode="text"
	hint="Letters and digits, on your loan slip."
/>

Props

PropTypeDefault
labelstring (required)
valuestring, bindable''
lengthnumber6
groupsnumber[], e.g. [3, 3]
mode'numeric' | 'text''numeric'
oncomplete(code) => void
name, hint, errorstring
disabledbooleanfalse

Accessibility

  • One real text field under the boxes: screen readers hear a single field with its label, not six.
  • It asks for one-time-code autofill, so phones offer the code from the message.
  • Numeric codes show the number keypad on phones.
Introduction
Theming
Changelog
Button
Action button
Confirm button
Copy button
Hold to confirm
Split button
Calendar
Checkbox
Color picker
Combobox
Date picker
Date range picker
File drop
Form
Input
Mention input
Multi select
Number field
OTP input
Password input
Phone input
Radio cards
Radio group
Rich text editor
Search field
Segmented control
Select
Signature pad
Slider
Switch
Tag input
Upload
Wheel picker
Textarea
Time field
Toggle group
Toolbar
Alert dialog
Command palette
Context menu
Dialog
Drawer
Dropdown
Hover card
Popover
Tooltip
Announcement bar
Alert
Empty state
Meter
Progress
Rolling number
Skeleton
Spinner
Toast
Avatar
Avatar group
Badge
Card
Carousel
Chart
Chat
Code block
Data table
Folder
Kbd
Mind map
Resizable panels
Scroll area
Separator
Sortable
Table
Tag group
Virtual list
Audio player
Image
Image compare
Video
Visualizer
Breadcrumb
File tree
Menubar
Navigation menu
Pagination
Sidebar layout
Stepper
Tabs
Alarm clock
Clock
Dynamic island
Fluid orb
Hourglass
Stopwatch
Timer
AI chat
AI media
Auth
Board
Booking
Dashboard
File explorer
Messaging
Notifications
Settings
Team
Overview
Pricing
Agent chat
Calendar
Voice agent
Mail inbox
CMS
Blog
Marketing site
E-commerce
AI agents
SaaS landing page
SaaS app
Accordion
Dark theme
Light theme