Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts

    else return true; misplaced

    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

    Code:
    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;
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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.

    Code:
    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)
    Last edited by Philip M; 06-17-2009 at 04:49 PM.

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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.

    Code:
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Did the function doContinue() work properly before you added the email validation?

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Actually, when I started working on this project the email validation was already there done by a previous developer and I'm continuing it.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Well, remove it temporarily and see what happens.

  • #7
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Well ye, I removed the "email validation" and ye it still works with no validation of course.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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.

  • #9
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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.

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

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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.

  • #11
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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
    Code:
    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
    Code:
    <a href="javascript:validemail(); doContinue();" class="formLink">
    I mean the DoContinue() function alone works great.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    At last!

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

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

  • Users who have thanked Philip M for this post:

    Snitz (06-18-2009)

  • #13
    Regular Coder
    Join Date
    Jul 2004
    Location
    Lebanon
    Posts
    223
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    At last!

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

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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •