Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional character in input mask

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?

like image 462
user823527 Avatar asked Mar 03 '12 01:03

user823527


2 Answers

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

like image 80
user1047873 Avatar answered Sep 19 '22 11:09

user1047873


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");
});
like image 25
Adam Zalcman Avatar answered Sep 20 '22 11:09

Adam Zalcman