Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Input OTP

    One-time code input split into single-digit boxes, with auto advance, paste support, and one hidden field for form submit.

    Verification code
    Verification code
    Verification code
    Verification code
    Verification code
    <x-markas.input-otp name="code" label="Verification code" />
    import { InputOTP } from "@/components/markas/InputOTP";
    
    export default function InputOTPExample() {
      return <InputOTP name="code" label="Verification code" />;
    }
    <script setup lang="ts">
    import { ref } from "vue";
    import InputOTP from "@/components/markas/InputOTP.vue";
    
    const code = ref("");
    </script>
    
    <template>
      <InputOTP v-model="code" name="code" label="Verification code" />
    </template>
    <script lang="ts">
      import InputOTP from "$lib/components/markas/InputOTP.svelte";
    
      let code = $state("");
    </script>
    
    <InputOTP bind:value={code} name="code" label="Verification code" />
    ---
    import InputOTP from "@/components/markas/InputOTP.astro";
    ---
    
    <InputOTP name="code" label="Verification code" />
    import { Component, signal } from "@angular/core";
    import { InputOTP } from "./markas/input-otp.component";
    
    @Component({
      selector: "markas-example",
      imports: [InputOTP],
      template: `
        <markas-input-otp [(value)]="code" name="code" label="Verification code" />
      `,
    })
    export default class InputOTPExample {
      readonly code = signal("");
    }

    Examples

    Four digits

    Card PIN
    Card PIN
    Card PIN
    Card PIN
    Card PIN

    States

    Filled
    Disabled
    Filled
    Disabled
    Filled
    Disabled
    Filled
    Disabled
    Filled
    Disabled

    Verification card

    Check your email

    We sent a 6-digit code to ana@example.com. It expires in 10 minutes.

    Verification code

    Did not get a code? Send again

    Check your email

    We sent a 6-digit code to ana@example.com. It expires in 10 minutes.

    Verification code

    Did not get a code? Send again

    Check your email

    We sent a 6-digit code to ana@example.com. It expires in 10 minutes.

    Verification code

    Did not get a code? Send again

    Check your email

    We sent a 6-digit code to ana@example.com. It expires in 10 minutes.

    Verification code

    Did not get a code? Send again

    Check your email

    We sent a 6-digit code to ana@example.com. It expires in 10 minutes.

    Verification code

    Did not get a code? Send again

    Installation

    npx markasui@latest add input-otp

    Usage

    <x-markas.input-otp
        name="code"
        label="Verification code"
        x-on:markas-complete="$el.closest('form').requestSubmit()"
    />

    Needs Alpine.js. Every change dispatches markas-change and a full code dispatches markas-complete, both with the code in $event.detail. Start with a code through value.

    Livewire

    <x-markas.input-otp label="Code" wire:model.live="code" x-on:markas-complete="$wire.verify()" />

    wire:model binds the joined code as one string. Listen for markas-complete to verify once every box is filled.

    import { useState } from "react";
    import { InputOTP } from "@/components/markas/InputOTP";
    
    const [code, setCode] = useState("");
    
    <InputOTP
      name="code"
      label="Verification code"
      value={code}
      onValueChange={setCode}
      onComplete={(value) => verify(value)}
    />;
    <script setup lang="ts">
    import { ref } from "vue";
    import InputOTP from "@/components/markas/InputOTP.vue";
    
    const code = ref("");
    </script>
    
    <template>
      <InputOTP
        v-model="code"
        name="code"
        label="Verification code"
        @complete="(value) => verify(value)"
      />
    </template>
    <script lang="ts">
      import InputOTP from "$lib/components/markas/InputOTP.svelte";
    
      let code = $state("");
    </script>
    
    <InputOTP
      bind:value={code}
      name="code"
      label="Verification code"
      oncomplete={(value) => verify(value)}
    />
    ---
    import InputOTP from "@/components/markas/InputOTP.astro";
    ---
    
    <InputOTP name="code" label="Verification code" />
    
    <script>
      document.addEventListener("markas-complete", (event) => {
        verify((event as CustomEvent<string>).detail);
      });
    </script>

    One small script per page handles every field. Like Blade, the fieldset dispatches markas-change and markas-complete events with the code in detail.

    import { Component, signal } from "@angular/core";
    import { InputOTP } from "./markas/input-otp.component";
    
    @Component({
      selector: "app-verify",
      imports: [InputOTP],
      template: `
        <markas-input-otp
          [(value)]="code"
          name="code"
          label="Verification code"
          (complete)="verify($event)"
        />
      `,
    })
    export class Verify {
      readonly code = signal("");
    
      verify(code: string) {
        // Send the code to your API.
      }
    }
    • The boxes accept digits only. Pasting a full code fills every box, even with spaces or dashes in it.
    • name goes to one hidden input that holds the joined code, so a normal form submit sends code=123456.
    • Extra attributes and class go to the <fieldset>. disabled disables every box.
    • Translate the box labels with digitLabel (default “Digit {index} of {length}”).

    Props

    NameTypeDefaultDescription
    lengthnumber6Number of digit boxes.
    labelstring-Group label, rendered as the fieldset legend.
    namestring-Name of the hidden input that holds the full code for form submit.
    value / defaultValue / v-model / bind:valuestring""The code. React accepts value with onValueChange or defaultValue; Vue uses v-model; Svelte bind:value; Angular value or [(value)]; Blade and Astro use value.
    onValueChange / markas-change(value: string) => void-Called on every change with the joined code (React). Blade and Astro dispatch a markas-change event.
    onComplete / @complete / markas-complete(value: string) => void-Called once every box is filled, with the full code. Blade and Astro dispatch a markas-complete event.
    oncomplete(value: string) => void-Svelte only. Called once every box is filled, with the full code.
    completeoutput<string>-Angular only. Emits the full code once every box is filled.
    digitLabelstringDigit {index} of {length}aria-label of each box. {index} and {length} are replaced with numbers.

    Accessibility

    • Boxes sit in a fieldset whose legend is the label prop, and each box has an aria-label such as "Digit 1 of 6".
    • Typing a digit moves focus to the next box; Backspace clears and moves back; Arrow Left, Arrow Right, Home, and End move between boxes.
    • Pasting a code fills all boxes. The first box uses autocomplete=one-time-code so phones can suggest SMS codes.
    • Boxes use inputmode=numeric for a number keyboard and accept digits only.

    Related components

    More form components with the same classes in every framework.