[英]Validating time in Dashcode widget
我正在嘗試在此輸入字段中驗證沒有AM / PM的12小時時間。 解決此問題的最佳方法是什么? 我列出了我目前無法解決的問題。 一直在尋找如何解決這個問題的好幾天。
<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;
}
以下功能看起來可以正常運行:
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;
}
但是,正如您特別提到的,這是用於Dashcode小部件的,我認為HTML5時間輸入類型可能就足夠了,在這種情況下,只需將輸入類型更改為“時間”就足夠了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.