A Bootstrap 6 two-factor verification card built on the new OtpInput component. A single input is drawn as six digit slots in two groups. The form submits itself when the last digit is entered or a full code is pasted. It also has an attempt limit, a resend countdown and a "Trust this device" switch. Use it after a password login, for phone or email confirmation, or before sensitive account changes.
How it works
The code field is Bootstrap 6 markup: <div class="otp otp-lg" data-bs-otp> around one <input class="otp-input" maxlength="6">. The data API turns it into visual .otp-slot boxes while the real input stays underneath. So typing, deleting, pasting and SMS autofill (autocomplete="one-time-code" is added for you) all work as in a normal field. data-bs-groups="[3,3]" splits the slots with a data-bs-separator dash.
script.js gets the instance with OtpInput.getOrCreateInstance() and listens to two events. input.bs.otpInput enables the Verify button only when all digits are present. complete.bs.otpInput calls form.requestSubmit(). The submit handler simulates a server check against data-otv-code. A wrong code adds .is-invalid to .otp (red slots and a short shake), shows the message in .otv-error with the remaining attempts, and calls otp.clear(). After MAX_ATTEMPTS the input is disabled until a new code is requested.
The resend link starts disabled with a countdown from data-otv-resend seconds, shown in .otv-seconds. When it reaches zero, the button is enabled and a polite live region says so. A correct code shows the .otv-pane--done success state.
Customization
data-otv-codeon.otv-root: the demo code. In production, verify on the server and remove this attribute and the.otv-hintline.data-otv-resendon.otv-root: the resend delay in seconds (30 by default).MAX_ATTEMPTSinscript.js: how many wrong codes are allowed before a resend is required.maxlengthon.otp-inputanddata-bs-groupson.otp: change the code length or grouping, e.g. 8 digits as[4,4].--bs-otp-size,--bs-otp-gapand--bs-otp-slot-border-radiuson.otv-otp: resize and reshape the slots.--otv-accenton.otv-root: the brand color for the button, switch, filled slots and focus rings.- Remove
data-otv-demoto start with empty slots. Replace thesetTimeoutcalls withfetch('/api/otp/verify')andfetch('/api/otp/resend').
Use cases
- Two-factor login: a six-digit code is entered after the password step
- Phone or email confirmation: the form submits itself when the last digit or a pasted code arrives
- Sensitive account changes: an attempt limit blocks repeated wrong guesses
- Trusted devices: a resend countdown and a Trust this device switch finish the flow
Accessibility
The input has a visually hidden <label> and is described by the hint and the error message. The decorative slots are aria-hidden, so screen readers read the single input. Errors use role="alert", resend availability is announced politely, and focus moves to the success heading after verification. The shake animation is turned off under prefers-reduced-motion.
Bootstrap 6 notes
Bootstrap 5 had no OTP field, so developers built rows of separate inputs. Bootstrap 6.0.0-alpha.1 adds .otp with data-bs-otp, plus otp-connected, otp-sm/otp-lg and the complete.bs.otpInput event. The switch uses the new .switch wrapper with a native switch attribute, and the button is btn-solid theme-primary. Alpha APIs can still change. Icons come from Bootstrap Icons.