Basically I want two scripts, one PHP and one JavaScript. These scripts need to validate certain fields in a form to make sure they are in HH:MM:SS format.
I had a paritial JavaScript one, but it doesn't seem to be working at all. The form is a standard form and the submit button looks like this:
Code:
<input type="submit" value="Open Ticket" onClick="return checkForm();" />
Even when I change the functions to only return false, it still submits through.
I also have no idea how this section of code works. I got it from a tutorial, but am not sure how it works. Could anyone point me to a guide on these expression things?
Code:
timePat = /^(\d{1,2}):(\d{2}):(\d{2})$/;
... and this code is all the JavaScript code in my <HEAD>. It doesn't seem to be working, because when a user clicks on Submit the form always posts. Even if I edit the function to only return with false.
Code:
<script language="JavaScript">
function checkForm() {
var ctime, ctimeLost, ctimeReran;
with(window.document.ticket) {
ctime = time;
ctimeLost = timeLost;
ctimeReran = timeReran;
}
if(!isValidTime(ctime)) {
alert("Time Opened much be in HH:MM:SS format");
return false;
}
if(!isValidTime(ctimeLost)) {
alert("Time Lost much be in HH:MM:SS format");
return false;
}
if(!isValidTime(ctimeReran)) {
alert("Time Reran much be in HH:MM:SS format");
return false;
}
return true;
}
function isValidTime(timeStr) {
// Checks if time is in HH:MM:SS format.
var timePat = /^(\d{1,2}):(\d{2}):(\d{2})$/;
var matchArray = timeStr.match(timePat);
if (matchArray == null) {
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
if (hour < 0 || hour > 23) {
return false;
}
if (minute<0 || minute > 59) {
return false;
}
if (second < 0 || second > 59) {
return false;
}
return true;
}
</script>
Thanks for any help.