Skip to main content

Opt Input

<sl-opt-input> | SlOptInput
Since 2.14 hub24

A one-time password (OTP) input component with multiple character slots.

<sl-opt-input label="Enter verification code" help-text="Enter the 6-digit code sent to your device"></sl-opt-input>

Examples

Basic OTP Input

The default OTP input has 6 slots for numeric input.

<sl-opt-input label="Verification Code"></sl-opt-input>

Custom Length

Use the length attribute to change the number of input slots.



<sl-opt-input label="4-digit PIN" length="4"></sl-opt-input>
<br />
<sl-opt-input label="6-digit PIN" length="6" numeric-only></sl-opt-input>
<br />
<sl-opt-input label="8-digit code" length="8"></sl-opt-input>

Text Input

Set type="text" to allow any character instead of just numbers.

<sl-opt-input label="Alphanumeric Code" type="text" length="6"></sl-opt-input>

Numeric Only Validation

Use numeric-only to restrict input to numbers only (0–9).

<sl-opt-input label="Numeric Code" numeric-only length="6"></sl-opt-input>

Alphabetic Only Validation

Use alpha-only to restrict input to letters only (a-z, A-Z).

<sl-opt-input label="Letter Code" alpha-only length="6" type="text"></sl-opt-input>

Alphanumeric Only Validation

Use alphanumeric-only to allow only letters and numbers (a-z, A-Z, 0–9).

<sl-opt-input label="Alphanumeric Code" alphanumeric-only length="6" type="text"></sl-opt-input>

Auto Submit

Enable auto-submit to automatically submit the form when all slots are filled.


<form class="auto-submit-form">
  <sl-opt-input 
    label="Verification Code" 
    auto-submit 
    numeric-only 
    length="6"
    class="auto-submit-input">
  </sl-opt-input>
  <br />
  <div class="submit-result" style="margin-top: 1rem; padding: 1rem; background: #f0f0f0; border-radius: 4px; display: none;">
    <strong>Form submitted!</strong> Code: <span class="submitted-code"></span>
  </div>
</form>

<script>
  const form = document.querySelector('.auto-submit-form');
  const input = document.querySelector('.auto-submit-input');
  const result = document.querySelector('.submit-result');
  const codeSpan = document.querySelector('.submitted-code');

  form.addEventListener('submit', (e) => {
    e.preventDefault();
    codeSpan.textContent = input.value;
    result.style.display = 'block';
    setTimeout(() => {
      result.style.display = 'none';
      input.clear();
    }, 2000);
  });
</script>

Complete Event

Listen for the sl-complete event which fires when all slots are filled.

<div class="complete-event-demo">
  <sl-opt-input 
    label="Enter Code" 
    numeric-only
    class="complete-input">
  </sl-opt-input>
  <div class="complete-message" style="margin-top: 1rem; color: green; display: none;">
    ✓ Code complete! Value: <strong class="complete-value"></strong>
  </div>
</div>

<script>
  const completeInput = document.querySelector('.complete-input');
  const completeMessage = document.querySelector('.complete-message');
  const completeValue = document.querySelector('.complete-value');

  completeInput.addEventListener('sl-complete', (e) => {
    completeValue.textContent = e.detail.value;
    completeMessage.style.display = 'block';
  });

  completeInput.addEventListener('sl-input', () => {
    if (completeInput.value.length < 6) {
      completeMessage.style.display = 'none';
    }
  });
</script>

Help Text

Add descriptive help text to assist users with the help-text attribute or slot.

<sl-opt-input label="Security Code" help-text="Check your email for the 6-digit verification code"></sl-opt-input>

Label with Icon

Use the label-suffix slot to add an icon or additional content after the label.

<sl-opt-input label="Verification Code">
  <sl-icon name="circle-info" slot="label-suffix"></sl-icon>
</sl-opt-input>

Label with Tooltip

Add a tooltip to the label icon to provide helpful information on hover.



<sl-opt-input label="Verification Code" numeric-only>
  <sl-tooltip content="Enter the 6-digit code sent to your email or phone" slot="label-suffix">
    <sl-icon name="circle-info"></sl-icon>
  </sl-tooltip>
</sl-opt-input>

<br /><br />

<sl-opt-input label="Security PIN" numeric-only length="4">
  <sl-tooltip content="Your 4-digit PIN is used to secure your account" slot="label-suffix">
    <sl-icon name="fas-circle-info"></sl-icon>
  </sl-tooltip>
</sl-opt-input>

Disabled

Use the disabled attribute to disable the input.

<sl-opt-input label="Verification Code" disabled value="123456"></sl-opt-input>

Readonly

Use the readonly attribute to make the input readonly.

<sl-opt-input label="Verification Code" readonly value="123456"></sl-opt-input>

Getting and Setting Values

Use the value property to get or set the current value.


Set Value Clear Get Value
<div class="opt-input-value-example">
  <sl-opt-input label="Enter Code" class="opt-value"></sl-opt-input>
  <br />
  <sl-button onclick="document.querySelector('.opt-value').value = '123456'">Set Value</sl-button>
  <sl-button onclick="document.querySelector('.opt-value').clear()">Clear</sl-button>
  <sl-button onclick="alert(document.querySelector('.opt-value').value)">Get Value</sl-button>
</div>

Listening for Events

Listen for sl-change and sl-input events to react when the value changes.

<div class="opt-input-events">
  <sl-opt-input label="Enter Code" class="opt-events"></sl-opt-input>
  <div class="event-log"></div>
</div>

<script>
  const optInput = document.querySelector('.opt-events');
  const eventLog = document.querySelector('.event-log');

  optInput.addEventListener('sl-input', () => {
    eventLog.innerHTML = `<strong>sl-input:</strong> Value = "${optInput.value}"`;
  });

  optInput.addEventListener('sl-change', () => {
    eventLog.innerHTML += `<br><strong>sl-change:</strong> Value = "${optInput.value}"`;
  });
</script>

[component-metadata:sl-opt-input]

Slots

Name Description
label The input’s label. Alternatively, you can use the label attribute.
help-text Text that describes how to use the input. Alternatively, you can use the help-text attribute.
label-suffix An icon or text to display after the label.

Learn more about using slots.

Properties

Name Description Reflects Type Default
label The label of the input. string ''
helpText
help-text
The help text of the input. string ''
length The number of OTP character slots. number 6
value The current OTP value. string ''
type The input type (text or number). 'text' | 'number' 'number'
disabled Disables the input. boolean false
readonly Makes the input readonly. boolean false
required Makes the input required. boolean false
name The input’s name attribute. string ''
pattern Pattern for validation (e.g., ”[0–9]*” for numbers only). string | undefined -
autoSubmit
auto-submit
Automatically submit/emit complete event when all slots are filled. boolean false
numericOnly
numeric-only
Allow only numeric input (0–9). When true, non-numeric characters are rejected. boolean false
alphaOnly
alpha-only
Allow only alphabetic input (a-z, A-Z). When true, non-alphabetic characters are rejected. boolean false
alphanumericOnly
alphanumeric-only
Allow only alphanumeric input (a-z, A-Z, 0–9). When true, special characters are rejected. boolean false
customValidator Custom validation function. Return true if the character is valid, false otherwise. (char: string) => boolean | undefined -
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
sl-change Emitted when the value changes. -
sl-input Emitted when the user inputs a character. -
sl-complete Emitted when all slots are filled and auto-submit is enabled. -
sl-focus Emitted when the component receives focus. -
sl-blur Emitted when the component loses focus. -

Learn more about events.

Methods

Name Description Arguments
focus() Sets focus on the first input. options: FocusOptions
blur() Removes focus from the component. -
clear() Clears all input values. -

Learn more about methods.

Custom Properties

Name Description Default
--slot-size The size of each input slot (default: 48px).
--slot-spacing The spacing between slots (default: 8px).

Learn more about customizing CSS custom properties.

Parts

Name Description
base The component’s base wrapper.
form-control The form control wrapper.
form-control-label The label wrapper.
form-control-input The input wrapper.
form-control-help-text The help text wrapper.
input-group The container for all input slots.
input Each individual input element.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.

  • <sl-icon>