项目作者: benhurott

项目描述 :
A js mask simple like killing zombies =).
高级语言: JavaScript
项目地址: git://github.com/benhurott/tinymask.git
创建时间: 2017-05-23T12:39:11Z
项目社区:https://github.com/benhurott/tinymask

开源协议:

下载


tinymask

A js mask simple like killing zombies =).

Usage

Install it from npm using npm install --save tinymask

  1. var TinyMask = require('tinymask')
  2. var maskInstance = TinyMask('9999-9999');
  3. var result = maskInstance.mask('12345678');
  4. console.log(result); //1234-5678

By default, we use this translation:

  • 9 -> Accept numbers
  • A -> Accept alpha
  • S -> Accept alphanumerics
  • * -> Accept all

Options

You can pass options for the mask. We use the defaults:

  1. var maskInstance = TinyMask('9999-9999', {
  2. translation: {
  3. '9': function (val) {
  4. return val.replace(/[^0-9]+/g, '');
  5. },
  6. 'A': function (val) {
  7. return val.replace(/[^a-zA-Z]+/g, '');
  8. },
  9. 'S': function (val) {
  10. return val.replace(/[^a-zA-Z0-9]+/g, '');
  11. },
  12. '*': function (val) {
  13. return val;
  14. }
  15. },
  16. invalidValues: [null, undefined, '']
  17. });

translation (Object | optional)

You can add or override any of the translation keys. Ex:

  1. var maskInstance = TinyMask('9999-9999', {
  2. translation: {
  3. // in this case, we add new # translation that allow
  4. // blank spaces.
  5. '#': function (val) {
  6. if (val === ' ') {
  7. return val;
  8. }
  9. return null;
  10. },
  11. // here we override the * translation to accept only
  12. // some characters instead all characters.
  13. '*': function (val) {
  14. if (['*', '!', '?'].indexOf(val) >= 0) {
  15. return val;
  16. }
  17. return null;
  18. }
  19. }
  20. });

invalidValues (Array | optional)

You can set ignored value. If any translation result on one of this values, that will be ignored.

  1. var maskInstance = TinyMask('9999-9999', {
  2. // in this case, all null, undefined, empty string or blanck spaces returned from translation will be ignored.
  3. invalidValues: [null, undefined, '', ' ']
  4. });

Release Notes

1.0.2

  • Fixing editing after complete mask.

1.0.1

  • Fixing fixed masks.

1.0.0

  • Releasing the first version of the mask.