Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time Validation 2

Status
Not open for further replies.

curt62

Programmer
Feb 13, 2003
12
US
I have a Javascript validation routine that runs on a Domino web form. I need two more validations done that are giving me trouble. Here is the situation

Start Time is entered in this field ="twField0"
End Time is entered in this field = "twField4"

Times are entered in this format (hh:mm AM/PM) and the user does not have the ability to deviate from this format.

I need to check that the period between the "start time" and the "end time" does not cross midnight. If it does, throw an alert and do not allow user to continue.

I also need to check that the duration between "start time" and "end time" does not exeed 12 hours. If it does throw an alert but allow user to continue.

The data entry is occuring in a form that passes the values to the parent form so the entry form never gets submitted to the server. This is why the validation must happen within this function.

Here is the existing code that is in the JS header of the form. All of these validations are currently working fine.


function twValidateRecord(intThisRecord) {

if (window.document.forms[0].twField0.value.length==0) {alert("Date cannot be empty"); return false;};

if (window.document.forms[0].twField1.value.length==0) {alert("Start Time cannot be empty"); return false;};

if (window.document.forms[0].twField4.value.length==0) {alert("End Time cannot be empty"); return false;};

if ((window.document.forms[0].twField3.value.length==0) && (window.document.forms[0].twField2.value.length!==0))
{alert("Need an In From lunch value"); return false;};

if ((window.document.forms[0].twField2.value.length==0) && (window.document.forms[0].twField3.value.length!==0))
{alert("Need an Out to lunch value"); return false;};


return true;


}

 
after you validate as above...

fromTime = document.forms[0].twField0.value
toTime = document.forms[0].twField4.value

fromDate = new Date()
toDate = new Date()

fromHour = fromTime.substr(0,2)
fromMin = fromTime.substr(3,2)
fromAM = fromTime.substr(6,2)

if (fromAM == "PM"){
fromHour = parseInt(fromHour) + 12
}

fromDate.setHours(fromHour)
fromDate.setMinutes(fromMin)

toHour = toTime.substr(0,2)
toMin = toTime.substr(3,2)
toAM = toTime.substr(6,2)

if (toAM == "PM"){
toHour = parseInt(toHour) + 12
}

toDate.setHours(toHour)
toDate.setMinutes(toMin)


diff = toDate.getTime() - fromDate.getTime()

if (diff > (1000 * 60 * 60 *12)){
//throw error - over 12 hours
}
else if (diff < 0){
//throw error - over midnite (ends before it starts)
}
Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
try this out:
[tt]
<html>
<head>
<script language=&quot;javascript&quot;>
function checkTimes(oStart, oEnd) {
if (!oStart.value || !oEnd.value) {
alert(&quot;Please enter a Start time and an End time.&quot;);
return false;
}
// times are this format (hh:mm AM/PM)
var hhStart = parseInt(oStart.value.split(&quot; &quot;)[0].split(&quot;:&quot;)[0], 10);
var hhEnd = parseInt(oEnd.value.split(&quot; &quot;)[0].split(&quot;:&quot;)[0], 10);
var mmStart = parseInt(oStart.value.split(&quot; &quot;)[0].split(&quot;:&quot;)[1], 10);
var mmEnd = parseInt(oEnd.value.split(&quot; &quot;)[0].split(&quot;:&quot;)[1], 10);

var ampmStart = oStart.value.split(&quot; &quot;)[1].toUpperCase();
var ampmEnd = oEnd.value.split(&quot; &quot;)[1].toUpperCase();

// make sure time doesn't span midnight
if (ampmStart == &quot;PM&quot; && hhStart < 12 && ampmEnd == &quot;AM&quot;) {
alert(&quot;Times may not span midnight.&quot;);
return false;
}

// make sure time span < 12 hours total
if (ampmStart != ampmEnd) hhEnd += 12;
if (hhEnd - hhStart > 12 || (hhEnd - hhStart == 12 && mmEnd > mmStart)) {
alert(&quot;Time span may not exceed 12 hours.&quot;);
return false;
}
}
</script>
</head>
<body>
<form>
<input type=&quot;text&quot; name=&quot;twField0&quot; />
<input type=&quot;text&quot; name=&quot;twField4&quot; />
<input type=&quot;button&quot; name=&quot;&quot; value=&quot;checkTimes();&quot; onclick=&quot;checkTimes(this.form.twField0,this.form.twField4);&quot; />
</form>
</body>
</html>

[/tt]





======================================================================
=========================================================
if (!succeed) try();
-jeff
 
jemminger

that is a nice bit of coding there but it will still allow for a start time to be say 11:30 AM and the end time to be 12:30 AM. This would also span midnight but would not be caught.

This is a time sheet application and I have to cover all my bases or the users will surely mess it up.
 
hmm...how about this:
[tt]
function checkTimes(oStart, oEnd) {
if (!oStart.value || !oEnd.value) {
alert(&quot;Please enter a Start time and an End time.&quot;);
return false;
}
// times are this format (hh:mm AM/PM)
var hhStart = parseInt(oStart.value.split(&quot; &quot;)[0].split(&quot;:&quot;)[0], 10);
var hhEnd = parseInt(oEnd.value.split(&quot; &quot;)[0].split(&quot;:&quot;)[0], 10);
var mmStart = parseInt(oStart.value.split(&quot; &quot;)[0].split(&quot;:&quot;)[1], 10);
var mmEnd = parseInt(oEnd.value.split(&quot; &quot;)[0].split(&quot;:&quot;)[1], 10);

var ampmStart = oStart.value.split(&quot; &quot;)[1].toUpperCase();
var ampmEnd = oEnd.value.split(&quot; &quot;)[1].toUpperCase();

if (ampmStart == &quot;PM&quot;) hhStart += 12;
if (ampmEnd == &quot;PM&quot;) hhEnd += 12;

// make sure time doesn't span midnight
if ((ampmStart == &quot;PM&quot; && hhStart < 12 && ampmEnd == &quot;AM&quot;) ||
ampmStart == &quot;AM&quot; && ampmEnd == &quot;AM&quot; && hhStart < 12 &&
(hhEnd > 12 || (hhEnd == 12 && mmEnd > 0))) {
alert(&quot;Times may not span midnight.&quot;);
return false;
}

// make sure time span < 12 hours total
if (hhEnd - hhStart > 12 || (hhEnd - hhStart == 12 && mmEnd > mmStart)) {
alert(&quot;Time span may not exceed 12 hours.&quot;);
return false;
}
}
[/tt]
=========================================================
if (!succeed) try();
-jeff
 
In my code, the 11:30 AM to 12:30 AM would throw an error because I set them on the same day - therefore the end time would occur before the start time... Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
mwolf00

your code is working for the most part however,I have a small problem. I need users to be able to select 12:00 AM as an end time to the shift. right now it throws the alert.
I attempted to modify your if statement to allow 12:00 AM as an end time but it won't work for me

if ((diff < 0)&&(document.forms[0].twField4.value!=&quot;12:00 AM&quot;) {
{alert(&quot;Shift cannot span Midnight&quot;); return false;};
}

Also the test for being over 12 hours is giving me the alert if I enter 11:30 AM as start time and 9:30 PM as end time. Not over 12 yet it throws the alert.

Any ideas?
 
// the easiest thing to do so you don't have to worry about the last day of the year is to reset &quot;toDate&quot; to 11:59:59:999

//after these two lines of code
toDate.setHours(toHour)
toDate.setMinutes(toMin)

//add this if statement (shave off 1 millisecond from midnight)
if (toTime == &quot;12:00 AM&quot;){
toDate.setHours(23)
toDate.setMinutes(59)
toDate.setSeconds(59)
toDate.setMilliseconds(999)
}

//original code continues here....

diff = toDate.getTime() - fromDate.getTime()

if (diff > (1000 * 60 * 60 *12)){
//throw error - over 12 hours
}
else if (diff < 0){
//throw error - over midnite (ends before it starts)
}
Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Did what you said but still throws the alert. Here is the code. Can you see my problem.

I appreciate any light you can shed.


function twValidateRecord(intThisRecord) {

if (window.document.forms[0].twField0.value.length==0) {alert(&quot;Date cannot be empty&quot;); return false;};

if (window.document.forms[0].twField1.value.length==0) {alert(&quot;Start Time cannot be empty&quot;); return false;};

if (window.document.forms[0].twField4.value.length==0) {alert(&quot;End Time cannot be empty&quot;); return false;};

if ((window.document.forms[0].twField3.value.length==0) && (window.document.forms[0].twField2.value.length!==0))
{alert(&quot;Need an In From lunch value&quot;); return false;};

if ((window.document.forms[0].twField2.value.length==0) && (window.document.forms[0].twField3.value.length!==0))
{alert(&quot;Need an Out to lunch value&quot;); return false;};

fromTime = document.forms[0].twField1.value
toTime = document.forms[0].twField4.value

fromDate = new Date()
toDate = new Date()

fromHour = fromTime.substr(0,2)
fromMin = fromTime.substr(3,2)
fromAM = fromTime.substr(6,2)

if (fromAM == &quot;PM&quot;){
fromHour = parseInt(fromHour) + 12
}

fromDate.setHours(fromHour)
fromDate.setMinutes(fromMin)

toHour = toTime.substr(0,2)
toMin = toTime.substr(3,2)
toAM = toTime.substr(6,2)

if (toAM == &quot;PM&quot;){
toHour = parseInt(toHour) + 12
}

toDate.setHours(toHour)
toDate.setMinutes(toMin)
if (toTime == &quot;12:00 AM&quot;){
toDate.setHours(23)
toDate.setMinutes(59)
toDate.setSeconds(59)
toDate.setMilliseconds(999)
}


diff = toDate.getTime() - fromDate.getTime()

if (diff < 0) {
{alert(&quot;Shift cannot span Midnight&quot;); return false;};
}

else if (diff > (1000 * 60 * 60 *12)){
{alert(&quot;This block of time is longer than twelve hours! Choosing OK will allow you to continue.&quot;); return true;};
}


return true;

};
 
Yep! I forgot something kinda important... I mishandled 12PM...


<script>
fromTime = &quot;12:00 PM&quot;
toTime = &quot;12:00 AM&quot;

fromDate = new Date()
toDate = new Date()

fromHour = fromTime.substr(0,2)
fromMin = fromTime.substr(3,2)
fromAM = fromTime.substr(6,2)

if (fromAM == &quot;PM&quot; && parseInt(fromHour) < 12){
fromHour = parseInt(fromHour) + 12
}

fromDate.setHours(fromHour)
fromDate.setMinutes(fromMin)

toHour = toTime.substr(0,2)
toMin = toTime.substr(3,2)
toAM = toTime.substr(6,2)

if (toAM == &quot;PM&quot; && parseInt(toHour) < 12){
toHour = parseInt(toHour) + 12
}

toDate.setHours(toHour)
toDate.setMinutes(toMin)
if (toTime == &quot;12:00 AM&quot;){
toDate.setHours(23)
toDate.setMinutes(59)
toDate.setSeconds(59)
toDate.setMilliseconds(999)
}


diff = toDate.getTime() - fromDate.getTime()
alert (fromDate + &quot;\n&quot; + toDate + &quot;\n&quot; + diff)
if (diff < 0) {
alert(&quot;Shift cannot span Midnight&quot;)
}
else if (diff > (1000 * 60 * 60 *12)){
alert(&quot;This block of time is longer than twelve hours! Choosing OK will allow you to continue.&quot;)
}
else{
alert (&quot;ok&quot;)
}
</script> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Tried it...it returns a box that shows the date and time first, click ok and and then another alert OK. Problem is it appears to OK everything now. good and bad entries alike.:-(
 
Copy and paste this onto a blank page and see what you get...

<script>
fromTime = new Array(&quot;11:59 PM&quot;, &quot;01:15 AM&quot;, &quot;03:55 PM&quot;,&quot;09:00 AM&quot;,&quot;10:00AM&quot;)
toTime = new Array(&quot;03:00 AM&quot;,&quot;03:15 PM&quot;,&quot;12:00 AM&quot;, &quot;05:00 PM&quot;,&quot;10:01 PM&quot;)

fromDate = new Date()
toDate = new Date()

for (x=0; x<fromTime.length; x++){
fromHour = fromTime[x].substr(0,2)
fromMin = fromTime[x].substr(3,2)
fromAM = fromTime[x].substr(6,2)

if (fromAM == &quot;PM&quot; && parseInt(fromHour) <12){
fromHour = parseInt(fromHour) + 12
}

fromDate.setHours(fromHour)
fromDate.setMinutes(fromMin)

toHour = toTime[x].substr(0,2)
toMin = toTime[x].substr(3,2)
toAM = toTime[x].substr(6,2)

if (toAM == &quot;PM&quot; && parseInt(toHour) <12){
toHour = parseInt(toHour) + 12
}

toDate.setHours(toHour)
toDate.setMinutes(toMin)
if (toTime[x] == &quot;12:00 AM&quot;){
toDate.setHours(23)
toDate.setMinutes(59)
toDate.setSeconds(59)
toDate.setMilliseconds(999)
}


diff = toDate.getTime() - fromDate.getTime()
document.writeln (&quot;From Time: &quot; + fromTime[x] + &quot;&nbsp;&nbsp;&nbsp;To Time: &quot; + toTime[x] + &quot;&nbsp;&nbsp;&nbsp;Millisecs:&quot; + diff)
if (diff < 0) {
document.writeln (&quot;Shift cannot span Midnight&quot;)
}
else if (diff > (1000 * 60 * 60 *12)){
document.writeln (&quot;This block of time is longer than twelve hours! Choosing OK will allow you to continue.&quot;)
}
else{
document.writeln (&quot;ok&quot;)
}
document.writeln (&quot;<br>&quot;)
}
</script> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
This is what I get....


From Time: 11:59 PM To Time: 03:00 AM Millisecs:-75540000 Shift cannot span Midnight
From Time: 01:15 AM To Time: 03:15 PM Millisecs:50400000 This block of time is longer than twelve hours! Choosing OK will allow you to continue.
From Time: 03:55 PM To Time: 12:00 AM Millisecs:29051055 ok
From Time: 09:00 AM To Time: 05:00 PM Millisecs:28811055 ok
From Time: 10:00AM To Time: 10:01 PM Millisecs:43271055 This block of time is longer than twelve hours! Choosing OK will allow you to continue.
 
Looks right to me - it's not returning &quot;OK&quot; for all of them, only the valid ones....

Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Duh
I neglected to subsitute

fromTime = document.forms[0].twField0.value
toTime = document.forms[0].twField4.value
for your values
fromTime = &quot;12:00 PM&quot;
toTime = &quot;12:00 AM&quot;

Thanks for your patience..
 
Doh!
Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048

mikewolf@tst-us.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top