Skip to content

OTP Input

A segmented one-time password input that renders individual cells for each digit. Auto-advances focus between cells, supports paste, Backspace navigation, and fires completion events.

Features

  • Backspace Navigation — moves focus backward and clears the cell
  • ⏭️ Auto-Advance — focus moves to the next cell automatically on input
  • Optional Separator — visual divider between cells
  • 🌈 6 Semantic Colors — primary, secondary, info, success, warning, error
  • 🎨 5 Variants — solid, flat, bordered, outline, ghost
  • 📋 Paste Support — pastes fill all cells at once
  • 📏 3 Sizes — sm, md, lg
  • 🔒 Masked Mode — renders characters instead of input values
  • 🔗 Form-Associatedname attribute & native form reset support
  • 🔢 Configurable Length — any number of cells (default 6)
  • 🔤 Two Input Typesnumeric (digits only) or alphanumeric

Source Code

View Source Code
ts
import { computed, defineComponent, html, onMount, typed } from '@vielzeug/craftit';

import type { ComponentSize, ThemeColor, VisualVariant } from '../../types';

import { colorThemeMixin, forcedColorsFocusMixin, sizeVariantMixin } from '../../styles';
import styles from './otp-input.css?inline';

export type BitOtpInputEvents = {
  change: { complete: boolean; originalEvent?: Event; value: string };
  complete: { originalEvent?: Event; value: string };
};

/** OTP Input props */
export type BitOtpInputProps = {
  /** Theme color */
  color?: ThemeColor;
  /** Make inputs disabled */
  disabled?: boolean;
  /** Accessible label */
  label?: string;
  /** Number of input cells */
  length?: number;
  /** Mask input (show dots instead of characters) */
  masked?: boolean;
  /** Form field name */
  name?: string;
  /** Show a separator in the middle (e.g. "–") */
  separator?: string;
  /** Component size */
  size?: ComponentSize;
  /** Input type: 'numeric' (digits only) or 'alphanumeric' */
  type?: 'numeric' | 'alphanumeric';
  /** Current value */
  value?: string;
  /** Visual variant */
  variant?: Exclude<VisualVariant, 'text' | 'frost' | 'glass'>;
};

/**
 * A segmented OTP (One-Time Password) input with N individual cells.
 * Auto-advances focus on input, auto-backs on Backspace, handles paste.
 *
 * @element bit-otp-input
 *
 * @attr {number} length - Number of cells (default: 6)
 * @attr {string} type - 'numeric' (default) | 'alphanumeric'
 * @attr {string} value - Current code value
 * @attr {boolean} disabled - Disable all cells
 * @attr {boolean} masked - Show as password
 * @attr {string} size - 'sm' | 'md' | 'lg'
 * @attr {string} color - Theme color
 * @attr {string} name - Form field name
 * @attr {string} label - Group aria-label
 * @attr {string} separator - Optional separator character shown in the middle
 *
 * @fires change - Emitted whenever a cell changes. detail: { value: string, complete: boolean, originalEvent?: Event }
 * @fires complete - Emitted when all cells are filled. detail: { value: string, originalEvent?: Event }
 *
 * @cssprop --otp-cell-size - Width and height of each cell
 * @cssprop --otp-cell-gap - Gap between cells
 * @cssprop --otp-cell-font-size - Font size inside cells
 * @cssprop --otp-cell-radius - Border radius of cells
 * @cssprop --otp-cell-border-color - Default border color
 * @cssprop --otp-cell-focus-border - Focused border color
 *
 * @example
 * ```html
 * <bit-otp-input length="6" color="primary"></bit-otp-input>
 * ```
 */
export const OTP_INPUT_TAG = defineComponent<BitOtpInputProps, BitOtpInputEvents>({
  props: {
    color: typed<BitOtpInputProps['color']>(undefined),
    disabled: typed<boolean>(false),
    label: typed<string>('One-time password'),
    length: typed<number>(6),
    masked: typed<boolean>(false),
    name: typed<string | undefined>(undefined),
    separator: typed<string | undefined>(undefined),
    size: typed<BitOtpInputProps['size']>(undefined),
    type: typed<BitOtpInputProps['type']>('numeric'),
    value: typed<string>(''),
    variant: typed<BitOtpInputProps['variant']>(undefined),
  },
  setup({ emit, host, props }) {
    const cells = computed(() => Array.from({ length: Number(props.length.value) || 6 }, (_, i) => i));

    function getInputs(): HTMLInputElement[] {
      return [...(host.shadowRoot?.querySelectorAll<HTMLInputElement>('input.cell') ?? [])];
    }
    function getValue(): string {
      return getInputs()
        .map((i) => i.value)
        .join('');
    }
    function isAllowed(char: string): boolean {
      if (props.type.value === 'numeric') return /^\d$/.test(char);

      return /^[a-z\d]$/i.test(char);
    }
    function handleInput(e: Event, index: number) {
      const input = e.target as HTMLInputElement;
      let val = input.value;

      // Keep only last character if multiple were typed somehow
      if (val.length > 1) val = val.slice(-1);

      // Validate character
      if (val && !isAllowed(val)) {
        input.value = '';

        return;
      }

      input.value = val;

      const allInputs = getInputs();
      const full = getValue();
      const complete = full.length === allInputs.length && full.split('').every(Boolean);

      host.setAttribute('value', full);
      emit('change', { complete, originalEvent: e, value: full });

      if (complete) emit('complete', { originalEvent: e, value: full });

      // Auto-advance
      if (val && index < allInputs.length - 1) {
        allInputs[index + 1].focus();
        allInputs[index + 1].select();
      }
    }
    function handleKeydown(e: KeyboardEvent, index: number) {
      const input = e.target as HTMLInputElement;
      const allInputs = getInputs();

      if (e.key === 'Backspace') {
        if (input.value) {
          input.value = '';
        } else if (index > 0) {
          allInputs[index - 1].focus();
          allInputs[index - 1].select();
          allInputs[index - 1].value = '';
        }

        const full = getValue();

        host.setAttribute('value', full);
        emit('change', { complete: false, originalEvent: e, value: full });
        e.preventDefault();
      } else if (e.key === 'ArrowLeft' && index > 0) {
        allInputs[index - 1].focus();
        e.preventDefault();
      } else if (e.key === 'ArrowRight' && index < allInputs.length - 1) {
        allInputs[index + 1].focus();
        e.preventDefault();
      } else if (e.key === 'Home') {
        allInputs[0].focus();
        e.preventDefault();
      } else if (e.key === 'End') {
        allInputs[allInputs.length - 1].focus();
        e.preventDefault();
      }
    }
    function handlePaste(e: ClipboardEvent) {
      e.preventDefault();

      const pasted = e.clipboardData?.getData('text') ?? '';
      const chars = pasted
        .split('')
        .filter((c) => isAllowed(c))
        .slice(0, Number(props.length.value) || 6);
      const allInputs = getInputs();

      chars.forEach((char, i) => {
        if (allInputs[i]) allInputs[i].value = char;
      });

      const full = getValue();
      const complete = full.length === allInputs.length && full.split('').every(Boolean);

      host.setAttribute('value', full);
      emit('change', { complete, originalEvent: e, value: full });

      if (complete) emit('complete', { originalEvent: e, value: full });

      // Focus the cell after last pasted char
      const focusIdx = Math.min(chars.length, allInputs.length - 1);

      allInputs[focusIdx]?.focus();
    }
    onMount(() => {
      // Populate cells from value prop on mount
      const initialVal = String(props.value.value || '');
      const allInputs = getInputs();

      initialVal.split('').forEach((c, i) => {
        if (allInputs[i]) allInputs[i].value = c;
      });
    });

    const separatorIdx = computed(() => {
      const len = Number(props.length.value) || 6;

      return props.separator.value != null ? Math.floor(len / 2) : -1;
    });

    return html`
      <div class="otp-group" part="group" role="group" :aria-label="${() => props.label.value}">
        ${() =>
          cells.value.map(
            (i) => html`
              ${() =>
                separatorIdx.value > 0 && i === separatorIdx.value
                  ? html`<span class="separator" aria-hidden="true">${() => props.separator.value || '-'}</span>`
                  : ''}
              <input
                class="cell"
                part="cell"
                :type="${() => (props.masked.value ? 'password' : 'text')}"
                :inputmode="${() => (props.type.value === 'numeric' ? 'numeric' : 'text')}"
                maxlength="1"
                :autocomplete="${() => (i === 0 ? 'one-time-code' : 'off')}"
                :aria-label="${() => `Digit ${i + 1} of ${props.length.value}`}"
                :disabled="${() => props.disabled.value || null}"
                :name="${() => (props.name.value ? `${props.name.value}[${i}]` : null)}"
                @input="${(e: Event) => handleInput(e, i)}"
                @keydown="${(e: KeyboardEvent) => handleKeydown(e, i)}"
                @paste="${(e: ClipboardEvent) => (i === 0 ? handlePaste(e) : e.preventDefault())}"
                @focus="${(e: FocusEvent) => (e.target as HTMLInputElement).select()}" />
            `,
          )}
      </div>
    `;
  },
  styles: [colorThemeMixin, sizeVariantMixin({}), forcedColorsFocusMixin('.cell'), styles],
  tag: 'bit-otp-input',
});

Basic Usage

html
<bit-otp-input label="Verification code" color="primary"></bit-otp-input>

<script type="module">
  import '@vielzeug/buildit';
</script>

Listen for completion:

html
<bit-otp-input id="otp" label="Enter OTP" color="primary"></bit-otp-input>

<script type="module">
  import '@vielzeug/buildit';

  document.getElementById('otp').addEventListener('complete', (e) => {
    console.log('OTP complete:', e.detail.value);
  });
</script>

Length

PreviewCode
RTL

Types

PreviewCode
RTL

Masked Input

Use masked to hide the entered values (useful for PINs).

PreviewCode
RTL

With Separator

Use separator to add a visual divider between cells.

PreviewCode
RTL

Sizes

PreviewCode
RTL

Colors

PreviewCode
RTL

Variants

PreviewCode
RTL

Disabled

PreviewCode
RTL

API Reference

Attributes

AttributeTypeDefaultDescription
lengthnumber6Number of input cells
type'numeric' | 'alphanumeric''numeric'Allowed character type
valuestring''Current value (string of all cells concatenated)
maskedbooleanfalseRender cells as hidden characters
separatorbooleanfalseShow a visual divider between cells
disabledbooleanfalseDisable all cells
labelstringAccessible label for the group
namestringForm field name
color'primary' | 'secondary' | 'info' | 'success' | 'warning' | 'error'Focus ring / active cell color
size'sm' | 'md' | 'lg''md'Cell size
variant'solid' | 'flat' | 'bordered' | 'outline' | 'ghost''solid'Visual style variant

Events

EventDetailDescription
change{ value: string, complete: boolean }Fired on every cell input
complete{ value: string }Fired when all cells have been filled

CSS Custom Properties

PropertyDescription
--otp-cell-sizeWidth and height of each cell
--otp-cell-gapGap between cells
--otp-cell-font-sizeFont size inside each cell
--otp-cell-radiusCell border radius
--otp-cell-border-colorDefault cell border color
--otp-cell-focus-borderCell border color when focused

Accessibility

The OTP input component follows WCAG 2.1 Level AA standards.

bit-otp-input

Keyboard Navigation

  • Focus auto-advances to the next cell on valid input; Backspace moves back and clears the cell.
  • Tab moves focus out of the group; paste fills all cells at once.

Screen Readers

  • Renders as a <fieldset> with a <legend> for the label attribute.
  • autocomplete="one-time-code" is set automatically on each cell input.
  • aria-disabled reflects the disabled state.

Best Practices

Do:

  • Always provide a label attribute to give context (e.g. "Verification code").
  • Use masked for PINs and security codes to prevent shoulder-surfing.
  • Listen to complete (not change) to trigger auto-submission after the last cell is filled.
  • Keep length at 4–8 cells — more cells increase cognitive load.

Don't:

  • Auto-submit without giving users a chance to review — show a confirmation step before sending.
  • Use alphanumeric for numeric-only codes (e.g. SMS OTPs) — numerics trigger the numeric keyboard on mobile.
  • Input — plain single-line input field