igorescobar / jQuery-Mask-Plugin

A jQuery Plugin to make masks on form fields and HTML elements.
http://igorescobar.github.io/jQuery-Mask-Plugin/
Other
4.77k stars 1.42k forks source link

On-the-fly mask change on mobile devices #751

Open nmbrone opened 4 years ago

nmbrone commented 4 years ago

Hi there 👋

Device

iPhone

Browser (and version)?

Safary/Chrome

Describe de problem depth:

I'm using the plugin like in the code example

var mask0 = '0000 0000 0000 0000 000';
var mask1 = '0000-0000-0000-0000';
var mask2 = 'DDDD DDDD DDDD DDDD DDD';

input.mask(mask0, {
  translation: {
    0: { pattern: /[A-Za-z0-9]/ },
    D: { pattern: /[0-9]/ }
  },
  onKeyPress: (val, e, field, options) => {
    var mask;
    if (val === '') {
      mask = mask0;
    } else if (/^123/.test(val)) {
      mask = mask2;
    } else {
      mask = mask1;
    }
    input.mask(mask, options);
  }
});

and it works great, but the only problem is that on mobile devices when a user pasting the copied value into the masked field the next error occurs

TypeError: undefined is not an object (evaluating 'p.maskDigitPosMapOld[i]')

https://github.com/igorescobar/jQuery-Mask-Plugin/blob/bfd14b5dc61b239a69458242b0c7014f1c17b8a9/src/jquery.mask.js#L224