One field for the whole code
Native paste, autofill, selection and deletion, with optional decorative progress slots.
Keyboard
- Type or paste into the single labelled input.
- Arrow keys, selection and Backspace use native text editing.
- Submit focuses the input when the code is incomplete.
States covered
- Empty
- Partial
- Complete format
- Validation error
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-otp-code.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useRef,useState} from 'react';
import {Button,OTPInput} from '@bld8/ui';
export default function CodeExample(){
const [code,setCode]=useState(''),[error,setError]=useState(''),[status,setStatus]=useState('');
const input=useRef<HTMLInputElement>(null);
return <form noValidate onSubmit={event=>{
event.preventDefault();
if(code.length!==6){setError('Enter all six digits.');input.current?.focus();return;}
setError('');setStatus('Code format is ready. This example does not verify a code.');
}}>
<OTPInput label="Example verification code" name="verificationCode" length={6} value={code}
onValueChange={next=>{setCode(next);setError('');setStatus('');}} inputRef={input} required showSlots
hint="Paste a six digit code or type it in this single field." error={error}/>
<Button type="submit">Check example code</Button><p role="status">{status}</p>
</form>;
}