Click to See Complete Forum and Search --> : this script is driving me crazy!!!


sanjuT
06-05-2003, 10:53 AM
I do not see why this will not work.

I have 2 dropdown boxes with times, both AM and PM.

I set a value for each time like this:

<option value="1">7AM</option>
<option value="2">7:15AM</option>
<option value="3">7:30AM</option>
<option value="4">7:45AM</option>
<option value="5">8AM</option>
<option value="6">8:15AM</option>
<option value="7">8:30AM</option>
<option value="8">8:45AM</option>
etc...

The first dropdown is for StartingTime, the second for EndingTime.

I am trying to validate this so that the user can not pick an EndingTime that is before the StartingTime.

I am using this for Netscape 4.x.

This is my script, and I put it in my general 'validate' function (which has a number of smaller functions):

//TIME VALIDATION
if ((f.StartingTime.options[f.StartingTime.selectedIndex].value)>=(f.EndingTime.options[f.EndingTime.selectedIndex].value)) {
alert ("Please make sure the Starting Time you have choosen is before the Ending Time.");
f.StartingTime.focus();
return false;
}

If anyone can help, I would appreaciate it.
THANKS!

Jona
06-05-2003, 11:26 AM
Make sure f refers to document.formName and that you don't get any errors. If you get any errors, please tell me what they are. Looks like it should work, though...

Jona

sanjuT
06-05-2003, 11:38 AM
when i try to submit the form without selecting any start or end times

(<select name="StartingTime">
<option value="start">Select
a Time...</option>
and

<select name="EndingTime">
<option value="end">Select a Time...</option>

then I get the alerts, and i continue to get them if i am still making the mistake of an ending time before the start time.

here is the Validation function that is called when the form is submitted, the error might be in how i set this up:

function validate(f) {
// Required Date must be 2 days from present
var twoDate = new Date();
var yy = twoDate.getFullYear();
var mm = twoDate.getMonth() + 1;
var dd = twoDate.getDate();
twoDate = new Date(yy, (mm - 1), dd);
var twoDate = new Date(yy, (mm - 1), dd);
//var twoWeeks = 14 * 24 * 60 * 60 * 1000;
var twoDays = 2 * 24 * 60 * 60 * 1000;
//twoDate.setTime(twoDate.getTime()+twoWeeks);
twoDate.setTime(twoDate.getTime()+twoDays);
with (f)
{
yy = Number(Year.options[Year.selectedIndex].text);
mm = Month.selectedIndex + 1;
dd = Number(Day.options[Day.selectedIndex].text);
}
var userDate = new Date(yy, (mm - 1), dd);
if (userDate.getTime() < twoDate.getTime())
{
alert("Date Required must be at\n"+
"least two days from now.");
f.Month.focus();
return false;
}
// }

//TIME VALIDATION
if ((f.StartingTime.options[f.StartingTime.selectedIndex].value)>=(f.EndingTime.options[f.EndingTime.selectedIndex].value)) {
alert ("Please make sure the Starting Time you have choosen is before the Ending Time.");
f.StartingTime.focus();
return false;
}




// Outer loop that cycles through every element
// of the form
var empty_fields;
var msg;
var empty_fields = "";
for(var i=0; i<f.length; i++)
{
// Variable that holds the numeric index of the
// current form element
var e = f.elements[i];

// Check to see if the current element is a
// text/textarea box and if it is not optional.
if(((e.type=="text") || (e.type=="textarea")) && !e.optional)
{
// Build a list of required fields that have
// been left empty
if((e.value==null) || (e.value==""))
{
empty_fields += "\n" + e.name;
}
}
}




<!-- Begin SECTION VALIDATION
//First Break
if ((f.FirstBreakTime.options[f.FirstBreakTime.selectedIndex].value!="b1") && (f.BreakItems1.value=="")) {
alert("You have selected a time for the first break, but no food item(s) were selected.");
f.BreakItems1.focus();
return false;
}
if ((f.FirstBreakTime.options[f.FirstBreakTime.selectedIndex].value=="b1") && (f.BreakItems1.value!="")) {
alert("You have selected food item(s) for the first break, but no time was specified.");
f.FirstBreakTime.focus();
return false;
}

//Second Break
if ((f.SecondBreakTime.options[f.SecondBreakTime.selectedIndex].value!="b2") && (f.BreakItems2.value=="")) {
alert("You have selected a time for the second break, but no food item(s) were selected.");
f.BreakItems2.focus();
return false;
}
if ((f.SecondBreakTime.options[f.SecondBreakTime.selectedIndex].value=="b2") && (f.BreakItems2.value!="")) {
alert("You have selected food item(s) for the second break, but no time was specified.");
f.SecondBreakTime.focus();
return false;
}

//Lunch
if ((f.LunchTime.options[f.LunchTime.selectedIndex].value!="l") && (f.LunchItems.value=="")) {
alert("You have selected a time for the lunch break, but no food item(s) were selected.");
f.LunchItems.focus();
return false;
}
if ((f.LunchTime.options[f.LunchTime.selectedIndex].value=="l") && (f.LunchItems.value!="")) {
alert("You have selected food item(s) for the lunch break, but no time was specified.");
f.LunchTime.focus();
return false;
}


//Third Break
if ((f.ThirdBreakTime.options[f.ThirdBreakTime.selectedIndex].value!="b3") && (f.BreakItems3.value=="")) {
alert("You have selected a time for the third break, but no food item(s) were selected.");
f.BreakItems3.focus();
return false;
}
if ((f.ThirdBreakTime.options[f.ThirdBreakTime.selectedIndex].value=="b3") && (f.BreakItems3.value!="")) {
alert("You have selected food item(s) for the third break, but no time was specified.");
f.ThirdBreakTime.focus();
return false;
}



// END SECTION VALIDATION--->

//validate department list
if (f.Dept.options[f.Dept.selectedIndex].value=="dept")
{
empty_fields +="\n" + "Department"
}

//validate event location
if (f.EventLocation.options[f.EventLocation.selectedIndex].value=="eventloc")
{
empty_fields +="\n" + "Event Location"
}
//validate start time
if (f.StartingTime.options[f.StartingTime.selectedIndex].value=="start")
{
empty_fields +="\n" + "Starting Time"
}
//validate end time
if (f.EndingTime.options[f.EndingTime.selectedIndex].value=="end")
{
empty_fields +="\n" + "Ending Time"
}

//validate audiovisual
//if (f.AudioVisual.options[f.AudioVisual.selectedIndex].value=="aud")
//{
// empty_fields +="\n" + "Audio Visual"
//}

//validate set up
if (f.SetUp.options[f.SetUp.selectedIndex].value=="set")
{
empty_fields +="\n" + "Set Up"
}

//validate preferred room
if (f.PreferredRoom.options[f.PreferredRoom.selectedIndex].value=="VP")
{
empty_fields +="\n" + "Preferred Room"
}


//alert(msg);


// if no empty fields, then go ahead and
// submit the form
if((empty_fields == null) || (empty_fields == "") )
{
return true;
}
// if missing information, display the error
// message
else if(empty_fields)
{
msg = "- The following required field(s) are empty:" + empty_fields + "\n";

alert(msg);
return false;
}
}

Jona
06-05-2003, 11:51 AM
I don't have Netscape 4.x, so I can't really help you too much by just looking at the code (because Netscape 4 is so old that... :p). So just tell me what the error says. "Object expected"? "Syntax error"? "[object] is null or undefined"? I also need to know when the error occurs.

Jona

sanjuT
06-05-2003, 11:55 AM
the error i get is that i do not get the alerts when i select a starting time that is after the ending time.

not really an error, just that the script won't work..

Jona
06-05-2003, 12:02 PM
Hm... Strange. I don't have Netscape four.. But I'll test it in NS6, Mozilla and IE and see what I can come up with... Do you have a link to a page with this on it or something?

Jona

sanjuT
06-05-2003, 12:07 PM
unfortunately this is on our intranet, so no link avail.

i'll attach the zipped page.

Jona
06-05-2003, 12:35 PM
Works in IE6, Mozilla 1.3, and NS6.2 for me..

Gil Davis... ;)
Jona

sanjuT
06-05-2003, 01:35 PM
thanks.

i tried in those browsers and yes, it works fine for me.

too bad my boss says i have to develop for this crappy old browser..

oh well, thanks again.

Jona
06-05-2003, 01:37 PM
Perhaps you should try setting it as a variable and using parseInt(). That might work.

Jona

scriptkid
06-05-2003, 01:47 PM
It looks to me like you are comparing the values of the two select boxes as two strings rather than two integers. You should go ahead and parseInt() them as Jona suggested. That may or may not solve the problem. It is more stable though.

sanjuT
06-05-2003, 02:21 PM
i tried using a variable, got the same problem..

i then tried the parseInt() way, and i am not sure if i am using it right, but it seems to work now (i guess it converts the option value to an integer).

here's what I tried:

//TIME VALIDATION
if (parseInt((f.StartingTime.options[f.StartingTime.selectedIndex].value))>=(parseInt(f.EndingTime.options[f.EndingTime.selectedIndex].value))) {
alert ("Please make sure the Starting Time you have choosen is before the Ending Time.");
f.StartingTime.focus();
return false;
}

Is this the correct way to use this, or have i lost my mind?
THANKS ALL!!!!!

Jona
06-05-2003, 02:34 PM
Yes! It looks like it works!

Jona

sanjuT
06-05-2003, 02:41 PM
Thanks a lot Jona, I really appreciate the time and effort you put in to help me :D

Jona
06-05-2003, 02:43 PM
You're welcome. I help when I can--if I can; and I usually can. ;) (And I'm glad I usually can.)

\Happy/ \Jona/

sanjuT
06-05-2003, 03:22 PM
My mind is mush right now, can't figure this out..

it is similar to the problem above.
I have a drop down on the same form, and the time specified in this dropdown must be between the StartTime and EndTime, as selected earlier in the form.

this is what i have, is there a stupid bracket error somewhere? i am trying to use variables here.

//FIRST BREAK TIME VAL
var endt=document.Layer11.document.form1.EndingTime.value;
var startt=document.Layer11.document.form1.StartingTime.value;

var firstb=document.Layer11.document.form1.FirstBreakTime.value;
if (

((parseInt(firstb))<(parseInt(startt)))

||

((parseInt(firstb))>(parseInt(endt)))
) {
alert ("The time you selected for the First Break does not fall within the times selected chosen.");
f.FirstBreakTime.focus();
return false;
}

Jona
06-05-2003, 03:35 PM
//FIRST BREAK TIME VAL
var endt=parseInt(document.Layer11.document.form1.EndingTime.value);
var startt=parseInt(document.Layer11.document.form1.StartingTime.value);

var firstb=parseInt(document.Layer11.document.form1.FirstBreakTime.value);
if(firstb<startt||firstb>endt){
alert ("The time you selected for the First Break does not fall within the times selected chosen.");
f.FirstBreakTime.focus();
return false;
}


Jona

sanjuT
06-05-2003, 03:55 PM
thanks, i found out what was wrong. i changed what u supplied to this:

//FIRST BREAK TIME VAL
var endt=parseInt(f.EndingTime.options[f.EndingTime.selectedIndex].value);
var startt=parseInt(f.StartingTime.options[f.StartingTime.selectedIndex].value);

var firstb=parseInt(f.FirstBreakTime.options[f.FirstBreakTime.selectedIndex].value);
if((firstb<startt)||(firstb>endt)){
alert ("The time you selected for the First Break does not fall within the times selected chosen.");
f.FirstBreakTime.focus();
return false;
}

we forgot the 'options[f.EndingTime.selectedIndex].' part.

it works now, thanks again!

Jona
06-05-2003, 04:06 PM
Cool!

Jona