The code below is to validate the email field in my form, if the field left empty the javascript will alert "please fill in your email" and then it'll take you to a blank page that says "false". I don't want that to happen, I want them to re-enter their email after clicking the "OK" on the alterbox. I think it's something to do with my "else return true;" - it's a misplaced.

Thanks again :)

function doContinue( )
{

email=document.quickbooking.email.value;

	if (email=="")
	{
	alert("Please fill in your email");
	return false;
	}
	
	else if(email.indexOf ('@', 0) == -1 || email.indexOf ('.', 0) == -1)
	{
		alert("Please fill in your Email in the right format");
		return false; 
	}

xF = document.forms['quickbooking'];
xCountry = getListValue( xF.country );
xETADay = getListValue( xF.ETADay );
xETAMonth = getListValue( xF.ETAMonth );
xETAYear = getListValue( xF.ETAYear );
xETAHour = getListValue( xF.ETAHour );
xETAMinute = getListValue( xF.ETAMinute );
xETA = xETADay + '-' + xMonths[ parseInt( xETAMonth )-1 ] + '-' + xETAYear.substr(0,4) + '-' + xETAHour + '-' + xETAMinute;
xETTDay = getListValue( xF.ETTDay );
xETTMonth = getListValue( xF.ETTMonth );
xETTYear = getListValue( xF.ETTYear );
xETTHour = getListValue( xF.ETTHour );
xETTMinute = getListValue( xF.ETTMinute );
xETT = xETTDay + '-' + xMonths[ parseInt( xETTMonth )-1 ] + '-' + xETTYear.substr(0,4) + '-' + xETTHour + '-' + xETTMinute;
xWizardNumber = xF.wizard_number.value;
xName = xF.surname.value ;
xemail = xF.email.value ;
xParams = '&CTR=' + xCountry +'&Country=' + xCountry + '&ETA=' + xETA + '&ETT=' + xETT+ '&CNAM=' + xName + '&WIZ=' + xWizardNumber+'&SOR=0038780x'+'&email=' + xemail;
//url = 'http://book.rent-at-avis.com/avisonline/ibe.nsf/PrefillX?OpenAgent&ResStep=ReservationStep1&IBEOwner=EU&LNG=GB' + xParams;location.href = url;
url = 'index.php?option=com_content&view=article&id=5&Itemid=22' + xParams;location.href = url;
else return true;
}

Holic,

The line else return true; will never be executed because it is immediately preceded by location.href = url; , effectively ending the life of the current page.

The problem would appear not to be with this function but with the function to which it returns its result.

Personally, I would simply submit the quickbooking form with action="index.php" method="post" then all the form fields will be available to in php's $_POST. You can send the other name|value pairs by using hidden form fields.

Hence your form might look something like this:

<form action="index.php" method="post" onsubmit="return doContinue(this);">
<input type="hidden" name="option" value="com_content">
<input type="hidden" name="view" value="article">
<input type="hidden" name="id" value="5">
<input type="hidden" name="Itemid" value="22">
<input type="hidden" name="WIZ" value="xWizardNumber">
<input type="hidden" name="SOR" value="0038780x">
... all the other form fields .....

Note: The onsubmit function will return false to suppress form submission or true to allow it.

And the corresponding checker function would be:

function doContinue(form) {
	var email = form.email.value;
	if (email=="") {
		alert("Please fill in your email");
		return false;
	}
	else if(email.indexOf ('@', 0) == -1 || email.indexOf ('.', 0) == -1) {
		alert("Please fill in your Email in the right format");
		return false;
	}
	return true;
}

Unless there's something I missed, there's no point processing all those data fields in javascript just to build a URL. All values will be available in php's $_POST array, and you can let php build the composite strings ETA and ETT from the raw values.

By the way, by using a regular expression, you can perform much more thorough email string validation. Google eg "javascript email regexp" for examples.

Hope this helps

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.