2.0.7 • Published 3 months ago
ng-otp-input v2.0.7
A fully customizable, one-time password (OTP) input component for the web built with Angular.
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
andngModel
are supported from v2.0.5 onwards). - Deprecated the custom
formCtrl
input property. UseformControl
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
orngModel
(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 theFormControl
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
Contributing
Show your support by starring the repo! Feel free to open issues or contribute via pull requests.
2.0.3
5 months ago
2.0.2
5 months ago
2.0.5
5 months ago
2.0.4
5 months ago
2.0.7
3 months ago
2.0.6
4 months ago
2.0.1
5 months ago
2.0.0
5 months ago
1.9.4
5 months ago
1.9.3
2 years ago
1.9.1
2 years ago
1.8.6
2 years ago
1.9.2
2 years ago
1.8.2
3 years ago
1.8.5
3 years ago
1.8.3
3 years ago
1.8.1
4 years ago
1.8.0
4 years ago
1.7.9
4 years ago
1.7.8
4 years ago
1.7.3
4 years ago
1.7.2
4 years ago
1.7.7
4 years ago
1.7.6
4 years ago
1.7.5
4 years ago
1.7.4
4 years ago
1.7.1
5 years ago
1.7.0
5 years ago
1.6.2
5 years ago
1.6.1
5 years ago
1.6.0
6 years ago
1.5.1
6 years ago
1.5.0
6 years ago
1.4.2
6 years ago
1.4.1
6 years ago
1.4.0
6 years ago
1.3.3
6 years ago
1.3.2
6 years ago
1.3.1
6 years ago
1.3.0
6 years ago
1.2.1
6 years ago
1.2.0
6 years ago
1.1.0
6 years ago
1.0.0
6 years ago