Optional character in input mask

How to specify an optional character in an input mask?

I found this masked input plugin http://digitalbush.com/projects/masked-input-plugin/ and these mask definitions.

$.mask.definitions['g']="[ ]";
$.mask.definitions['h']="[aApP]";
$.mask.definitions['i']="[mM]";
$.mask.definitions['2']="[0-1]";
$.mask.definitions['6']="[0-5]";
new_mask = "29:69";
$("#txtTime").mask(new_mask);

This defines an input mask for a 12 hour time format, for example. 11 a.m. I would like to allow the user to specify only one digit for the "hour". Instead of entering 01:00, the user should be able to enter 1:00. How to do it?

+5
source share
4 answers

You need to use ?for extra characters, so in your case you must use " 2?9:69".

+3
source

, :

. , '?' . + .

jQuery(function($){
   $("#phone").mask("(999) 999-9999? x99999");
});
+3

$.mask.definitions['g']="[0-9 ]";

0

, ($ 0.00 - $10000.00), , .

, $.mask.definitions['symbol'] = '???' , API mask :

$.each(mask.split(""), function (i, c) {
    if (c == '?') {
        len--;
        partialPosition = i;
    } else if (defs[c]) {
        tests.push(new RegExp(defs[c]));
        if (firstNonMaskPos == null)
            firstNonMaskPos = tests.length - 1;
    } else {
        tests.push(null);
    }
});

... On the other hand, the definition of the mask []|[0-9]does not work (I'm trying to make an empty string or 0-9 for those who are not literate in regex.) For reference, I'm trying to create to fulfill the condition 0.00-10000.00, for example oooo9.99, where ois equal []|[0-9].

Since the code confirms that it is based on regular expressions, a zero or range pattern should work, but it is not; this is a more obscure mask error. If only I try to do something wrong, which is also possible ...

0
source

All Articles