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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    function validate()

    I am having a little trouble getting this function validate to work. I can get both if statements to work, just not at the same time. As it stands, just the fname and lname part works; however, if I remove that part, the address and email part will work. I would appreciate any help. Thanks.

    Code:
    function validate() {
    if (document.form1.fname.value.length > 1
    && document.form1.lname.value. length > 1 ) 
    return true;
    else {
    alert("Please enter First and Last Name.");
    return false;}
    
    if (document.form1.address.value.length > 1
    || document.form1.email.value. length > 1 ) 
    return true;
    else {
    alert("Please enter Address or Email.");
    return false;}
    }
    Here is the <form> attribute that I have.
    Code:
    <form name="form1" action="acknowledgement.html" method="get" 
    enctype="application/x-www-form-urlencoded" 
    onSubmit="return validate();">

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Logic problem. I think you're saying:
    Code:
    if ( fnameIsOk && lnameIsOkay )
      return true; // I'm done - don't check anything else
    else
      return false; // and alert
    
    if ( addressIsOk OR emailIsOk )
      return true; // I'm done - don't check anything else
    else
      return false; // and alert
    But what you want to say is:
    Code:
    if ( fnameIsNotOk )
      return false; // and alert bad fname
    else if ( lnameIsNotOk )
      return false; // and alert bad lname
    else if (addressIsNotOk && emailIsNotOk )
      return false; // and alert bad address/email combo
    else // all conditions passed
      return true;
    How does that look?

  • Users who have thanked tomws for this post:

    jwh500 (05-07-2008)

  • #3
    Regular Coder ghostz00's Avatar
    Join Date
    Aug 2006
    Posts
    128
    Thanks
    11
    Thanked 2 Times in 2 Posts
    When you use the return keyword (no matter what it returns) it stops the execution of the function. So when your first if statment returns true or false, the second if statement never gets executed.

    I'd probably loop through all of the inputs and when there is no value, log it and then at the end return the errors.
    Greg

  • #4
    New to the CF scene
    Join Date
    May 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks tomws, You are right on. This was for an assignment at school, and I ended up doing it just as you suggested shortly after I posted here. (It was due in one hour) I figured it out with trial and error, but your posted showed the logic of it thanks a lot. I appreciate it. -Jeff


  •  

    Posting Permissions

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