A powerful Angular directive for input masking with customizable patterns
ngx-mask is the input-masking library built for modern Angular. One standalone directive (NgxMaskDirective) and pipe (NgxMaskPipe) cover all three form models β Reactive Forms, template-driven, and the new Signal Forms β through a first-class ControlValueAccessor, and run cleanly in zoneless and SSR applications. You get production-ready masks out of the box: dates and times with validity checking, numbers with thousand separators and decimal precision, IP, CPF/CNPJ, secure/hidden input, plus fully custom patterns with prefixes, suffixes and multi-mask expressions. No runtime dependencies beyond Angular, ~15 KB gzipped β provide it once, bind mask, done.
NGX-MASK is a feature-rich input mask directive for Angular applications that provides:
|
β’ Custom patterns & expressions β’ Multiple mask patterns (|) β’ Built-in common patterns β’ Prefix & suffix support |
β’ Thousand separators β’ Decimal markers β’ Negative numbers β’ Leading zeros |
β’ Real-time validation β’ Clear on non-match β’ Show/hide mask typing β’ Keep character positions |
|
β’ Leading zero handling β’ AM/PM support β’ Custom separators β’ Multiple formats |
β’ Custom placeholders β’ Special characters β’ Transform functions β’ Custom validation |
β’ Reactive Forms β’ ControlValueAccessor β’ Built-in validation β’ Standalone support |
Check out our live documentation and examples
# For Angular 17 and above
$ npm install ngx-mask # Using npm
$ bun add ngx-mask # Using bun
# For specific Angular versions:
# Angular 16.x.x
$ npm install ngx-mask@16.4.2 # Using npm
$ bun add ngx-mask@16.4.2 # Using bun
# Angular 15.x.x
$ npm install ngx-mask@15.2.3 # Using npm
$ bun add ngx-mask@15.2.3 # Using bun
# Angular 14.x.x
$ npm install ngx-mask@14.3.3 # Using npm
$ bun add ngx-mask@14.3.3 # Using bun
# Angular 13.x.x or 12.x.x
$ npm install ngx-mask@13.2.2 # Using npm
$ bun add ngx-mask@13.2.2 # Using bunPackage Manager Note: You can use either npm or bun based on your preference. Both package managers will work equally well with ngx-mask.
NGX-MASK follows Angular's official support policy, supporting Active and LTS versions. Currently supported:
- Angular 17 and newer (latest features and updates)
- For older Angular versions, use the corresponding NGX-MASK version as specified above
Note: Versions for Angular older than v17 will not receive new features or updates.
ngx-mask ships as a standalone directive (NgxMaskDirective) and pipe (NgxMaskPipe) β there is no NgxMaskModule in current versions. Configuration is registered through one of two provider functions:
provideEnvironmentNgxMask(config?)β application-wide config. Use it once inbootstrapApplication/app.config.ts(or a rootNgModule'sproviders).provideNgxMask(config?)β injector-level config. Use it in a component's or feature'sprovidersto configure/override the options for that subtree only.
Directive inputs (e.g. [thousandSeparator]) always override any provider config. See USAGE.md for the full decision guide, examples, and common pitfalls.
bootstrapApplication(AppComponent, { providers: [provideEnvironmentNgxMask()] }).catch((err) =>
console.error(err)
);import { NgxMaskConfig } from 'ngx-mask';
const maskConfig: Partial<NgxMaskConfig> = { validation: false };
bootstrapApplication(AppComponent, { providers: [provideEnvironmentNgxMask(maskConfig)] }).catch(
(err) => console.error(err)
);@Component({
selector: 'my-feature',
standalone: true,
imports: [NgxMaskDirective],
providers: [provideNgxMask()],
})
export class MyFeatureComponent {}Module-based apps import the standalone directive/pipe into imports and register the provider function:
import { NgxMaskDirective, NgxMaskPipe, provideEnvironmentNgxMask } from 'ngx-mask';
@NgModule({
imports: [NgxMaskDirective, NgxMaskPipe],
exports: [NgxMaskDirective, NgxMaskPipe],
providers: [provideEnvironmentNgxMask()],
})
export class AppModule {}NgxMaskModule.forRoot() / forChild() only exist in ngx-mask 14.x and older (Angular < 15):
// Before (ngx-mask <= 14)
@NgModule({ imports: [NgxMaskModule.forRoot(maskConfig)] })
export class AppModule {}
// After (current ngx-mask)
@NgModule({
imports: [NgxMaskDirective],
providers: [provideEnvironmentNgxMask(maskConfig)],
})
export class AppModule {}We welcome contributions! Please read our contributing guidelines to learn about our development process and how you can propose bugfixes and improvements.
Maintained by Igor Nepipenko