I have these date and time fields, and I want to set a javascript validation for the time.
If the format is invalid, it should make the label visible, else it should be invisible.
This is the code I have so far.
<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>
The problem is the label shows up when loaded irrespective of what I set as visible. I tried visibility = "hidden" and it still shows up.
Here is the validation part:
<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>
So, how should I go about this? Google shows up different validation methods but not how to hide/show labels