jquery-meiomask icon indicating copy to clipboard operation
jquery-meiomask copied to clipboard

Optional mask characters

Open gkinsman opened this issue 13 years ago • 10 comments

Hi,

Is it possible to define a mask with optional characters? I can't find any information in the demo's or the homepage about this. Such a mask would perhaps look like:

AAAA[AA]

Where only four characters are necessary. Obviously there are edge cases related to separated optional characters, but it's something pretty much every other masking library supports.

I really love the paste abilities, and the rest of the plugin looks pretty good. I'd love to use it, but this is holding me back.

Cheers

gkinsman avatar Jul 26 '12 23:07 gkinsman

Me too. The inability to define an optional part holds me back too. http://digitalbush.com/projects/masked-input-plugin/ has it.

MarkKharitonov avatar Aug 31 '12 14:08 MarkKharitonov

it should be a ? or grouping, like you showed above, I was searching the docs for this option, being used to the masked input, and it's not available...

pocesar avatar Mar 13 '13 15:03 pocesar

I need the same functionality. I need to create a mask with a phone digit optional towards the end: (99) 9999-9999?9 Is there any way to do it?

fredw avatar May 20 '13 17:05 fredw

Celular de São Paulo ftw :P

pocesar avatar May 20 '13 19:05 pocesar

did you guys try something like this:

// 'n' can be any char of your choice
$.mask.rules.n = /[0-9]?/

and then defining the mask as:

(99) 9999-9999n

In this case the last number would be optional.

Does this solve the problem?

fabiomcosta avatar May 20 '13 19:05 fabiomcosta

Isso mesmo, funcionou perfeitamente. Obrigado!

fredw avatar May 20 '13 21:05 fredw

How to give the first number as optional. for time the available values for first digit is 0,1,2. If I give 3 then it should be always 03 only. But giving 3 directly is not accepting. Can anyone provide me the solution for it.

mrapaka avatar Jan 15 '14 09:01 mrapaka

Adding a new rule like $.mask.rules.n = /[0-9]?/ will allow letters and symbols too.

julianonunes avatar May 30 '14 19:05 julianonunes

App.phone = {
  overflow: function (character) {
    var $that = $(this);

    if (character.match(/[0-9]/)) {
      $that
        .setMask('(99) 99999-9999').val($that.val() + character)
        .on('keyup', App.phone.keyup);
    }
  },
  keyup: function (event) {
    if (event.keyCode == 8 || event.keyCode == 46) {
      $(this).setMask($.mask.masks.phone);
    }
  }
}

$.mask.masks.phone = { mask: '(99) 9999-9999', autoTab: false, onOverflow: App.phone.overflow };
$(':input').setMask();

This solution is completely functional, created by @marcelofraga.

emaiax avatar Jun 03 '14 18:06 emaiax

try this:

$.mask.rules.n = /[0-9]\d{0}?/

deivide avatar Jun 11 '14 20:06 deivide