Optional character in input mask

user823527 picture user823527 · Mar 3, 2012 · Viewed 21.9k times · Source

How do I 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, e.g. 11:00. I'd like to allow the user to specify only one digit for the "hour". Instead of having to type 01:00, the user should be able to type 1:00. How do I do that?

Answer

Bruno Silva picture Bruno Silva · Mar 3, 2012

You need to use ? for optional characters so in your case you should use "2?9:69".