PDA

View Full Version : Form Validation


sunnyview
08-28-2002, 07:40 AM
Thanks to glenngv and whammy with your previous help with this script. It's working fine now, except that it validates from the bottom upwards!

I have no idea how to correct this, except by reversing the order of the validation within the script.

I would appreciate a detailed reply, as I am new to JavaScript and am currently struggling. Thanks in anticipation.

<script language="JavaScript">
<!--
// Function to validate the form.
function ProcessForm()
{

var Proceed = 1;
var Message;
var FocusField;


//Airport Drop-off --- Airport Pick-up --- Airport Pick-up
//Airport Drop-off --- Airport Pick-up --- Airport Pick-up


if (document.booking.pickup[1].checked)
{
if (!document.booking.Fltno.value)
{
Message = "Please enter your flight number.";
FocusField = "Fltno";
Proceed = 0;
}
}


if (document.booking.pickup[1].checked)
{
if (!document.booking.pickuptime.value)
{
Message = "Please enter your scheduled NZ pick-up time.";
FocusField = "pickuptime";
Proceed = 0;
}
}


if (document.booking.pickup[1].checked)
{
if (!document.booking.pickupdate.value)
{
Message = "Please enter your scheduled NZ pick-up date.";
FocusField = "pickupdate";
Proceed = 0;
}
}


if (document.booking.pickup[1].checked)
{
if (!document.booking.group.value)
{
Message = "Please enter your scheduled NZ pick-up date.";
FocusField = "group";
Proceed = 0;
}
}



//Airport Drop-off --- Airport Drop-off - Airport Drop-off
//Airport Drop-off --- Airport Drop-off - Airport Drop-off


if (document.booking.dropoff[1].checked)
{
if (!document.booking.Fltno2.value)
{
Message = "Please enter your return flight number.";
FocusField = "Fltno2";
Proceed = 0;
}
}


if (document.booking.dropoff[1].checked)
{
if (!document.booking.dropofftime.value)
{
Message = "Please enter your scheduled NZ drop-off time.";
FocusField = "dropofftime";
Proceed = 0;
}
}


if (document.booking.dropoff[1].checked)
{
if (!document.booking.dropoffdate.value)
{
Message = "Please enter your scheduled NZ drop-off date.";
FocusField = "dropoffdate";
Proceed = 0;
}
}


//Renters Details --- Renters Details --- Renters Details
//Renters Details --- Renters Details --- Renters Details


if (!document.booking.Surname.value)
{
Message = "Please enter your Surname.";
FocusField = "Surname";
Proceed = 0;
}


if (!document.booking.Firstname.value)
{
Message = "Please enter your Firstname.";
FocusField = "Firstname";
Proceed = 0;
}


if (!document.booking.DOB.value)
{
Message = "Please enter your Date of Birth.";
FocusField = "DOB";
Proceed = 0;
}


if (!document.booking.Homeaddress.value)
{
Message = "Please enter your Home Address.";
FocusField = "Homeaddress";
Proceed = 0;
}


if (!document.booking.driversln.value)
{
Message = "Please enter your Driver's Licence No.";
FocusField = "driversln";
Proceed = 0;
}


if (!document.booking.issuing.value)
{
Message = "Please enter the Issuing Country or State of your Driver's Licence.";
FocusField = "issuing";
Proceed = 0;
}


if (!document.booking.expiry.value)
{
Message = "Please enter the expiry date of your Driver's Licence.";
FocusField = "expiry";
Proceed = 0;
}


if (Proceed == 1)
{
alert("The form has been successfully completed.");
return true;
}
else
{
alert( Message );
if (FocusField != "")
{
eval("document.booking." + FocusField + ".focus()");
}
return false;
}
}

//-->
</script>

glenngv
08-28-2002, 07:55 AM
is the order of fields in the function and in the form the same?

sunnyview
08-28-2002, 08:25 AM
Yes

glenngv
08-28-2002, 09:30 AM
put else in your if conditions so that it will go to only one condition. also you've got some conditions like if (document.booking.pickup[1].checked) repeatedly being checked. here's the modified code:

function ProcessForm()
{

var Proceed = 1;
var Message;
var FocusField;


//Airport Drop-off --- Airport Pick-up --- Airport Pick-up
//Airport Drop-off --- Airport Pick-up --- Airport Pick-up


if (document.booking.pickup[1].checked)
{
if (!document.booking.Fltno.value)
{
Message = "Please enter your flight number.";
FocusField = "Fltno";
Proceed = 0;
}
else if (!document.booking.pickuptime.value)
{
Message = "Please enter your scheduled NZ pick-up time.";
FocusField = "pickuptime";
Proceed = 0;
}
else if (!document.booking.pickupdate.value)
{
Message = "Please enter your scheduled NZ pick-up date.";
FocusField = "pickupdate";
Proceed = 0;
}
else if (!document.booking.group.value)
{
Message = "Please enter your scheduled NZ pick-up date.";
FocusField = "group";
Proceed = 0;
}

}



//Airport Drop-off --- Airport Drop-off - Airport Drop-off
//Airport Drop-off --- Airport Drop-off - Airport Drop-off


else if (document.booking.dropoff[1].checked)
{
if (!document.booking.Fltno2.value)
{
Message = "Please enter your return flight number.";
FocusField = "Fltno2";
Proceed = 0;
}
else if (document.booking.dropoff[1].checked)
{
if (!document.booking.dropofftime.value)
{
Message = "Please enter your scheduled NZ drop-off time.";
FocusField = "dropofftime";
Proceed = 0;
}
else if (!document.booking.dropoffdate.value)
{
Message = "Please enter your scheduled NZ drop-off date.";
FocusField = "dropoffdate";
Proceed = 0;
}

}


//Renters Details --- Renters Details --- Renters Details
//Renters Details --- Renters Details --- Renters Details


else if (!document.booking.Surname.value)
{
Message = "Please enter your Surname.";
FocusField = "Surname";
Proceed = 0;
}


else if (!document.booking.Firstname.value)
{
Message = "Please enter your Firstname.";
FocusField = "Firstname";
Proceed = 0;
}


else if (!document.booking.DOB.value)
{
Message = "Please enter your Date of Birth.";
FocusField = "DOB";
Proceed = 0;
}


else if (!document.booking.Homeaddress.value)
{
Message = "Please enter your Home Address.";
FocusField = "Homeaddress";
Proceed = 0;
}


else if (!document.booking.driversln.value)
{
Message = "Please enter your Driver's Licence No.";
FocusField = "driversln";
Proceed = 0;
}


else if (!document.booking.issuing.value)
{
Message = "Please enter the Issuing Country or State of your Driver's Licence.";
FocusField = "issuing";
Proceed = 0;
}


else if (!document.booking.expiry.value)
{
Message = "Please enter the expiry date of your Driver's Licence.";
FocusField = "expiry";
Proceed = 0;
}


if (Proceed == 1)
{
alert("The form has been successfully completed.");
return true;
}
else
{
alert( Message );
if (FocusField != "")
{
eval("document.booking." + FocusField + ".focus()");
}
return false;
}
}

sunnyview
08-28-2002, 10:08 AM
Thanks for your reply. I've tried this but the form does not validate at all now, it just sends. I can't paste in the rest of the form 'cause its too long. I've tried modifying some else and is statements but this makes it worse. Best I leave alone I think.
Any ideas?

glenngv
08-28-2002, 10:23 AM
why not copy the function and save it in a different filename?

sunnyview
08-28-2002, 11:53 AM
Yes, I have done this and it does not work. What I meant by too big was that I could not copy the entire document and post it to this forum.

glenngv
08-29-2002, 04:35 AM
if fixed the pairing of curly braces {}
i also put the indention to make it clearer
here's the code:


function ProcessForm()
{
var Proceed = 1;
var Message;
var FocusField;

//Airport Drop-off --- Airport Pick-up --- Airport Pick-up
//Airport Drop-off --- Airport Pick-up --- Airport Pick-up

if (document.booking.pickup[1].checked)
{
if (!document.booking.Fltno.value)
{
Message = "Please enter your flight number.";
FocusField = "Fltno";
Proceed = 0;
}
else if (!document.booking.pickuptime.value)
{
Message = "Please enter your scheduled NZ pick-up time.";
FocusField = "pickuptime";
Proceed = 0;
}
else if (!document.booking.pickupdate.value)
{
Message = "Please enter your scheduled NZ pick-up date.";
FocusField = "pickupdate";
Proceed = 0;
}
else if (!document.booking.group.value)
{
Message = "Please enter your scheduled NZ pick-up date.";
FocusField = "group";
Proceed = 0;
}
}

//Airport Drop-off --- Airport Drop-off - Airport Drop-off
//Airport Drop-off --- Airport Drop-off - Airport Drop-off

else if (document.booking.dropoff[1].checked)
{
if (!document.booking.Fltno2.value)
{
Message = "Please enter your return flight number.";
FocusField = "Fltno2";
Proceed = 0;
}
else if (document.booking.dropoff[1].checked)
{
if (!document.booking.dropofftime.value)
{
Message = "Please enter your scheduled NZ drop-off time.";
FocusField = "dropofftime";
Proceed = 0;
}
else if (!document.booking.dropoffdate.value)
{
Message = "Please enter your scheduled NZ drop-off date.";
FocusField = "dropoffdate";
Proceed = 0;
}
}
}

//Renters Details --- Renters Details --- Renters Details
//Renters Details --- Renters Details --- Renters Details

else if (!document.booking.Surname.value)
{
Message = "Please enter your Surname.";
FocusField = "Surname";
Proceed = 0;
}

else if (!document.booking.Firstname.value)
{
Message = "Please enter your Firstname.";
FocusField = "Firstname";
Proceed = 0;
}

else if (!document.booking.DOB.value)
{
Message = "Please enter your Date of Birth.";
FocusField = "DOB";
Proceed = 0;
}

else if (!document.booking.Homeaddress.value)
{
Message = "Please enter your Home Address.";
FocusField = "Homeaddress";
Proceed = 0;
}

else if (!document.booking.driversln.value)
{
Message = "Please enter your Driver's Licence No.";
FocusField = "driversln";
Proceed = 0;
}

else if (!document.booking.issuing.value)
{
Message = "Please enter the Issuing Country or State of your Driver's Licence.";
FocusField = "issuing";
Proceed = 0;
}

else if (!document.booking.expiry.value)
{
Message = "Please enter the expiry date of your Driver's Licence.";
FocusField = "expiry";
Proceed = 0;
}

if (Proceed == 1)
{
alert("The form has been successfully completed.");
return true;
}
else
{
alert( Message );
if (FocusField != "")
{
eval("document.booking." + FocusField + ".focus()");
}
return false;
}
}