Used to enter a one-time password (OTP).
Basic Usage
export function InputOtpBasic() { const [otp, setOtp] = useState("123"); return ( <div> <ElInputOtp value={otp} onChange={setOtp} /> <div style={valueText}>Value: {otp}</div> </div> );}
Custom Length
The length of the input fields can be customized by setting the length prop.
export function InputOtpCustomLength() { const [otp1, setOtp1] = useState(""); const [otp2, setOtp2] = useState(""); return ( <div style={stack}> <ElInputOtp value={otp1} onChange={setOtp1} length={4} /> <ElInputOtp value={otp2} onChange={setOtp2} length={8} /> </div> );}
Types
There are three types available: outlined (default), filled, and underlined.
export function InputOtpTypes() { const [otp1, setOtp1] = useState(""); const [otp2, setOtp2] = useState(""); const [otp3, setOtp3] = useState(""); return ( <div style={stack}> <div> <p style={label}>Outlined (Default)</p> <ElInputOtp value={otp1} onChange={setOtp1} type="outlined" /> </div> <div> <p style={label}>Filled</p> <ElInputOtp value={otp2} onChange={setOtp2} type="filled" /> </div> <div> <p style={label}>Underlined</p> <ElInputOtp value={otp3} onChange={setOtp3} type="underlined" /> </div> </div> );}
Sizes
There are three sizes available: large, default, and small.
export function InputOtpSizes() { const [otp1, setOtp1] = useState(""); const [otp2, setOtp2] = useState(""); const [otp3, setOtp3] = useState(""); return ( <div style={stack}> <div> <p style={label}>Large</p> <ElInputOtp value={otp1} onChange={setOtp1} size="large" /> </div> <div> <p style={label}>Default</p> <ElInputOtp value={otp2} onChange={setOtp2} size="default" /> </div> <div> <p style={label}>Small</p> <ElInputOtp value={otp3} onChange={setOtp3} size="small" /> </div> </div> );}
Disabled & Readonly
Disabled and readonly states are supported.
export function InputOtpDisabled() { const [otp1] = useState("123"); const [otp2] = useState("456"); return ( <div style={stack}> <div style={typeWrap}> <p style={label}>Disabled</p> <ElInputOtp value={otp1} disabled /> <ElInputOtp value={otp1} disabled type="filled" /> <ElInputOtp value={otp1} disabled type="underlined" /> </div> <div style={typeWrap}> <p style={label}>Readonly</p> <ElInputOtp value={otp2} readonly /> <ElInputOtp value={otp2} readonly type="filled" /> <ElInputOtp value={otp2} readonly type="underlined" /> </div> </div> );}
Mask
Use the mask prop to hide the input characters.
export function InputOtpMask() { const [otp, setOtp] = useState("123"); return ( <div> <ElInputOtp value={otp} onChange={setOtp} mask /> <div style={valueText}>Value: {otp}</div> </div> );}
Separator
Customize the separator between OTP fields.
export function InputOtpSeparator() { return ( <div style={stack}> <ElInputOtp separator="/" /> <ElInputOtp separator={(index) => (index & 1 ? "—" : "")} /> <ElInputOtp separatorSlot={() => "•"} /> </div> );}
Custom Validation
Set the validator prop to validate the input character, and use inputmode to specify the keyboard type.
export function InputOtpValidator() { const [otp1, setOtp1] = useState(""); const [otp2, setOtp2] = useState(""); const onlyAllowNumber = (value: string) => /^\d$/.test(value); const onlyAllowLetter = (value: string) => /^[a-zA-Z]$/.test(value); return ( <div style={stack}> <div> <p style={label}>Numbers only</p> <ElInputOtp value={otp1} onChange={setOtp1} validator={onlyAllowNumber} inputmode="numeric" /> </div> <div> <p style={label}>Letters only</p> <ElInputOtp value={otp2} onChange={setOtp2} validator={onlyAllowLetter} /> </div> </div> );}
API
Attributes
| Name | Description | Type | Default |
|---|
| model-value / v-model | The value of the OTP fields. Since numbers must not have leading zeros, modelValue is allowed to be a number only during initialization. | string / number | undefined |
| length | The OTP fields length | number | 6 |
| validator | Custom validator function | Function | () => true |
| inputmode | Native inputmode attribute | string | — |
| type | The type of the OTP fields | enum | 'outlined' |
| size | The size of the OTP fields | enum | — |
| mask | Whether to enable password mode | boolean | — |
| disabled | Whether the OTP fields are disabled | boolean | undefined |
| separator | The separator between OTP fields | string / VNode / Function | — |
| validate-event | Whether to trigger form validation | boolean | true |
| readonly | Same as readonly in native input | boolean | false |
| id | Native id attribute | string | — |
| aria-label a11y | Native aria-label attribute | string | — |
Events
| Name | Description | Type |
|---|
| update:modelValue | Triggers when value updates | Function |
| change | Triggers when the value changes after input blur | Function |
| finish | Fires when all fields have been filled | Function |
| focus | Triggers when input is focused | Function |
| blur | Triggers when input is blurred | Function |
Slots
| Name | Description | Type |
|---|
| separator | The separator between OTP fields | object |
Exposes
| Name | Description | Type |
|---|
| inputRefs | HTML input elements array | object |
| focus | Focus an OTP input field | Function |
| blur | Blur the focused OTP input field | Function |