← All guides BOOTSTRAP 6 · ALPHA 1

OTP Input Examples

Interactive one-time password, PIN, and verification-code examples with ready-to-copy code.

Based on the official Bootstrap 6 OTP Input documentation. Requires JavaScript and an internet connection for the CDN.

Version note: Bootstrap 6 is currently an alpha release. This page uses the Bootstrap 6.0.0-alpha.1 CDN. Verify the exact version against your project's installed files before deploying.

How it works

Bootstrap's OTP component uses one real text input and renders its value as separate visual slots. This is more accessible than creating a separate input for every digit and supports paste, keyboard editing, autofill, and password managers.

  • Wrap one input in a <div class="otp" data-bs-otp>.
  • Set maxlength to define the number of slots.
  • Use data-bs-type, data-bs-mask, and data-bs-groups to customize behavior.
  • Use a label or aria-label to provide an accessible name.
Basic HTML
<div class="otp" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6"
         aria-label="Verification code"
         autocomplete="one-time-code">
</div>

Interactive examples

1Connected inputs

Connected slots share borders and look like a single segmented control.

HTML
<div class="otp otp-connected" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6"
         aria-label="Verification code"
         autocomplete="one-time-code">
</div>

2Four-digit PIN

Set maxlength="4" for a four-slot PIN.

HTML
<div class="otp otp-connected" data-bs-otp>
  <input type="text" class="otp-input" maxlength="4"
         aria-label="PIN" autocomplete="one-time-code">
</div>

For security, validate the PIN on the server; don't rely on the UI alone.

3With separator

Split a six-digit code into groups of three, such as 123·456. The separator is visual only.

HTML
<div class="otp" data-bs-otp data-bs-groups="[3,3]">
  <input type="text" class="otp-input" maxlength="6"
         aria-label="Verification code"
         autocomplete="one-time-code">
</div>

Set a custom separator with data-bs-separator="-".

4Alphanumeric and masking

Alphanumeric mode accepts letters and digits. Mask mode visually hides the entered characters.

HTML
<!-- Letters and digits -->
<div class="otp otp-connected" data-bs-otp
     data-bs-type="alphanumeric">
  <input type="text" class="otp-input" maxlength="6"
         aria-label="Alphanumeric code">
</div>

<!-- Mask the displayed characters -->
<div class="otp otp-connected" data-bs-otp data-bs-mask="true">
  <input type="text" class="otp-input" maxlength="6"
         aria-label="Masked code">
</div>

5Validation

Use .is-valid or .is-invalid on the OTP wrapper to show status styles.

HTML
<div class="otp otp-connected is-valid" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6"
         value="123456" aria-label="Verification code">
</div>

<div class="otp otp-connected is-invalid" data-bs-otp>
  <input type="text" class="otp-input" maxlength="6"
         value="123" aria-label="Verification code">
</div>

The classes display UI state. Your application must determine whether the code is actually valid.

6With form label

Use a real label connected with for and id, and describe the field with aria-describedby.

Enter the 6-digit code sent to your phone.
HTML
<div class="vstack gap-2">
  <label class="form-label" for="otpCode">Verification code</label>
  <div class="otp" data-bs-otp>
    <input type="text" class="otp-input" id="otpCode"
           maxlength="6" aria-describedby="otpHelp"
           autocomplete="one-time-code">
  </div>
  <div id="otpHelp" class="form-text">
    Enter the 6-digit code sent to your phone.
  </div>
</div>

Try validation and read the value

Enter a six-digit code and click Check code. For this demonstration only, 123456 is considered valid.

Demo code: 123456
JavaScript
const wrapper = document.querySelector('#otpValidationWrapper');
const otp = bootstrap.OtpInput.getOrCreateInstance(wrapper);

document.querySelector('#otpDemoForm').addEventListener('submit', event => {
  event.preventDefault();
  const value = otp.getValue();

  if (value.length !== 6) {
    wrapper.classList.remove('is-valid');
    wrapper.classList.add('is-invalid');
    // Show an error message.
    return;
  }

  // Send value to your server to verify the real OTP.
  console.log('OTP entered:', value);
});

The demo compares against a sample value in browser code only. Never use a client-side comparison as real OTP security.

Quick reference

Attribute / APIPurposeExample
data-bs-otpInitializes OTP behavior.<div class="otp" data-bs-otp>
maxlengthSets the number of slots by default.maxlength="6"
data-bs-lengthExplicit number of slots.data-bs-length="6"
data-bs-typeAllowed character type.numeric, alphanumeric, alpha
data-bs-mask="true"Display a dot for each character.data-bs-mask="true"
data-bs-groupsSplit slots into groups.data-bs-groups="[3,3]"
data-bs-separatorChoose the group separator.data-bs-separator="-"
.otp-connectedJoin slots into one connected field.class="otp otp-connected"
.is-valid / .is-invalidShow success/error styling.Applied to the OTP wrapper
getValue()Read the current code.otp.getValue()
setValue(value)Set the code programmatically.otp.setValue("654321")
clear()Clear the code and focus the field.otp.clear()
complete.bs.otpInputFires when all slots are filled.wrapper.addEventListener(...)

Listen for completion

Use the completion event when you want to enable a button or submit the code once all slots have been filled.

const wrapper = document.querySelector('#otpWrapper');

wrapper.addEventListener('complete.bs.otpInput', event => {
  console.log('OTP complete:', event.value);
  // Submit to your server-side verification endpoint.
});

wrapper.addEventListener('input.bs.otpInput', event => {
  console.log('Current OTP:', event.value);
});