...

View Full Version : else return true; misplaced



Snitz
06-17-2009, 02:47 PM
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;
}

Philip M
06-17-2009, 04:45 PM
Two problems:-

email=document.quickbooking.email.value;

In Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or ID.

if (email=="")

This kind of "validation" is barely worthy of the name, as even a single space or a ? will return false (i.e pass the validation). Use a proper comprehensive validation instead.


var em=document.quickbooking.email.value;
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(em))) {
alert ("Invalid email address - please re-enter");
document.quickbooking.email.value = ""; // clear the field
document.quickbooking.email.focus(); // and refocus on it
return false;
}

return true is the default and there is no need to specify it.

“Expert: a man who makes three correct guesses consecutively.”
Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)

Snitz
06-18-2009, 07:28 AM
Two problems:-

email=document.quickbooking.email.value;

In Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or ID.

if (email=="")

This kind of "validation" is barely worthy of the name, as even a single space or a ? will return false (i.e pass the validation). Use a proper comprehensive validation instead.


var em=document.quickbooking.email.value;
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(em))) {
alert ("Invalid email address - please re-enter");
document.quickbooking.email.value = ""; // clear the field
document.quickbooking.email.focus(); // and refocus on it
return false;
}

return true is the default and there is no need to specify it.

“Expert: a man who makes three correct guesses consecutively.”
Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)
I replaced the whole function with yours and still it didn't work, it's still redirecting me to a blank page with "false" written on it after clicking "ok" on the alertbox.

Philip M
06-18-2009, 07:46 AM
Did the function doContinue() work properly before you added the email validation?

Snitz
06-18-2009, 07:48 AM
Actually, when I started working on this project the email validation was already there done by a previous developer and I'm continuing it.

Philip M
06-18-2009, 07:53 AM
Well, remove it temporarily and see what happens.

Snitz
06-18-2009, 07:58 AM
Well ye, I removed the "email validation" and ye it still works with no validation of course.

Philip M
06-18-2009, 08:07 AM
In that case separate out the email validation into a separate function.

Then onsubmit = "return (validateEmail() && doContinue())"
in other words, submit only if both functions return true.

But if it worked before under the previous developer I do not see why it does not work now.

Snitz
06-18-2009, 08:28 AM
In that case separate out the email validation into a separate function.

Then onsubmit = "return (validateEmail() && doContinue())"
in other words, submit only if both functions return true.

But if it worked before under the previous developer I do not see why it does not work now.
That didn't work either, here's how the 2 functions became after the split.
And I added the return onclick. And btw, the validation didn't work either with the previous developer, I thought you were talking about the whole form.


function ValidateEmail() {
var em=document.quickbooking.email.value;
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(em))) {
alert ("Invalid email address - please re-enter");
document.quickbooking.email.value = ""; // clear the field
document.quickbooking.email.focus(); // and refocus on it
return false;
}
}

function doContinue( )
{

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;
}
//-->

Philip M
06-18-2009, 10:36 AM
The problem lies within your function doContinue(). Your original post was misleading as it gave the impression that it was the added email validation which caused the error. Now you say it has never worked.

I haven't a clue what the problem with the function doContinue() is. Try the usual technique of cutting it in half, testing each part independently (with alerts to show what is happening) , cutting the halves in half again .....

Get rid of the //--> tag. It has not been required since IE4 and may perhaps cause a problem.

Snitz
06-18-2009, 10:46 AM
Ok, this is what I did.

I have split the function in half: validemail() and DoContinue() and I tested them.
The submit button of the form is validating perfectly with this
onclick="return validemail() && doContinue();"

The other link is validating that there is no email or the email is wrong and after you've clicked ok on the alertbox it redirects you to the next page instead of staying on the current.

This is the code of the link
<a href="javascript:validemail(); doContinue();" class="formLink">

I mean the DoContinue() function alone works great.

Philip M
06-18-2009, 11:11 AM
At last!

<a href="javascript:validemail(); doContinue();" ">

<a href = "#" class="formLink" onclick = "return validemail() && doContinue()">Click Here</a>

Snitz
06-18-2009, 11:16 AM
At last!

<a href="javascript:validemail(); doContinue();" ">

<a href = "#" class="formLink" onclick = "return validemail() && doContinue()">Click Here</a>
Finally!!! :D
Thank you very much Philip :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum