Javascript jQuery AM PM 时间格式的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8820372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex for AM PM time format for jquery
提问by SoftwareSavant
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.
我对用正则表达式屏蔽输入字段感到沮丧。我想将输入限制为 hh:mm AM|PM 格式,但我无法使用此正则表达式。
I use this regex in conjunction with a jquery tool from www.ThimbleOpenSource.com
. It was the filter_input.js tool or whatever.
我将此正则表达式与来自www.ThimbleOpenSource.com
. 它是 filter_input.js 工具或其他工具。
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 测试链接。
回答by Armin
I have made a jsfiddle example, based on the regular expression of the answer von Yuri: http://jsfiddle.net/Evaqk/
我根据回答 von Yuri 的正则表达式做了一个 jsfiddle 示例: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');
}
});
回答by YuS
First of all, if you using it for input field, you should never let users input date or time information using text fields and hoping it will be in strict format.
首先,如果你将它用于输入字段,你永远不应该让用户使用文本字段输入日期或时间信息,并希望它是严格的格式。
But, if you insist:
但是,如果你坚持:
/^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/
This regex will validate time in AM/PM format.
此正则表达式将以 AM/PM 格式验证时间。
回答by noob
You can't do that with that plugin because here you need to check each character.
你不能用那个插件来做到这一点,因为在这里你需要检查每个字符。
HTML:
HTML:
<form>
<p>When?</p>
<input type="text" id="test1" placeholder="hh:mm(AM|PM)"/>
</form>
JavaScript:
JavaScript:
$("#test1").keypress(function(e) {
var regex = ["[0-2]",
"[0-4]",
":",
"[0-6]",
"[0-9]",
"(A|P)",
"M"],
string = $(this).val() + String.fromCharCode(e.which),
b = true;
for (var i = 0; i < string.length; i++) {
if (!new RegExp("^" + regex[i] + "$").test(string[i])) {
b = false;
}
}
return b;
});
回答by anngelfra
So I'm pretty sure this is solved by now, but I was recently struggling with this and couldn't find an answer fast enough. I'm using Bootstrap Validator (bootstrapvalidator.com by @nghuuphuoc) in conjunction with Eonasdan's DateTimepicker to validate an hour (PS: You can disable the date in Eonasdan's plugin).
所以我很确定这个问题现在已经解决了,但我最近一直在努力解决这个问题并且找不到足够快的答案。我正在使用 Bootstrap Validator(@nghuuphuoc 的 bootstrapvalidator.com)和 Eonasdan 的 DateTimepicker 来验证一个小时(PS:您可以在 Eonasdan 的插件中禁用日期)。
Since Bootstrap Validator doesn't yet have a validator for time, you have to use a Regex. This one worked perfectly for me:
由于 Bootstrap Validator 还没有时间验证器,因此您必须使用 Regex。这个对我来说非常有效:
^([0-1]?[0-9]|2[0-3]):[0-5][0-9] [APap][mM]$
^([0-1]?[0-9]|2[0-3]):[0-5][0-9] [APap][mM]$
回答by Malek Tubaisaht
the follwing function return (true/false) value
以下函数返回(真/假)值
function CheckTime(HtmlInputElement) {
var dt = HtmlInputElement.value;
return (/(0?[1-9]|1[0-2]):[0-5][0-9] ?[APap][mM]$/.test(dt));
}
回答by Ajay2707
Check this solution too.
也检查这个解决方案。
<!DOCTYPE html>
<html>
<body>
<p>This demo used to validate time with 12 hours format</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var regexp = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/;
var res = regexp.test('12:00 AM'); //try with alphabets or wrong format
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>