使用regex验证格式化为1:00:00的时间

Validate time formatted as 1:00:00 with regex

本文关键字:时间 regex 验证 格式化 使用      更新时间:2023-09-26

我为jquery验证插件增加了时间。

$.validator.addMethod("time24", function(value, element) {
    return this.optional(element) || /^([01]'d|2[0-3])(:[0-5]'d){1,2}$/.test(value);
}, "not valid time format.");

这只适用于这种时间格式:

00:00:00
01:00:00

但我的时间格式是:

0:00:00
1:00:00

这个正则表达式怎么样:

/^([01]?'d|2[0-3])(:[0-5]'d){1,2}$/

子表达式[01]?'d|2[0-3]将接受01。。。910。。。,22230001。。。这是你想要的,还是你想要一小时的前导零?在这种情况下使用:

/^(1?'d|2[0-3])(:[0-5]'d){1,2}$/