New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ng-otp-input

Package Overview
Dependencies
Maintainers
0
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ng-otp-input

A fully customizable, one-time password input component for the web built with Angular.

  • 2.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42K
decreased by-4.65%
Maintainers
0
Weekly downloads
 
Created
Source

npm version npm npm

A fully customizable, one-time password (OTP) input component for the web built with Angular.

GIPHY

Demo | Edit on StackBlitz


Installation

For Angular 16 and above:
npm install --save ng-otp-input
For Angular 12 to 15:
npm install --save ng-otp-input@1.9.3
For Angular 11 and below:
npm install --save ng-otp-input@1.8.1

Major Changes in v2

  • Requires Angular 16 or above.
  • Emits null instead of an empty string if no value is supplied.
  • Fully supports Reactive Forms (formControl and ngModel are supported from v2.0.5 onwards).
  • Deprecated the custom formCtrl input property. Use formControl instead.

Usage

Import the Module

For Modules:

import { NgOtpInputModule } from 'ng-otp-input';

@NgModule({
  imports: [
    ...otherModules,
    NgOtpInputModule,
  ],
})
export class AppModule {}

For Standalone Components:

import { NgOtpInputComponent } from 'ng-otp-input';

@Component({
  standalone: true,
  imports: [NgOtpInputComponent],
})
export class YourComponent {}

Add the Component to Your Template

<!-- With Event Binding -->
<ng-otp-input (onInputChange)="onOtpChange($event)" [config]="{ length: 5 }"></ng-otp-input>

<!-- Using Reactive FormControl (v2.0.5 and above) -->
<ng-otp-input [formControl]="yourFormControl" [config]="{ length: 5 }"></ng-otp-input>

API Reference

Component Inputs/Outputs

NameTypeRequiredDefaultDescription
disabledbooleanNofalseDisables all inputs when set to true.
onOtpChangeOutputNo--Emits the OTP value on change. Not required if using formControl.
onBlurOutputNo--Triggered when focus is lost from the component.
setValuefunctionNo--Allows programmatic setting of the component value.
configobjectYes{ length: 4 }Configuration object for customization (see Config Options below).

Config Options

NameTypeRequiredDefaultDescription
allowNumbersOnlybooleanNofalseRestricts input to numeric values only.
disableAutoFocusbooleanNofalsePrevents automatic focus on load or when setting the value.
containerClassstringNo--Adds a custom CSS class to the container element.
containerStylesobjectNo--Customizes the container styles. Check NgStyle.
inputStylesobjectNo--Customizes the styles of individual inputs.
inputClassstringNo--Adds a custom CSS class to each input box.
isPasswordInputbooleanNofalseMasks input as password fields.
lengthnumberYes4Sets the number of OTP inputs to render.
letterCasestringNo--Converts input to Upper or Lower case.
placeholderstringNo--Sets a placeholder for each input box.
separatorcharNo--Inserts a separator character between input boxes.
showErrorbooleanNofalseHighlights inputs with red borders if formControl is invalid, dirty, or touched.

Advanced Features

Updating Component Value

  • Use formControl or ngModel (v2.0.5 and above) for updates.
  • For earlier versions, use setValue:
@ViewChild(NgOtpInputComponent, { static: false }) ngOtpInput: NgOtpInputComponent;

updateOtpValue() {
  this.ngOtpInput.setValue('12345'); // Replace with your OTP value.
}

Disabling Inputs

  • Use the disabled property or set the FormControl to a disabled state (v2.0.5+).
  • For earlier versions, use disable method:
this.ngOtpInput.otpForm.disable();

Focus on a Specific Input

const eleId = this.ngOtpInput.getBoxId(0);
this.ngOtpInput.focusTo(eleId);

License

License: MIT


Contributing

Show your support by starring the repo! Feel free to open issues or contribute via pull requests.

Keywords

FAQs

Package last updated on 10 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc