用于jquery的AM PM时间格式的正则表达式 [英] Regex for AM PM time format for jquery
问题描述
我对正则表达式屏蔽输入字段感到沮丧。我想限制输入为hh:mm AM | PM格式,我无法使这个正则表达式工作。
I am being frustrated by a regular expression to mask the input field. I want to Limit input to hh:mm AM|PM format and I can't get this regex to work.
我将此正则表达式与来自 www.ThimbleOpenSource.com
的jquery工具结合使用。它是filter_input.js工具或其他什么。
I use this regex in conjunction with a jquery tool from www.ThimbleOpenSource.com
. It was the filter_input.js tool or whatever.
它似乎适用于一个简单的正则表达式,但我提出的那个似乎不起作用。这是我在下面的jsFiddle测试链接。
It seems to work for a simple regular expression but the one I came up with doesn't seem to work. This is my jsFiddle test link below.
推荐答案
我做了一个jsfiddle示例,基于答案von Yuri的正则表达式:
http://jsfiddle.net/Evaqk/
I have made a jsfiddle example, based on the regular expression of the answer von Yuri: http://jsfiddle.net/Evaqk/
$('#test1, #test2').blur(function(){
var validTime = $(this).val().match(/^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/);
if (!validTime) {
$(this).val('').focus().css('background', '#fdd');
} else {
$(this).css('background', 'transparent');
}
});
这篇关于用于jquery的AM PM时间格式的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!