Input OTP

InputOtp - Phlex implementation

Examples

Default

Accessible one-time password component with auto-advance and copy-paste functionality.

<%= render "ui/input_otp", length: 6 do %>
  <%= render "ui/input_otp/group" do %>
    <%= render "ui/input_otp/slot", index: 0 %>
    <%= render "ui/input_otp/slot", index: 1 %>
    <%= render "ui/input_otp/slot", index: 2 %>
  <% end %>
  <%= render "ui/input_otp/separator" %>
  <%= render "ui/input_otp/group" do %>
    <%= render "ui/input_otp/slot", index: 3 %>
    <%= render "ui/input_otp/slot", index: 4 %>
    <%= render "ui/input_otp/slot", index: 5 %>
  <% end %>
<% end %>

Four Digits

Shorter PIN-style input without separator.

<%= render "ui/input_otp", length: 4 do %>
  <%= render "ui/input_otp/group" do %>
    <%= render "ui/input_otp/slot", index: 0 %>
    <%= render "ui/input_otp/slot", index: 1 %>
    <%= render "ui/input_otp/slot", index: 2 %>
    <%= render "ui/input_otp/slot", index: 3 %>
  <% end %>
<% end %>

Pattern

Use the pattern prop to restrict input to specific characters.

<%= render "ui/input_otp", length: 6, pattern: "^[a-zA-Z0-9]+$" do %>
  <%= render "ui/input_otp/group" do %>
    <%= render "ui/input_otp/slot", index: 0 %>
    <%= render "ui/input_otp/slot", index: 1 %>
    <%= render "ui/input_otp/slot", index: 2 %>
    <%= render "ui/input_otp/slot", index: 3 %>
    <%= render "ui/input_otp/slot", index: 4 %>
    <%= render "ui/input_otp/slot", index: 5 %>
  <% end %>
<% end %>

Separator

Use the separator component to visually separate groups of slots.

<%= render "ui/input_otp", length: 6 do %>
  <%= render "ui/input_otp/group" do %>
    <%= render "ui/input_otp/slot", index: 0 %>
    <%= render "ui/input_otp/slot", index: 1 %>
  <% end %>
  <%= render "ui/input_otp/separator" %>
  <%= render "ui/input_otp/group" do %>
    <%= render "ui/input_otp/slot", index: 2 %>
    <%= render "ui/input_otp/slot", index: 3 %>
  <% end %>
  <%= render "ui/input_otp/separator" %>
  <%= render "ui/input_otp/group" do %>
    <%= render "ui/input_otp/slot", index: 4 %>
    <%= render "ui/input_otp/slot", index: 5 %>
  <% end %>
<% end %>

Features

  • Custom styling with Tailwind classes
  • Disabled state support

API Reference

Input OTP

Accessible one-time password container component.

Parameters

NameTypeDefaultDescription
lengthInteger6The length
patternString\\dThe pattern
nameStringnilForm field name
idStringnilHTML id attribute
disabledBooleanfalseWhether the element is disabled

Group

Container for grouping OTP input slots together visually.

Separator

Visual divider between OTP input groups.

Keyboard Shortcuts

KeyDescription
ArrowLeftMoves focus left or decreases value
ArrowRightMoves focus right or increases value
HomeMoves focus to first item
EndMoves focus to last item

JavaScript

Stimulus Controller

ui--input-otp

Values

NameTypeDescription
lengthNumberThe length

Actions

inputkeydownpastecheckCompleteclear

Events

EventDescriptionDetail
inputotp:completeFired when inputotp complete-