input otp

Accessible one-time password component with copy-paste functionality.

PreviousNext
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';

@Component({
  selector: 'z-demo-input-otp-preview',
  imports: [ZardInputOtpImports, FormsModule],
  template: `
    <z-input-otp [zMaxLength]="6" [(ngModel)]="value">
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="0" />
        <z-input-otp-slot [zIndex]="1" />
        <z-input-otp-slot [zIndex]="2" />
        <z-input-otp-slot [zIndex]="3" />
        <z-input-otp-slot [zIndex]="4" />
        <z-input-otp-slot [zIndex]="5" />
      </z-input-otp-group>
    </z-input-otp>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpPreviewComponent {
  value = '123456';
}

Installation

Copy
npx zard-cli@latest add input-otp

Usage

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';
Copy
<z-input-otp [zMaxLength]="6">
  <z-input-otp-group>
    <z-input-otp-slot [zIndex]="0" />
    <z-input-otp-slot [zIndex]="1" />
    <z-input-otp-slot [zIndex]="2" />
  </z-input-otp-group>
  <z-input-otp-separator />
  <z-input-otp-group>
    <z-input-otp-slot [zIndex]="3" />
    <z-input-otp-slot [zIndex]="4" />
    <z-input-otp-slot [zIndex]="5" />
  </z-input-otp-group>
</z-input-otp>
Copy

Examples

Pattern

Use zPattern to restrict the characters a slot accepts.
import { REGEXP_ONLY_DIGITS } from '@/shared/components/input-otp/input-otp.utils';

<z-input-otp [zMaxLength]="6" [zPattern]="REGEXP_ONLY_DIGITS">
  ...
</z-input-otp>
Copy
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';
import { REGEXP_ONLY_DIGITS } from '@/shared/components/input-otp/input-otp.utils';

@Component({
  selector: 'z-demo-input-otp-pattern',
  imports: [ZardInputOtpImports, ZardFieldImports],
  template: `
    <div z-field class="w-fit">
      <label z-field-label for="digits-only">Digits Only</label>
      <z-input-otp id="digits-only" [zMaxLength]="6" [zPattern]="REGEXP_ONLY_DIGITS">
        <z-input-otp-group>
          <z-input-otp-slot [zIndex]="0" />
          <z-input-otp-slot [zIndex]="1" />
          <z-input-otp-slot [zIndex]="2" />
          <z-input-otp-slot [zIndex]="3" />
          <z-input-otp-slot [zIndex]="4" />
          <z-input-otp-slot [zIndex]="5" />
        </z-input-otp-group>
      </z-input-otp>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpPatternComponent {
  readonly REGEXP_ONLY_DIGITS = REGEXP_ONLY_DIGITS;
}

Separator

Use z-input-otp-separator between groups to split the slots into smaller blocks.
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';

@Component({
  selector: 'z-demo-input-otp-separator',
  imports: [ZardInputOtpImports],
  template: `
    <z-input-otp [zMaxLength]="6">
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="0" />
        <z-input-otp-slot [zIndex]="1" />
      </z-input-otp-group>
      <z-input-otp-separator />
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="2" />
        <z-input-otp-slot [zIndex]="3" />
      </z-input-otp-group>
      <z-input-otp-separator />
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="4" />
        <z-input-otp-slot [zIndex]="5" />
      </z-input-otp-group>
    </z-input-otp>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpSeparatorComponent {}

Controlled

Bind the value with ngModel to read and write it from the parent component.
Enter your one-time password.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';

@Component({
  selector: 'z-demo-input-otp-controlled',
  imports: [ZardInputOtpImports, FormsModule],
  template: `
    <div class="space-y-2">
      <z-input-otp [zMaxLength]="6" [(ngModel)]="value">
        <z-input-otp-group>
          <z-input-otp-slot [zIndex]="0" />
          <z-input-otp-slot [zIndex]="1" />
          <z-input-otp-slot [zIndex]="2" />
          <z-input-otp-slot [zIndex]="3" />
          <z-input-otp-slot [zIndex]="4" />
          <z-input-otp-slot [zIndex]="5" />
        </z-input-otp-group>
      </z-input-otp>
      <div class="text-center text-sm">
        @if (value === '') {
          Enter your one-time password.
        } @else {
          You entered: {{ value }}
        }
      </div>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpControlledComponent {
  value = '';
}

Disabled

Use the disabled binding to disable every slot at once.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';

@Component({
  selector: 'z-demo-input-otp-disabled',
  imports: [ZardInputOtpImports, FormsModule],
  template: `
    <z-input-otp id="disabled" [zMaxLength]="6" [(ngModel)]="value" [disabled]="true">
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="0" />
        <z-input-otp-slot [zIndex]="1" />
        <z-input-otp-slot [zIndex]="2" />
      </z-input-otp-group>
      <z-input-otp-separator />
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="3" />
        <z-input-otp-slot [zIndex]="4" />
        <z-input-otp-slot [zIndex]="5" />
      </z-input-otp-group>
    </z-input-otp>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpDisabledComponent {
  value = '123456';
}

Invalid

Use zInvalid on a slot — or on the whole z-input-otp — to mark the value as invalid.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';

@Component({
  selector: 'z-demo-input-otp-invalid',
  imports: [ZardInputOtpImports, FormsModule],
  template: `
    <z-input-otp [zMaxLength]="6" [(ngModel)]="value">
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="0" zInvalid />
        <z-input-otp-slot [zIndex]="1" zInvalid />
      </z-input-otp-group>
      <z-input-otp-separator />
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="2" zInvalid />
        <z-input-otp-slot [zIndex]="3" zInvalid />
      </z-input-otp-group>
      <z-input-otp-separator />
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="4" zInvalid />
        <z-input-otp-slot [zIndex]="5" zInvalid />
      </z-input-otp-group>
    </z-input-otp>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpInvalidComponent {
  value = '000000';
}

Four Digits

Use zMaxLength to change how many slots the input holds.
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';
import { REGEXP_ONLY_DIGITS } from '@/shared/components/input-otp/input-otp.utils';

@Component({
  selector: 'z-demo-input-otp-four-digits',
  imports: [ZardInputOtpImports],
  template: `
    <z-input-otp [zMaxLength]="4" [zPattern]="REGEXP_ONLY_DIGITS">
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="0" />
        <z-input-otp-slot [zIndex]="1" />
        <z-input-otp-slot [zIndex]="2" />
        <z-input-otp-slot [zIndex]="3" />
      </z-input-otp-group>
    </z-input-otp>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpFourDigitsComponent {
  readonly REGEXP_ONLY_DIGITS = REGEXP_ONLY_DIGITS;
}

Alphanumeric

Pair REGEXP_ONLY_DIGITS_AND_CHARS with [zIntegerOnly]="false" to accept letters too.
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';
import { REGEXP_ONLY_DIGITS_AND_CHARS } from '@/shared/components/input-otp/input-otp.utils';

@Component({
  selector: 'z-demo-input-otp-alphanumeric',
  imports: [ZardInputOtpImports],
  template: `
    <z-input-otp [zMaxLength]="6" [zPattern]="REGEXP_ONLY_DIGITS_AND_CHARS" [zIntegerOnly]="false">
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="0" />
        <z-input-otp-slot [zIndex]="1" />
        <z-input-otp-slot [zIndex]="2" />
      </z-input-otp-group>
      <z-input-otp-separator />
      <z-input-otp-group>
        <z-input-otp-slot [zIndex]="3" />
        <z-input-otp-slot [zIndex]="4" />
        <z-input-otp-slot [zIndex]="5" />
      </z-input-otp-group>
    </z-input-otp>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpAlphanumericComponent {
  readonly REGEXP_ONLY_DIGITS_AND_CHARS = REGEXP_ONLY_DIGITS_AND_CHARS;
}

Form

Use formControlName to bind the OTP to a reactive form.
Verify your login Enter the verification code we sent to your email address: m@example.com
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideRefreshCw } from '@ng-icons/lucide';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';

const SLOT_CLASSES =
  '[&_[data-slot=input-otp-slot]>input]:h-12 [&_[data-slot=input-otp-slot]>input]:w-11 [&_[data-slot=input-otp-slot]>input]:text-xl';

@Component({
  selector: 'z-demo-input-otp-form',
  imports: [ZardInputOtpImports, ZardFieldImports, ZardCardImports, ZardButtonComponent, ReactiveFormsModule, NgIcon],
  template: `
    <form [formGroup]="form" (ngSubmit)="onSubmit()">
      @let codeControl = form.controls.code;
      @let codeInvalid = codeControl.invalid && codeControl.touched;
      <z-card class="mx-auto max-w-md">
        <div z-card-header>
          <z-card-title zTitle="Verify your login" />
          <z-card-description [zDescription]="description" />
          <ng-template #description>
            Enter the verification code we sent to your email address:
            <span class="font-medium">m&#64;example.com</span>
          </ng-template>
        </div>

        <div z-card-content>
          <div z-field [attr.data-invalid]="codeInvalid || null">
            <div class="flex items-center justify-between">
              <label z-field-label for="otp-verification">Verification code</label>
              <button z-button type="button" zType="outline" zSize="xs">
                <ng-icon name="lucideRefreshCw" />
                Resend Code
              </button>
            </div>
            <z-input-otp id="otp-verification" [zMaxLength]="6" formControlName="code" [zInvalid]="codeInvalid">
              <z-input-otp-group [class]="slotClasses">
                <z-input-otp-slot [zIndex]="0" />
                <z-input-otp-slot [zIndex]="1" />
                <z-input-otp-slot [zIndex]="2" />
              </z-input-otp-group>
              <z-input-otp-separator class="mx-2" />
              <z-input-otp-group [class]="slotClasses">
                <z-input-otp-slot [zIndex]="3" />
                <z-input-otp-slot [zIndex]="4" />
                <z-input-otp-slot [zIndex]="5" />
              </z-input-otp-group>
            </z-input-otp>
            @if (codeInvalid) {
              <z-field-error>
                @if (codeControl.hasError('required')) {
                  Enter the verification code.
                } @else if (codeControl.hasError('minlength')) {
                  The code must be 6 digits.
                }
              </z-field-error>
            } @else {
              <p z-field-description>
                <a href="#">I no longer have access to this email address.</a>
              </p>
            }
          </div>
        </div>

        <div z-card-footer>
          <div z-field>
            <button z-button type="submit" class="w-full" [zDisabled]="form.invalid">Verify</button>
            <div class="text-muted-foreground text-sm">
              Having trouble signing in?
              <a href="#" class="hover:text-primary underline underline-offset-4 transition-colors">Contact support</a>
            </div>
          </div>
        </div>
      </z-card>
    </form>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideRefreshCw })],
})
export class ZardDemoInputOtpFormComponent {
  readonly slotClasses = SLOT_CLASSES;

  readonly form = new FormGroup({
    code: new FormControl('', [Validators.required, Validators.minLength(6)]),
  });

  onSubmit(): void {
    this.form.markAllAsTouched();

    if (this.form.valid) {
      console.log('Verification code:', this.form.value.code);
    }
  }
}

RTL

To enable RTL support in zard/ui, see the RTL configuration guide.
رمز التحقق

أدخل الرمز المكون من 6 أرقام الذي أرسلناه إلى هاتفك.

// You will notice this example is rendered inside a wrapper that sets the dir and data-lang attributes, and
// reads its text from a translations helper. This is because this site is not RTL by default.
// In your application you won't need either: set dir="rtl" on <html> (or use the CDK Dir directive on a subtree)
// and write the text of your own language. See the RTL guide: https://zardui.com/docs/rtl

import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardInputOtpImports } from '@/shared/components/input-otp/input-otp.imports';
import { injectRtlTranslation, type ZardRtlTranslations } from '@/shared/demo-utils/rtl-translation';

const translations: ZardRtlTranslations<{ label: string; description: string }> = {
  en: {
    dir: 'ltr',
    values: {
      label: 'Verification code',
      description: 'Enter the 6-digit code we sent to your phone.',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      label: 'رمز التحقق',
      description: 'أدخل الرمز المكون من 6 أرقام الذي أرسلناه إلى هاتفك.',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      label: 'קוד אימות',
      description: 'הזינו את הקוד בן 6 הספרות ששלחנו לטלפון שלכם.',
    },
  },
};

@Component({
  selector: 'z-demo-input-otp-rtl',
  imports: [...ZardInputOtpImports],
  template: `
    <div class="flex flex-col items-start gap-2">
      <span class="text-sm font-medium">{{ i18n.t().label }}</span>
      <z-input-otp [zMaxLength]="6">
        <z-input-otp-group>
          <z-input-otp-slot [zIndex]="0" />
          <z-input-otp-slot [zIndex]="1" />
          <z-input-otp-slot [zIndex]="2" />
        </z-input-otp-group>
        <z-input-otp-separator />
        <z-input-otp-group>
          <z-input-otp-slot [zIndex]="3" />
          <z-input-otp-slot [zIndex]="4" />
          <z-input-otp-slot [zIndex]="5" />
        </z-input-otp-group>
      </z-input-otp>
      <p class="text-muted-foreground text-sm">{{ i18n.t().description }}</p>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoInputOtpRtlComponent {
  protected readonly i18n = injectRtlTranslation(translations);
}

API Reference

z-input-otp

One-time code input; slots and arrow keys follow the text direction.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
''
[zMaxLength]

Max characters; falls back to the slot count, or 6

Type
number
Default
—
[zPattern]

Per-character regex; presets in input-otp.utils

Type
string
Default
'[0-9]'
[zReadonly]

Makes every slot readonly

Type
boolean
Default
false
[zIntegerOnly]

Numeric keyboard, digits only

Type
boolean
Default
true
[zInvalid]

Marks every slot as invalid

Type
boolean
Default
false
[zSize]

Size of the slots and separators

Type
'sm' | 'default' | 'lg'
Default
'default'
(zValueChange)

Emitted when the value changes

Type
EventEmitter<string>
Default
—
(zComplete)

Emitted when every slot is filled

Type
EventEmitter<string>
Default
—

z-input-otp-signal

z-input-otp for signal forms ([formField]).

Prop
[value]

Current value

Type
string
Default
''
[(value)]

Current value

Type
string
Default
''
[disabled]

Disabled state

Type
boolean
Default
false
[(disabled)]

Disabled state

Type
boolean
Default
false

z-input-otp-slot

A single character slot.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
''
[zIndex]

Zero-based slot position

Type
number
Default
required
[zInvalid]

Marks this slot as invalid

Type
boolean
Default
false

z-input-otp-group

Joins slots into one connected block.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
''

z-input-otp-separator

Separator between slot groups.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
''
github iconwhatsapp icondiscord iconX icon

Made with ❤ in Brazil. Open source and available on GitHub .