Skip to content

🎭 @ngneat/input-mask is an angular library that creates an input mask

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
license.gif
Notifications You must be signed in to change notification settings

Blafasel3/input-mask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

51 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@ngneat/input-mask


npm (scoped) MIT commitizen PRs styled with prettier All Contributors ngneat-lib spectator semantic-release

@ngneat/input-mask is an angular library that creates an input mask. Behind the scene, it uses inputmask.

Features

  • πŸ”‘ Support for form validation
  • 🎭 Wrapper function to easily create input-masks
  • πŸ” Helps you to convert final values to desired format
  • ☝️ Single directive to handle everything
  • πŸ›  All the configurations of inputmask provided

Installation

You can install it through Angular CLI

ng add @ngneat/input-mask

or with npm

npm install @ngneat/input-mask inputmask@5
npm install -D @types/inputmask@5

When you install using npm or yarn, you will also need to import InputMaskModule in your app.module:

import { InputMaskModule } from '@ngneat/input-mask';

@NgModule({
  imports: [InputMaskModule],
})
class AppModule {}

Usage examples

1. Date

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
import { createMask } from '@ngneat/input-mask';

@Component({
  selector: 'app-root',
  template: `
    <input [inputMask]="dateInputMask" [formControl]="dateFC" placeholder="dd/mm/yyyy">
  `,
})
export class AppComponent {
  dateInputMask = createMask<Date>({
    alias: 'datetime',
    inputFormat: 'dd/mm/yyyy',
    parser: (value: string) => {
      const values = value.split('/');
      const year = +values[2];
      const month = +values[1] - 1;
      const date = +values[0];
      return new Date(year, month, date);
    },
  });

  dateFC = new FormControl('');
}

2. IP Address

@Component({
  template: `
    <input [inputMask]="ipAddressMask" [formControl]="ipFC" placeholder="_._._._">
  `,
})
export class AppComponent {
  ipAddressMask = createMask({ alias: 'ip' });
  ipFC = new FormControl('');
}

3. Currency

@Component({
  template: `
    <input [inputMask]="currencyInputMask" [formControl]="currencyFC" placeholder="$ 0.00">
  `,
})
export class AppComponent {
  currencyInputMask = createMask({
    alias: 'numeric',
    groupSeparator: ',',
    digits: 2,
    digitsOptional: false,
    prefix: '$ ',
    placeholder: '0',
  });
  currencyFC = new FormControl('');
}

4. License Plate

@Component({
  template: `
    <input [inputMask]="licenseInputMask" [formControl]="licenseFC" placeholder="___-___">
  `,
})
export class AppComponent {
  licenseInputMask = createMask('[9-]AAA-999');
  licenseFC = new FormControl('');
}

5. Email

@Component({
  template: `
    <input [inputMask]="emailInputMask" [formControl]="emailFC" placeholder="_@_._">
  `,
})
export class AppComponent {
  emailInputMask = createMask({ alias: 'email'});
  emailFC = new FormControl('');
}

More examples

All examples are available on stackblitz.

You can create any type of input-mask which is supported by InputMask plugin.

Validation

When [inputMask] is used with [formControl], it adds validation out-of-the box. The validation works based on isValid function.

If the validation fails, the form-control will have below error:

{ inputMask: false }

createMask wrapper function

This library uses inputmask plugin to handle mask related tasks. So, you can use all the options available there.

The recommended way to create an inputmask is to use the createMask function provided with this library.

parser function

Apart from inputmask options, we have added one more option called parser. This basically helps you to keep the value of form-control in pre-defined format, without updating UI.

For example, you want your users to enter date in input[type=text] with dd/mm/yyyy format and you want to store a Date value in the form-control:

@Component({
  template: `
    <input [inputMask]="dateInputMask" [formControl]="dateFC" placeholder="dd/mm/yyyy">
  `,
})
export class AppComponent {
  dateInputMask = createMask<Date>({
    alias: 'datetime',
    inputFormat: 'dd/mm/yyyy',
    parser: (value: string) => {
      const values = value.split('/');
      const year = +values[2];
      const month = +values[1] - 1;
      const date = +values[0];
      return new Date(year, month, date);
    },
  });

  dateFC = new FormControl('');
}

In above example, whenver you try to access dateFC.value, it won't be the string which user entered, but rather a Date created based on the parser function.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Dharmen Shah

πŸ’» πŸ–‹ πŸ“– πŸ’‘ 🚧 πŸ“¦

Netanel Basal

πŸ› πŸ’Ό πŸ€” πŸ§‘β€πŸ« πŸ“† πŸ‘€

Robin Herbots

πŸ€”

This project follows the all-contributors specification. Contributions of any kind welcome!

About

🎭 @ngneat/input-mask is an angular library that creates an input mask

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
license.gif

Stars

Watchers

Forks

Packages

No packages published