Leaving here for future reference.
This example uses the following library:
https://github.com/igorescobar/jQuery-Mask-Plugin
In the examples above, we have problems with the codes that use this library in the form of problems when generating schedules started with '0', for example (06:30, 08:30), so let’s expand the code a little:
HTML:
<input type="text" id="hora" />
JS:
var SPMaskBehavior = function (val) {
return val.replace(/\D/g, '')[0] === '2' ? 'AE:CD' : 'AB:CD';
},
spOptions = {
onKeyPress: function(val, e, field, options) {
field.mask(SPMaskBehavior.apply({}, arguments), options);
},
translation: {
"A": { pattern: /[0-2]/, optional: false},
"B": { pattern: /[0-9]/, optional: false},
"C": { pattern: /[0-5]/, optional: false},
"D": { pattern: /[0-9]/, optional: false},
"E": { pattern: /[0-3]/, optional: false}
}
};
$('#hora').mask(SPMaskBehavior, spOptions);
What the above code does is detect if the first digit of the time was '0' or '1' and in this case allows the second digit to be between '0' and '9'. If the first digit of the time is '2', only allow it to be continued by '0-3'. Minutes are no secret, they proceed in the same way.
JSFIDDLE: http://jsfiddle.net/9cvLzmfs/
Check out if you can solve this: http://stackoverflow.com/questions/2259843/jquery-masked-edit-for-time
– gustavox
I tried to create a fiddle with the answer code of the link I posted above, but I couldn’t... By the way, nor the jQuery Masked Plugin seems to have this possibility (on the demo page itself it accepts 99:99). I think it will be difficult to get something like input time (too bad few browsers accept). I am also interested in this answer, because to change the date (I was using the
date
) I am suffering here, and I still have the hours to change (I didn’t even start :/). So I hope you find(mos) a good answer! :-)– gustavox
Want a solution for modern or old browsers as well? this plugin does not allow this, but it can make a "custom" version if you do not have to use this plugin. I will see if I find an agnostic tb.
– Sergio
@Sergio No need for retroactive compatibility. I will guide users to always use the most updated possible versions of the respective browsers. I am very interested in the version custom.
– Leonel Sanches da Silva
@Ciganomorrisonmendez, your project employs a standard of 12 (am/pm) or 24 hours?
– Caio Felipe Pereira
@Caiofelipepereira Hours is a free field, but if you want to make an answer to 24h, ok.
– Leonel Sanches da Silva
@Ciganomorrisonmendez para 12h is easy to restrict, using the plugin suggested in Guilherme Diego’s reply. For 24 hours, there is a parole that is hard to get out. I keep trying here and I warn you
– Caio Felipe Pereira
@I have not yet had time to finish a "custom Mask" for this case. The idea I’m working on is this: http://jsfiddle.net/hqob3wxj/ do what you want?
– Sergio
The correct answer should be from @master-Oak
– Luiz Vaz