Enter a one-time code with support for pasting, autofill, and native form validation.
Use an OTP Input for a short code sent by text or email, like a sign-in or verification code. Each character gets its own slot, and a pasted or autofilled code fills them all at once.
import { Component } from "@angular/core"
import { LucideMinus } from "@lucide/angular"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import {
OTPInput,
OTPInputGroup,
OTPInputSeparator,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
imports: [
Field,
FieldLabel,
OTPInput,
OTPInputGroup,
OTPInputSeparator,
OTPInputSlot,
LucideMinus,
],
template: `
<div uiField>
<label uiFieldLabel>Verification code</label>
<div uiOTPInput [maxLength]="6">
<div uiOTPInputGroup>
<div uiOTPInputSlot [index]="0"></div>
<div uiOTPInputSlot [index]="1"></div>
<div uiOTPInputSlot [index]="2"></div>
</div>
<div uiOTPInputSeparator>
<svg lucideMinus></svg>
</div>
<div uiOTPInputGroup>
<div uiOTPInputSlot [index]="3"></div>
<div uiOTPInputSlot [index]="4"></div>
<div uiOTPInputSlot [index]="5"></div>
</div>
</div>
</div>
`,
})
class Example {}
export { Example }Installation
npm install @ui-sdk/angular @ui-sdk/coreComposition
Use the following composition to build OTPInput:
uiOTPInput
โโโ uiOTPInputGroup
โโโ uiOTPInputSlot
โโโ uiOTPInputSlotuiOTPInput creates the native input and holds the code. uiOTPInputGroup lays out a run of slots. Each uiOTPInputSlot displays the character at its zero-based index. Add uiOTPInputSeparator between groups when the code is easier to read in sections, like the dash in the example above.
Groups and separators change the presentation of one value. They do not create additional fields or tab stops: the slots display the value of a single native input, which handles selection, paste, undo, form submission, and autofill. It uses autocomplete="one-time-code" so supported browsers and password managers can offer a saved or received code.
Usage
Import the parts and put them together:
import { Component } from "@angular/core"
import {
OTPInput,
OTPInputGroup,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
selector: "app-example",
imports: [OTPInput, OTPInputGroup, OTPInputSlot],
template: `
<div uiOTPInput [maxLength]="4" aria-label="Verification code">
<div uiOTPInputGroup>
<div uiOTPInputSlot [index]="0"></div>
<div uiOTPInputSlot [index]="1"></div>
<div uiOTPInputSlot [index]="2"></div>
<div uiOTPInputSlot [index]="3"></div>
</div>
</div>
`,
})
class Example {}
export { Example }Four Digits
Set maxLength to the code's length and render that many slots. The default length is six.
import { Component } from "@angular/core"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import {
OTPInput,
OTPInputGroup,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
imports: [Field, FieldLabel, OTPInput, OTPInputGroup, OTPInputSlot],
template: `
<div uiField>
<label uiFieldLabel>PIN</label>
<div uiOTPInput [maxLength]="4" textAlign="center">
<div uiOTPInputGroup>
@for (index of [0, 1, 2, 3]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
</div>
</div>
`,
})
class Example {}
export { Example }Alphanumeric
Set pattern to digits, letters, or alphanumeric. The default is digits. The pattern filters characters and selects a suitable keyboard on touch devices.
import { Component } from "@angular/core"
import { LucideMinus } from "@lucide/angular"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import {
OTPInput,
OTPInputGroup,
OTPInputSeparator,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
imports: [
Field,
FieldLabel,
OTPInput,
OTPInputGroup,
OTPInputSeparator,
OTPInputSlot,
LucideMinus,
],
template: `
<div uiField>
<label uiFieldLabel>Recovery code</label>
<div uiOTPInput [maxLength]="8" pattern="alphanumeric">
<div uiOTPInputGroup>
@for (index of [0, 1, 2, 3]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
<div uiOTPInputSeparator>
<svg lucideMinus></svg>
</div>
<div uiOTPInputGroup>
@for (index of [4, 5, 6, 7]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
</div>
</div>
`,
})
class Example {}
export { Example }Custom Pattern
Pass a regular expression, such as /[0-9A-F]/i to accept hexadecimal characters. Use inputMode to choose the keyboard separately. The example displays letters in uppercase without changing their stored case.
import { Component } from "@angular/core"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import {
OTPInput,
OTPInputGroup,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
imports: [Field, FieldLabel, OTPInput, OTPInputGroup, OTPInputSlot],
template: `
<div uiField>
<label uiFieldLabel>Device code</label>
<div uiOTPInput [maxLength]="6" [pattern]="/[0-9A-F]/i" inputMode="text">
<div uiOTPInputGroup>
@for (index of [0, 1, 2, 3, 4, 5]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
</div>
</div>
`,
})
class Example {}
export { Example }Validation
Inside a Field, pass invalid when your code rejects the value, such as a code the server didn't accept. The field marks the input with aria-invalid and connects its uiFieldError. Outside a field, pass aria-invalid and aria-describedby yourself.
import { Component, signal } from "@angular/core"
import { Field, FieldError, FieldLabel } from "@ui-sdk/angular/field"
import {
OTPInput,
OTPInputGroup,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
imports: [
Field,
FieldError,
FieldLabel,
OTPInput,
OTPInputGroup,
OTPInputSlot,
],
template: `
<div uiField [invalid]="invalid()">
<label uiFieldLabel>Verification code</label>
<div uiOTPInput defaultValue="482913" (valueChange)="invalid.set(false)">
<div uiOTPInputGroup>
@for (index of [0, 1, 2, 3, 4, 5]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
</div>
<div *uiFieldError>That code has expired. Request a new one.</div>
</div>
`,
})
class Example {
readonly invalid = signal(true)
}
export { Example }Controlled
Use [(value)] to bind the code. (beforeValueChange) also receives the change details. Call $event.cancel() in the change callback to reject a change. Incoming values are filtered by the pattern and limited to maxLength, just like typed values.
import { Component, signal } from "@angular/core"
import { useLog } from "./log"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import {
OTPInput,
OTPInputGroup,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
imports: [Field, FieldLabel, OTPInput, OTPInputGroup, OTPInputSlot],
template: `
<div uiField>
<label uiFieldLabel>One-time password</label>
<div
uiOTPInput
[(value)]="value"
(beforeValueChange)="
log(($event.value || 'Empty') + ' (' + $event.reason + ')')
"
>
<div uiOTPInputGroup>
@for (index of [0, 1, 2, 3, 4, 5]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
</div>
</div>
`,
})
class Example {
readonly log = useLog()
readonly value = signal("")
}
export { Example }See State and Events for the shared state conventions. The reason distinguishes typing, pasting, undo, and controller actions.
On Complete
(valueCommit) runs when a change fills every slot, including typing, paste, autofill, and controller writes. Changing an already-full code runs it again. Form resets and page restoration do not.
import { Component } from "@angular/core"
import { useLog } from "./log"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import {
OTPInput,
OTPInputGroup,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
imports: [Field, FieldLabel, OTPInput, OTPInputGroup, OTPInputSlot],
template: `
<div uiField>
<label uiFieldLabel>Verification code</label>
<div
uiOTPInput
(valueCommit)="
log('Checking ' + $event.value + ' (' + $event.reason + ')')
"
>
<div uiOTPInputGroup>
@for (index of [0, 1, 2, 3, 4, 5]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
</div>
</div>
`,
})
class Example {
readonly log = useLog()
}
export { Example }Forms
Set name to include the code in FormData. required blocks an empty code, and a partial code fails length validation. Reset restores the default value. Use form to associate a control outside the form with its ID.
import { Component } from "@angular/core"
import { useLog } from "./log"
import {
Field,
FieldDescription,
FieldError,
FieldLabel,
} from "@ui-sdk/angular/field"
import {
OTPInput,
OTPInputGroup,
OTPInputSlot,
} from "@ui-sdk/angular/otp-input"
@Component({
imports: [
Field,
FieldDescription,
FieldError,
FieldLabel,
OTPInput,
OTPInputGroup,
OTPInputSlot,
],
template: `
<div data-slot="stack">
<form id="verify-sign-in" (submit)="onSubmit($event)">
<div uiField>
<label uiFieldLabel>Verification code</label>
<div uiOTPInput name="code" required>
<div uiOTPInputGroup>
@for (index of [0, 1, 2, 3, 4, 5]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
</div>
<p uiFieldDescription>
Enter the six-digit code we sent to hollis@example.com.
</p>
<div *uiFieldError></div>
</div>
</form>
<div uiField>
<label uiFieldLabel>Recovery code (optional)</label>
<div
uiOTPInput
name="recovery"
form="verify-sign-in"
[maxLength]="8"
pattern="alphanumeric"
>
<div uiOTPInputGroup>
@for (index of [0, 1, 2, 3, 4, 5, 6, 7]; track index) {
<div uiOTPInputSlot [index]="index"></div>
}
</div>
</div>
<div *uiFieldError></div>
</div>
<div data-slot="row">
<button type="reset" form="verify-sign-in">Reset</button>
<button type="submit" form="verify-sign-in" data-variant="solid">
Verify
</button>
</div>
</div>
`,
})
class Example {
readonly log = useLog()
onSubmit(event: SubmitEvent) {
event.preventDefault()
const data = [...new FormData(event.currentTarget as HTMLFormElement)].map(
([name, value]) => `${name}=${value}`
)
this.log(`Submitted ${data.join(", ")}`)
}
}
export { Example }A typed partial code reports tooShort. Partial default values and controller writes are checked through the native input's pattern, because browsers do not apply minlength validation to those values.
Disabled and Read-Only
A disabled input cannot be focused, changed, or submitted. readOnly prevents changes while keeping the input focusable and included in form data.
<div uiOTPInput disabled defaultValue="123456" aria-label="Disabled code">
...
</div>
<div uiOTPInput readOnly defaultValue="654321" aria-label="Read-only code">
...
</div>Controller
Create a createOTPInputController() and pass it as controller to read, fill, clear, or focus the input from another control. Writes use the imperative reason and pass through the same filtering and change callbacks as typing.
import { Component } from "@angular/core"
import { OTPInput, createOTPInputController } from "@ui-sdk/angular/otp-input"
@Component({
selector: "app-example",
imports: [OTPInput],
template: `
<button (click)="code.setValue('123456')">Fill</button>
<button (click)="code.clear()">Clear</button>
<button (click)="logCode()">Read</button>
<button (click)="code.focus()">Focus</button>
<div uiOTPInput [controller]="code" aria-label="Verification code">...</div>
`,
})
class Example {
readonly code = createOTPInputController()
logCode() {
console.log(this.code.getValue())
}
}
export { Example }Controllers covers creating and sharing a controller.
Custom Rendering
The root's state exposes all slots. Each slot exposes char, active, and hasFakeCaret if you want to draw its contents yourself. See Parts and Composition for the rendering conventions.
Styling
Draw the active slot and caret with data-active and data-fake-caret. The examples include the caret animation and disable it for reduced motion. Keep position: relative on each slot so its caret stays inside it.
Each part has a data-slot, such as otp-input-slot, and its state attributes are listed under Data Attributes.
Data Attributes
OTPInput
[data-slot="otp-input"]
| Attribute | What it means |
|---|---|
data-complete | Present while every slot is filled. |
data-filled | Present while the code holds any character. |
data-disabled | Present when disabled, by its own prop or a <fieldset>. |
data-focused | Present while the input has focus. |
OTPInputGroup
[data-slot="otp-input-group"]
| Attribute | What it means |
|---|---|
data-complete | Present while every slot is filled. |
data-filled | Present while the code holds any character. |
data-disabled | Present when disabled, by its own prop or a <fieldset>. |
data-focused | Present while the input has focus. |
OTPInputSlot
[data-slot="otp-input-slot"]
| Attribute | What it means |
|---|---|
data-filled | Present while the slot holds a character. |
data-disabled | Present when disabled, by its own prop or a <fieldset>. |
data-active | Present on the slot typing goes to: the selected character, or the empty slot after the last one. Only while focused. |
data-fake-caret | Present on the active slot while it's empty. Draw a blinking caret in it; the input's own caret is hidden. |
OTPInputSeparator
[data-slot="otp-input-separator"]
| Attribute | What it means |
|---|---|
data-disabled | Present when disabled, by its own prop or a <fieldset>. |
API Reference
Each option below is an input on the directive, and each callback an output. A change callback is two outputs: (beforeValueChange), with the details and cancel(), then (valueChange) with the new value alone. Bind value with [(value)]. id, autofocus, aria-label,
aria-labelledby, aria-describedby, aria-invalid, aria-busy, aria-required, aria-errormessage and aria-details go to the input. When a name comes from your component, bind it with [aria-label]="name".
OTPInput
debug?boolean- Logs every state change with its reason to the console, in development builds.
value?string- The code, for a controlled input.
defaultValue?string- The code it starts with, when uncontrolled. A form reset returns here.
(beforeValueChange)- Runs when the code changes: typing or deleting (
"input-change"), a paste ("paste"), the browser's undo and redo, a form reset, or a controller. Autofill and codes from the keyboard's suggestions are"input-change".details.cancel()keeps the code as it was. (valueCommit)- Runs when a change fills every slot, with the whole code, like to verify it. A controller filling it counts, so does a code that changes while full.
maxLength?number- How many characters the code has, one slot each.
pattern?OTPInputPattern- Which characters it accepts:
"digits","letters","alphanumeric", or a regular expression each character must match, like/[A-F0-9]/i. Anything else typed, pasted or dropped is ignored. inputMode?"decimal" | "none" | "numeric" | "tel" | "text"- The keyboard touch screens show.
textAlign?"center" | "end" | "start"- Where the invisible input keeps its text and caret. It decides where the browser puts its own popups, like the paste menu on phones.
"start"and"end"follow the reading direction. autoComplete?string- The input's
autocomplete."one-time-code"lets phones offer a code from a message, and password managers fill it. disabled?boolean- Disables it like a native input: it can't be focused, and isn't submitted.
readOnly?boolean- Makes it read-only: focusable and submitted, but the code can't change.
required?boolean- Requires a code before the form submits. A partial code is too short either way.
name?string- The name it submits the code under.
form?string- The id of a form to belong to, when it isn't inside that form.
controller?{ getValue: () => string; setValue: (value: string, options?: OTPInputChangeOptions) => boolean; clear: (options?: OTPInputChangeOptions) => boolean; focus: () => void; }- Reads and writes the code from anywhere. Create one with
createOTPInputController().
OTPInputSlot
indexnumber- Which character of the code the slot shows, from 0.
State
What each part's state() signal holds. The root, groups and separators get the
input's state.
valuestring- The code.
slotsOTPInputSlotData[]- One entry per slot: its character, whether it's active, and whether it draws a caret.
focusedboolean- Whether the input has focus.
completeboolean- Whether every slot is filled.
disabledboolean- Whether it's disabled, by itself or by a
<fieldset>.
Slots get their own:
charstring | null- The character in the slot, or null when it's empty.
activeboolean- Whether the slot is where typing goes: the selected character, or the empty slot after the last one.
hasFakeCaretboolean- Whether the slot should draw a caret: it's active and empty. The real caret is hidden.
indexnumber- Which character of the code the slot shows.
focusedboolean- Whether the input has focus.
disabledboolean- Whether it's disabled, by itself or by a
<fieldset>.
Reasons
$event.reason says why the code changed. Autofill and a code from the keyboard's suggestions are input-change.
input-changeEvent- Typing, pasting or dropping changed a text field's value; in a combobox it can also open or close it, or Enter keeps the typed text as the value.
pasteClipboardEvent- Files were pasted, like a screenshot, or text and tokens into a token input.
undoKeyboardEvent | InputEvent- Undo, from the keyboard, the Edit menu or a controller.
redoKeyboardEvent | InputEvent- Redo, from the keyboard, the Edit menu or a controller.
resetnone- A form reset.
restorenone- The browser restored state, like after going back, or a saved value was read back from storage.
imperativenone- A controller or
apicall.
Controller
Create one with createOTPInputController() and pass it as controller.
setValue() and clear() take an optional { reason, event }, and run the change callback like typing.
getValue()() => string- The code.
setValue(value, options?)(value: string, options?: OTPInputChangeOptions | undefined) => boolean- Sets the code, as typing it would. Characters that don't fit the pattern are dropped. Returns false when the change was canceled or nothing changed.
clear(options?)(options?: OTPInputChangeOptions | undefined) => boolean- Empties the code. Returns false when the change was canceled or nothing changed.
focus()() => void- Moves focus to the input.
Events
A before output emits { value, reason, event, cancel() } before the change, and its plain output follows with the value alone. A bound value only updates when the change isn't canceled.
| Binding | On | Receives |
|---|---|---|
[(value)] | uiOTPInput | The code |
(beforeValueChange) | uiOTPInput | { value, reason, cancel() }: the new code and the reason |
(valueCommit) | uiOTPInput | { value, reason, cancel() }: the full code and the reason that filled it |
Accessibility
Name the input with FieldLabel, a native label, aria-label, or aria-labelledby. Screen readers encounter one input with the whole code; the visual slots, groups, and separators are hidden from them.
Keyboard
The input keeps its native keys. Arrow keys, Home, and End move the selection between slots, Backspace deletes, and your usual shortcuts undo and redo. Pasting a code such as 123-456 removes the separators.
Tested Behavior
Every line below is an automated test.
- Typing fills the slots in order.
- Characters the pattern doesn't accept are ignored.
- Filling every slot completes the code.
- Backspace deletes the last character.
- ArrowLeft goes back a slot, and typing replaces its character.
- Pressing a filled slot lets you retype it.
- Pasting a code with a dash fills every slot.
- The form submits the code under its name.
- A partial default code is too short to submit.
- A typed partial code is too short, not the wrong format.
- A canceled change keeps the code.
- A disabled code can't be typed into.
- A code of letters and digits takes both.
- The input asks for a one-time code and a numeric keyboard.
- Typing a whole code with no waits fills it.
Runs in React, Vue and web components.