npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-otp-input

v2.0.5

Published

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

Downloads

171,252

Readme

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 [email protected]

For Angular 11 and below:

npm install --save [email protected]

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

| Name | Type | Required | Default | Description | |-----------------|----------|----------|---------|-----------------------------------------------------------------------------| | disabled | boolean | No | false | Disables all inputs when set to true. | | onOtpChange | Output | No | -- | Emits the OTP value on change. Not required if using formControl. | | onBlur | Output | No | -- | Triggered when focus is lost from the component. | | setValue | function | No | -- | Allows programmatic setting of the component value. | | config | object | Yes | { length: 4 } | Configuration object for customization (see Config Options below). |


Config Options

| Name | Type | Required | Default | Description | |-------------------|---------|----------|------------|----------------------------------------------------------------------------------------------| | allowNumbersOnly | boolean | No | false | Restricts input to numeric values only. | | disableAutoFocus | boolean | No | false | Prevents automatic focus on load or when setting the value. | | containerClass | string | No | -- | Adds a custom CSS class to the container element. | | containerStyles | object | No | -- | Customizes the container styles. Check NgStyle. | | inputStyles | object | No | -- | Customizes the styles of individual inputs. | | inputClass | string | No | -- | Adds a custom CSS class to each input box. | | isPasswordInput | boolean | No | false | Masks input as password fields. | | length | number | Yes | 4 | Sets the number of OTP inputs to render. | | letterCase | string | No | -- | Converts input to Upper or Lower case. | | placeholder | string | No | -- | Sets a placeholder for each input box. | | separator | char | No | -- | Inserts a separator character between input boxes. | | showError | boolean | No | false | Highlights 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.