Hello,
I'm having some serious problems debugging a script that I'm trying to make
work. I'm working on a form where a user can type in a time (in the format
of HH:MM), and another script automatically calculate how much time is
inbetween.
That part of it is working fine, but what ISN'T working fine is the script
that validates whether or not the user has entered the time in proper
syntax, and to make sure that the Time In does not come after the Time Out.
Here's the two functions, one to validate In and one to validate Out:
function validateInTimeFormat(){
// This function validates whether or not a user has entered proper time
into the form
var inTime = document.frm.inTime.value;
if (validatingOutTime == false) {
if (inTime.indexOf(":") == "-1"){
alert("Time In: You didn't put a \":\"");
this.document.frm.inTime.focus();
validatingInTime = true;
return false;
} else if ((inTime.substr(0,inTime.indexOf(":"))<1) ||
(inTime.substr(0,inTime.indexOf(":"))>12)) {
alert("Please check to make sure your time is in the proper format
(hh/mm)");
document.frm.inTime.focus();
validatingInTime = true;
return false;
} else if ((inTime.substr(inTime.indexOf(":")+1,inTime.lengt h) < 0) ||
(inTime.substr(inTime.indexOf(":")+1,inTime.length ) > 59)) {
alert("Please check to make sure your time is in the proper format
(hh/mm)");
document.frm.inTime.focus();
validatingInTime = true;
return false;
} else {
validatingInTime = false;
return true;
}
}
}
function validateOutTimeFormat(){
// This function validates whether or not a user has entered proper time
into the form
var outTime = document.frm.outTime.value;
if (validatingInTime == false) {
if (outTime.indexOf(":") == "-1"){
alert("Time In: You didn't put a \":\"");
this.document.frm.outTime.focus();
validatingOutTime = true;
return false;
} else if ((outTime.substr(0,outTime.indexOf(":"))<1) ||
(outTime.substr(0,outTime.indexOf(":"))>12)) {
alert("Please check to make sure your time is in the proper format
(hh/mm)");
document.frm.outTime.focus();
validatingOutTime = true;
return false;
} else if ((outTime.substr(outTime.indexOf(":")+1,outTime.le ngth) < 0) ||
(outTime.substr(outTime.indexOf(":")+1,outTime.len gth) > 59)) {
alert("Please check to make sure your time is in the proper format
(hh/mm)");
document.frm.outTime.focus();
validatingOutTime = true;
return false;
} else {
validatingOutTime = false;
return true;
}
}
}
Here's how i have it called in the page:
<br />Time: In <input name="inTime" type="text"
class="formInputText" id="inTime" size="5" onblur="return
validateInTimeFormat();" />
<select name="inTime12Hr" class="formInputText" id="inTime12Hr">
<option value="AM">AM</option>
<option value="PM">PM</option>
</select><br />
Time: Out <input name="outTime" type="text" class="formInputText"
id="outTime" size="5" onblur="return validateOutTimeFormat();" />
<select name="outTime12Hr" class="formInputText" id="outTime12Hr">
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>
FYI: I know I haven't done anything in the functions with the AM and PM.
Eventually I'll just have it add 12 hours to the result if it's PM, but I
haven't reached that point yet. I've been working in IE and also in Mozilla
Firefox (with the JavaScript Debugger). I get everything from "Error:
outTime has no properties" to some crazy nsexception type stuff in Mozilla.
I think the latter occurs when trying to focus the cursor back on the field
in question. It (somewhat) works in IE but not in Mozilla at all. Also,
noting the "if validatingInTime==false" stuff, I had to do that because it
would automatically jump between the inTime field and outTiem field into
eternity if you clicked on one and then the other. Any clues on how much
I've screwed this up? JavaScript is SOOOOOOOOOOO difficult to debug.
Thanks in advance,
Kenneth