我有这些日期和时间字段,我想为时间设置一个 javascript 验证。
如果格式无效,则应使标签可见,否则应不可见。
这是我到目前为止的代码。
<td nowrap="nowrap" align="left">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="right" nowrap="nowrap">
<span id="startDateLabel">Start date/time: </span>
<input id="startDateStr" name="startDateStr" size="8" onchange="if (!formatDate(this,'USA')) {this.value = '';}" />
<button id="startDateCalendarTrigger">...</button>
<input id="startDateTime" type="text" size="8" name="startTime" value="12:00 AM" onchange="validateHHMM(this.value);"/>
<label id="startTimeLabel" visible="false">Time must be entered in the format HH:MM AM/PM</label>
<BR>
<span id="endDateLabel">End date/time: </span>
<input id="endDateStr" name="endDateStr" size="8" onchange="if (!formatDate(this,'USA')) {this.value = '';}" />
<button id="endDateCalendarTrigger">...</button>
<input id="endDateTime" type="text" size="8" name="endTime" value="12:00 AM" onchange="validateHHMM2(this.value);"/>
<label id="endTimeLabel" visible="false">Time must be entered in the format HH:MM AM/PM</label>
</td>
</tr>
</table>
</td>
问题是标签在加载时显示,无论我设置为可见。我试过可见性=“隐藏”,它仍然出现。
这是验证部分:
<script>
function validateHHMM(inputField) {
var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(inputField.value);
if (isValid) {
document.getElementById("startTimeLabel").style.visibility = "hidden";
}else {
document.getElementById("startTimeLabel").style.visibility = "visible";
}
return isValid;
}
function validateHHMM2(inputField) {
var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(inputField.value);
if (isValid) {
document.getElementById("endTimeLabel").style.visibility = "hidden";
}else {
document.getElementById("endTimeLabel").style.visibility = "visible";
}
return isValid;
}
</script>
那么,我该怎么做呢?谷歌显示了不同的验证方法,但没有显示如何隐藏/显示标签