jquery掩码编辑时间 [英] jquery masked edit for time

查看:16
本文介绍了jquery掩码编辑时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 meioMask Plugin 有什么方法可以设置一个面具,所以它会接受 24 小时制的有效时间,甚至更好地接受 12 小时制?

Using meioMask Plugin is there any way to set up a mask so it will accept valid time in 24h or even better in 12h system?

$("#txtTime").setMask('time');

此插件有一个预定义的 24 小时时间"掩码,但它并不完全正确,因此您可以输入无效的时间值,例如29:00".这个面具不适合这个目的吗?如果不适合,哪个更好?

This plugin has a pre-defined 24h 'time' mask but it's not quite correct, so you can enter invalid time values like "29:00". Is this mask is not suitable for this purpose and if not which one would be better?

推荐答案

试试这个:

$.mask.rules.H = /([0-1][0-9]|2[0-3])/;  // 24 hours
$.mask.masks.time = 'H:59';
$("#txtTime").setMask('time');

更正:

规则只适用于一个字符,因此请记住这一点,您可以在输入以 '2' 开头时交换掩码并验证下一个字符为 [0-3]:

The rules need to be for one character only, so having that in mind, you can swap the mask when the input starts with '2' and validate the next char to be [0-3]:

$("#txtTime").setMask("29:59")
.keypress(function() {
  var currentMask = $(this).data('mask').mask;
  var newMask = $(this).val().match(/^2.*/) ? "23:59" : "29:59";
  if (newMask != currentMask) {
    $(this).setMask(newMask);
  }
});

注意:在这种情况下,我使用的是按键事件,如果需要,请确保处理粘贴事件.

Note: In this case i'm using the keypress event, make sure to handle the paste event if needed.

这篇关于jquery掩码编辑时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆