Optional character in input mask

24,529

Solution 1

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

Solution 2

Quote and example from the page you linked to in your question:

You can have part of your mask be optional. Anything listed after '?' within the mask is considered optional user input. The common example for this is phone number + optional extension.

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

Solution 3

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

Share:
24,529
user823527
Author by

user823527

Updated on December 29, 2020

Comments

  • user823527
    user823527 over 3 years

    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?

  • user823527
    user823527 over 12 years
    But this still doesn't allow the user to specify only one character for the hour. When I click out of the input field, the mask puts the minutes for the missing hour digit.
  • Bruno Silva
    Bruno Silva over 12 years
    Good catch, this seems like a bug in the plugin. Unfortunately, I don't know of a good solution for it unless you modify the plugin itself.