[英]Validating time in Dashcode widget
I am trying to validate 12 hour time without AM/PM in this input field. 我正在尝试在此输入字段中验证没有AM / PM的12小时时间。 What is the best way to approach this?
解决此问题的最佳方法是什么? I have listed what I have currently which is not working out.
我列出了我目前无法解决的问题。 Been looking for a couple days on how to approach this to no luck.
一直在寻找如何解决这个问题的好几天。
<input id="dtstartf" type="text" name="time" value="08:00" class="apple-textfield apple- no-children" apple-part="com.apple.Dashcode.part.textfield" onkeypress="time(event)">
function time(event) {
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;
}
The following function looks to work OK: 以下功能看起来可以正常运行:
function validateTime(time) {
// Check pattern
var pattern = /^\d{1,2}:\d{1,2}$/;
if (!pattern.test(time)) {
return false;
}
// Split them and check the individual values
var splitvalues = time.split(':');
if (splitvalues[0] > 12 || splitvalues[0] < 1 || splitvalues[1] < 0 || splitvalues[1] > 59) {
return false;
}
// If we get to this point, everything should be valid
return true;
}
However, as you've specifically mentioned that this is for a Dashcode widget, I think the HTML5 time input type may be sufficient, in which case just changing the input type to "time" will be sufficient. 但是,正如您特别提到的,这是用于Dashcode小部件的,我认为HTML5时间输入类型可能就足够了,在这种情况下,只需将输入类型更改为“时间”就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.