RobinHerbots / Inputmask

Input Mask plugin
https://robinherbots.github.io/Inputmask/
MIT License
6.39k stars 2.17k forks source link

Custom mask: allow * in ip mask #1943

Open cbranca opened 6 years ago

cbranca commented 6 years ago

Hi, I'm having some trouble creating a custom mask. I need the "ip" mask that allows to insert also "". I used the "ip" mask and added the following regex: '25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|\' But it doesn't work, can you give me some help? This is the code:

Inputmask.extendAliases({
    ipstar: {
      mask: 'i[i[i]].i[i[i]].i[i[i]].i[i[i]]',
      definitions: {
        i: {
          validator: function (chrs, maskset, pos, strict, opts) {
            return pos - 1 > -1 && maskset.buffer[pos - 1] !== '.' ? (chrs = maskset.buffer[pos - 1] + chrs,
              chrs = pos - 2 > -1 && maskset.buffer[pos - 2] !== '.' ? maskset.buffer[pos - 2] + chrs : '0' + chrs) : chrs = '00' + chrs,
            new RegExp('25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|\*').test(chrs);
          }
        }
      },
      onUnMask: function (maskedValue, unmaskedValue, opts) {
        return maskedValue;
      },
      inputmode: 'verbatim'
    }
  });
  var im = new Inputmask('ipstar');
  im.mask(input);
RobinHerbots commented 6 years ago

@cbranca ,

As example for an Ip you would like to use 10.200.0.* ?

I guess updating the regex to (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|*

RobinHerbots commented 6 years ago

@cbranca ,

Maybe this one

 "ip": { //ip-address mask
            mask: "(i[i[i]])|\\*.(i[i[i]])|\\*.(i[i[i]])|\\*.(i[i[i]])|\\*",
            definitions: {
                "i": {
                    validator: function (chrs, maskset, pos, strict, opts) {
                        if (pos - 1 > -1 && maskset.buffer[pos - 1] !== ".") {
                            chrs = maskset.buffer[pos - 1] + chrs;
                            if (pos - 2 > -1 && maskset.buffer[pos - 2] !== ".") {
                                chrs = maskset.buffer[pos - 2] + chrs;
                            } else chrs = "0" + chrs;
                        } else chrs = "00" + chrs;
                        return new RegExp("25[0-5]|2[0-4][0-9]|[01][0-9][0-9]").test(chrs);
                    }
                }
            },
            onUnMask: function (maskedValue, unmaskedValue, opts) {
                return maskedValue;
            },
            inputmode: "numeric",
        },
cbranca commented 6 years ago

Hi @RobinHerbots , sorry for late response. I solved this way:

Inputmask.extendAliases({
    ipstar: {
      mask: '(i[i[i]]|*).(i[i[i]]|*).(i[i[i]]|*).(i[i[i]]|*)',
      definitions: {
        'i': {
          validator: function (chrs, maskset, pos, strict, opts) {
            if (pos - 1 > -1 && maskset.buffer[pos - 1] !== '.') {
              chrs = maskset.buffer[pos - 1] + chrs;
              if (pos - 2 > -1 && maskset.buffer[pos - 2] !== '.') {
                chrs = maskset.buffer[pos - 2] + chrs;
              } else {
                chrs = '0' + chrs;
              }
            } else {
              chrs = '00' + chrs;
            }
            return new RegExp('25[0-5]|2[0-4][0-9]|[01][0-9][0-9]').test(chrs);
          }
        },
        '*': {
          validator: '[\\*]',
          cardinality: 1
        }
      },
      onUnMask: function (maskedValue, unmaskedValue, opts) {
        return maskedValue;
      },
      inputmode: 'verbatim'
    }
  });
  var im = new Inputmask('ipstar');