OTP Input
The OTPInput components provide one-time password / PIN input fields with CSS-only and multi-field variants.
Quick Start
Usage Scenarios
Single-Field CSS-Only OTP
Multi-Field Auto-Advance OTP
Custom Variant and Size
OTPInput(length=4, name="pin", variant="success", size="lg")
OTPInputGroup(length=8, name="pin", variant="danger", size="sm", gap=1)
Parameter Reference
OTPInput
| Parameter | Type | Default | Description |
|---|---|---|---|
length |
int |
6 |
Number of digits expected |
name |
str |
"otp" |
Form field name for the concatenated value |
variant |
str |
"primary" |
Bootstrap color variant for focus styling |
size |
"sm" \| "md" \| "lg" |
"md" |
Input size |
placeholder |
str |
"•" |
Character to show in each position |
**kwargs |
Any |
- | Additional HTML attributes |
OTPInputGroup
| Parameter | Type | Default | Description |
|---|---|---|---|
length |
int |
6 |
Number of digit boxes |
name |
str |
"otp" |
Form field name for the concatenated value |
variant |
str |
"primary" |
Bootstrap color variant for focus styling |
size |
"sm" \| "md" \| "lg" |
"md" |
Input size |
gap |
int |
2 |
Gap between boxes in Bootstrap spacing units |
autofocus |
bool |
True |
Auto-focus the first input on render |
**kwargs |
Any |
- | Additional HTML attributes |
Accessibility
- Single-field OTP uses
aria-labelwith the expected digit count. - Multi-field OTP uses
aria-labelper digit (e.g., "Digit 1 of 6"). autocomplete="one-time-code"is set for browser OTP autofill support.
API Reference
faststrap.components.forms.otp_input.OTPInput(length=6, *, name='otp', variant='primary', size='md', placeholder='•', **kwargs)
Render a CSS-only OTP/pin input with a single field.
Truly zero-JS: uses a single with maxlength, inputmode, and CSS letter-spacing for the visual split appearance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Number of digits expected (default 6) |
6
|
name
|
str
|
Form field name for the concatenated value |
'otp'
|
variant
|
Literal['primary', 'secondary', 'success', 'danger', 'warning', 'info']
|
Bootstrap color variant for focus styling |
'primary'
|
size
|
Literal['sm', 'md', 'lg']
|
Input size (sm, md, lg) |
'md'
|
placeholder
|
str
|
Character to show in each position (default "•") |
'•'
|
**kwargs
|
Any
|
Additional HTML attributes (cls, id, hx-, data-, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div wrapping a styled single-field OTP input |
Source code in src/faststrap/components/forms/otp_input.py
faststrap.components.forms.otp_input.OTPInputGroup(length=6, *, name='otp', variant='primary', size='md', gap=2, autofocus=True, **kwargs)
Render a multi-field OTP input with auto-advance.
Each digit gets its own input box. Uses a single line of JavaScript (via Faststrap INIT_SCRIPT) for auto-advancing focus between fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Number of digit boxes (default 6) |
6
|
name
|
str
|
Form field name for the concatenated value |
'otp'
|
variant
|
Literal['primary', 'secondary', 'success', 'danger', 'warning', 'info']
|
Bootstrap color variant for focus styling |
'primary'
|
size
|
Literal['sm', 'md', 'lg']
|
Input size (sm, md, lg) |
'md'
|
gap
|
int
|
Gap between boxes in Bootstrap spacing units (0-5) |
2
|
autofocus
|
bool
|
Auto-focus the first input on render |
True
|
**kwargs
|
Any
|
Additional HTML attributes (cls, id, hx-, data-, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div containing individual digit input boxes |
Source code in src/faststrap/components/forms/otp_input.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |