...

View Full Version : Time Verification



bspahr
10-18-2006, 10:35 PM
I have 2 text fields:
<input type="text" name="startTime">
<input type="text" name="endTime">

For the start time I want to verify that they select a time between
7:00am and 11:30 am.

For the end time I want to verify that they select a time between
12:30 pm and 6:00 pm

It's seems like something similiar to what is below should be pretty close, but I have not been able to get it to work. Can anyone help?


<script language="javascript">

function chkForm() {
var startTime = document.frmOrder.startTime.value;
var endTime = document.frmOrder.endTime.value;

// Check start time
if (startTime.value < 7:00 && > 11:30 ) {
alert('This time is not valid');
return false
}

}
</script>

felgall
10-18-2006, 11:00 PM
There are three things wrong with your test.

The first is that you haven't specified what should be tested for greater than 11:30


if (startTime.value < 7:00 && startTime.value > 11:30 ) {

Second you are testing for if it is bogth less than one value and greater than the other and it can't be both at the same time.


if (startTime.value < 7:00 || startTime.value > 11:30 ) {

Finally 7:00 and 11:30 are not valud as numbers because of the colon in the middle and they are not valid as strings because they are not enclosed in quoutes and they are not valid as times because they are not part of a date/time object. If you enclose them in quotes then they will be valid strings for the test but 8:9999999 will be within the acceptable range even though it is not actually able to be interpreted as a time. You will need to add validation for the field being entered as a correct hh:mm format


var tz = startTime.value.split(':');
if (tz.length != 2) {alert('This time is not valid'); return false;}
if (Number(tz[0]) < 1 || Number(tz[0]) > 12) {alert('This time is not valid'); return false;}
if (Number(tz[1]) > 59) {alert('This time is not valid'); return false;}

bspahr
10-24-2006, 04:16 PM
Thanks for the response. This is sort of working. It still posts the form if the enter a time higher than 11:30 or before 7:00 though. The start time entered must be between 7:00am and 11:30 am. Same thing for the end time (between 12:30 and 6:00 pm). Also, neither of these fields are required. How can I let them post the form even if the don't enter anything?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum