Top Related Projects
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Input Mask plugin
Format input text content when you are typing...
A jQuery Plugin to make masks on form fields and HTML elements.
VanillaMasker is a pure javascript mask input
jQuery Masked Input Plugin
Quick Overview
The ngx-mask
library is a powerful and flexible input mask solution for Angular applications. It provides a set of directives and pipes that allow developers to easily apply input masks to form fields, ensuring consistent and user-friendly data entry.
Pros
- Flexible Masking: The library supports a wide range of mask patterns, including numeric, alphanumeric, and custom masks, allowing developers to tailor the input experience to their specific needs.
- Reactive Forms Support:
ngx-mask
seamlessly integrates with Angular's Reactive Forms, making it easy to apply masks to form fields and handle validation. - Cross-browser Compatibility: The library is designed to work across a wide range of modern browsers, ensuring a consistent user experience.
- Active Development and Community: The project is actively maintained, with regular updates and a supportive community of contributors.
Cons
- Learning Curve: While the library is relatively easy to use, developers may need to spend some time understanding the syntax and configuration options to get the most out of it.
- Potential Performance Impact: Depending on the complexity of the masks used, the library may have a slight performance impact on the application, especially in large forms.
- Limited Customization: While the library provides a good set of built-in features, developers may find that they need to write custom code to achieve certain advanced masking requirements.
- Dependency on Angular: The
ngx-mask
library is specifically designed for Angular applications and may not be suitable for use in other JavaScript frameworks or libraries.
Code Examples
Applying a Numeric Mask
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { NgxMaskModule } from 'ngx-mask';
@Component({
selector: 'app-numeric-mask',
template: `
<form [formGroup]="form">
<input type="text" formControlName="phoneNumber" mask="(00) 00000-0000" />
</form>
`
})
export class NumericMaskComponent {
form = new FormGroup({
phoneNumber: new FormControl('')
});
}
This example demonstrates how to apply a numeric mask to a phone number input field using the mask
directive provided by the ngx-mask
library.
Applying a Custom Mask
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { NgxMaskModule } from 'ngx-mask';
@Component({
selector: 'app-custom-mask',
template: `
<form [formGroup]="form">
<input type="text" formControlName="creditCard" mask="0000 0000 0000 0000" />
</form>
`
})
export class CustomMaskComponent {
form = new FormGroup({
creditCard: new FormControl('')
});
}
This example demonstrates how to apply a custom mask to a credit card input field using the mask
directive.
Using the Mask Pipe
<p>Your credit card number: {{ creditCardNumber | mask: '0000 0000 0000 0000' }}</p>
In this example, the mask
pipe is used to format a credit card number for display purposes.
Getting Started
To get started with ngx-mask
, follow these steps:
- Install the library using npm or yarn:
npm install ngx-mask
- Import the
NgxMaskModule
in your Angular module:
import { NgxMaskModule } from 'ngx-mask';
@NgModule({
imports: [
// ...
NgxMaskModule.forRoot()
],
// ...
})
export class YourModule { }
- Use the
mask
directive in your template to apply a mask to an input field:
<input type="text" mask="(00) 00000-0000" />
- Optionally, you can configure the library by passing options to the
forRoot()
method:
NgxMaskModule.forRoot({
show
Competitor Comparisons
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Pros of text-mask
- More flexible and can be used with various JavaScript frameworks, not limited to Angular
- Supports a wider range of input types, including number inputs
- Has a more extensive set of built-in mask patterns
Cons of text-mask
- Less actively maintained, with fewer recent updates
- May require more setup and configuration for specific use cases
- Lacks some Angular-specific optimizations and integrations
Code Comparison
text-mask:
import createNumberMask from 'text-mask-addons/dist/createNumberMask'
const numberMask = createNumberMask({
prefix: '$',
suffix: '.00'
})
<input mask={numberMask} />
ngx-mask:
<input type="text" mask="separator.2" prefix="$" suffix=".00" [dropSpecialCharacters]="false">
Both libraries offer similar functionality for masking input fields, but ngx-mask provides a more concise and Angular-friendly syntax. text-mask requires importing additional functions and creating mask configurations, while ngx-mask allows for inline mask definitions within the template.
text-mask offers more flexibility for use across different frameworks, but ngx-mask provides a more streamlined experience for Angular developers. The choice between the two depends on the specific project requirements and the development environment.
Input Mask plugin
Pros of Inputmask
- More versatile, supporting various input types beyond just Angular
- Extensive documentation and examples
- Larger community and longer development history
Cons of Inputmask
- Steeper learning curve due to more complex API
- Potentially heavier in terms of file size and performance
Code Comparison
ngx-mask:
<input type="text" mask="000-000-0000" prefix="+1 ">
Inputmask:
Inputmask("999-999-9999", {
placeholder: "___-___-____",
clearIncomplete: true
}).mask(document.getElementById("phone"));
ngx-mask is more straightforward for Angular applications, with a simpler directive-based approach. Inputmask offers more flexibility but requires more setup.
Both libraries provide similar core functionality for input masking, but their implementation and usage differ. ngx-mask is tailored for Angular, making it easier to integrate into Angular projects. Inputmask, being framework-agnostic, offers broader applicability across different web technologies.
The choice between these libraries depends on the specific project requirements, development environment, and the level of customization needed for input masking.
Format input text content when you are typing...
Pros of cleave.js
- Framework-agnostic, can be used with any JavaScript project
- Supports both numeric and date/time formatting
- Lightweight and has no dependencies
Cons of cleave.js
- Less Angular-specific features compared to ngx-mask
- May require more manual configuration for complex use cases
- Not actively maintained (last commit over 2 years ago)
Code Comparison
cleave.js:
var cleave = new Cleave('.input-phone', {
phone: true,
phoneRegionCode: 'US'
});
ngx-mask:
<input mask="(000) 000-0000" prefix="+1 " [showMaskTyped]="true">
Both libraries provide input masking functionality, but ngx-mask is specifically designed for Angular applications, offering a more integrated experience with Angular's template syntax. cleave.js, on the other hand, provides a more general-purpose solution that can be used in various JavaScript environments.
While cleave.js offers flexibility across different frameworks, ngx-mask provides a more streamlined experience for Angular developers with its directive-based approach. The choice between the two depends on the specific project requirements and the development environment.
A jQuery Plugin to make masks on form fields and HTML elements.
Pros of jQuery-Mask-Plugin
- Lightweight and easy to integrate into existing jQuery projects
- Supports a wide range of browsers, including older versions
- Offers a simple and intuitive API for creating custom masks
Cons of jQuery-Mask-Plugin
- Requires jQuery as a dependency, which may increase overall bundle size
- Limited to jQuery-based projects, not suitable for modern frameworks
- Less actively maintained compared to ngx-mask
Code Comparison
jQuery-Mask-Plugin:
$('#date').mask('00/00/0000');
$('#phone').mask('(000) 000-0000');
$('#custom').mask('AA-00-SSSS');
ngx-mask:
<input mask="00/00/0000" [ngxMask]="'date'">
<input mask="(000) 000-0000" [ngxMask]="'phone'">
<input [mask]="'AA-00-SSSS'" [ngxMask]="'custom'">
Both libraries provide similar functionality for input masking, but ngx-mask is specifically designed for Angular applications, offering tighter integration with the framework. jQuery-Mask-Plugin is more versatile for general web development but may not be the best choice for modern, component-based applications.
VanillaMasker is a pure javascript mask input
Pros of vanilla-masker
- Framework-agnostic, can be used with any JavaScript project
- Lightweight and has minimal dependencies
- Supports both browser and Node.js environments
Cons of vanilla-masker
- Less actively maintained (last update was several years ago)
- Fewer built-in mask patterns compared to ngx-mask
- Limited documentation and examples
Code Comparison
ngx-mask:
<input type="text" mask="(000) 000-0000" prefix="+1 ">
vanilla-masker:
VMasker(document.querySelector("input")).maskPattern("(999) 999-9999");
Summary
vanilla-masker is a lightweight, framework-agnostic masking library that works in both browser and Node.js environments. It offers flexibility but has fewer built-in patterns and less active maintenance compared to ngx-mask. ngx-mask, on the other hand, is specifically designed for Angular applications and provides more extensive features and regular updates. The choice between the two depends on the project requirements, framework preferences, and the need for ongoing support and updates.
jQuery Masked Input Plugin
Pros of jquery.maskedinput
- Lightweight and easy to integrate into existing jQuery projects
- Supports a wide range of browsers, including older versions
- Simple and intuitive syntax for defining masks
Cons of jquery.maskedinput
- Requires jQuery as a dependency, which may not be ideal for modern projects
- Less actively maintained compared to ngx-mask
- Limited to input elements, while ngx-mask can be applied to various Angular components
Code Comparison
jquery.maskedinput:
$("#phone").mask("(999) 999-9999");
$("#date").mask("99/99/9999");
ngx-mask:
<input mask="(000) 000-0000" type="text">
<input mask="00/00/0000" type="text">
Summary
jquery.maskedinput is a solid choice for jQuery-based projects, offering simplicity and broad browser support. However, ngx-mask is more suitable for modern Angular applications, providing better integration with the framework and more active development. The syntax for both libraries is relatively straightforward, with ngx-mask using Angular's template-driven approach and jquery.maskedinput utilizing jQuery selectors.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
NGX-MASK
A powerful Angular directive for input masking with customizable patterns
Created with â¤ï¸ by
Table of Contents
Features
NGX-MASK is a feature-rich input mask directive for Angular applications that provides:
ð¯ Masking Patterns⢠Custom patterns & expressions ⢠Multiple mask patterns (|) ⢠Built-in common patterns ⢠Prefix & suffix support |
ð¢ Number Formatting⢠Thousand separators ⢠Decimal markers ⢠Negative numbers ⢠Leading zeros |
⡠Input Control⢠Real-time validation ⢠Clear on non-match ⢠Show/hide mask typing ⢠Keep character positions |
ð Date & Time⢠Leading zero handling ⢠AM/PM support ⢠Custom separators ⢠Multiple formats |
ð ï¸ Customization⢠Custom placeholders ⢠Special characters ⢠Transform functions ⢠Custom validation |
ð Form Integration⢠Reactive Forms ⢠ControlValueAccessor ⢠Built-in validation ⢠Standalone support |
Demo
Check out our live documentation and examples
Installation
# 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 bun
Package Manager Note: You can use either npm or bun based on your preference. Both package managers will work equally well with ngx-mask.
Version Compatibility
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.
Quick Start
For Angular 15+ (Standalone)
Application-wide Setup with Default Config
bootstrapApplication(AppComponent, { providers: [provideEnvironmentNgxMask()] }).catch((err) =>
console.error(err)
);
With Custom Configuration
import { NgxMaskConfig } from 'ngx-mask';
const maskConfig: Partial<NgxMaskConfig> = { validation: false };
bootstrapApplication(AppComponent, { providers: [provideEnvironmentNgxMask(maskConfig)] }).catch(
(err) => console.error(err)
);
Feature-level Configuration
@Component({
selector: 'my-feature',
standalone: true,
imports: [NgxMaskDirective],
providers: [provideNgxMask()],
})
export class MyFeatureComponent {}
For Angular < 15 (NgModule)
Application-wide Setup with Default Config
import { NgxMaskModule } from 'ngx-mask';
@NgModule({ imports: [NgxMaskModule.forRoot()] })
export class AppModule {}
With Custom Configuration
import { NgxMaskModule, NgxMaskConfig } from 'ngx-mask';
const maskConfig: Partial<NgxMaskConfig> = { validation: false };
@NgModule({ imports: [NgxMaskModule.forRoot(maskConfig)] })
export class AppModule {}
Feature-level Configuration
import { NgxMaskModule } from 'ngx-mask';
@NgModule({ imports: [NgxMaskModule.forChild()] })
export class FeatureModule {}
Related Projects
Check out other projects by JSDaddy:
Contributing
We welcome contributions! Please read our contributing guidelines to learn about our development process and how you can propose bugfixes and improvements.
Top Related Projects
Input mask for React, Angular, Ember, Vue, & plain JavaScript
Input Mask plugin
Format input text content when you are typing...
A jQuery Plugin to make masks on form fields and HTML elements.
VanillaMasker is a pure javascript mask input
jQuery Masked Input Plugin
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot