SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problems with if/else statement

    I'm writing a form validation function. All went well until I did this function:

    //check email field
    if(document.form1.email1.value == "")
    {
    alert('Please enter an Email address.');
    document.form1.email1.focus();
    return false;
    }
    else
    {
    var email1 = document.form1.email1.value;

    apos=email1.indexOf("@");
    dotpos=email1.lastIndexOf(".");
    lastpos=email1.length-1;
    if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
    {
    alert("The Email you entered is not valid. Please fix it.");
    document.form1.email1.focus();
    return false;
    }
    else
    {
    return true;
    }
    }

    //check adults field
    if(document.form1.adults.value == "")
    {
    alert('Please enter a number of adults.');
    document.form1.adults.focus();
    return false;
    }


    as you can see, after the if/else on the email check I continued to check the field called "adults" in my form, but for some strange reason it doesnt work (I tried with some other fields too, so its not a name clash).

    NOTE that when I put the "adults" field part:

    if(document.form1.adults.value == "")
    {
    alert('Please enter a number of adults.');
    document.form1.adults.focus();
    return false;
    }

    before the check email part (that uses if/else) it worked good. So I reccon something is wrong with my statement.

    thanks.

  2. #2
    SitePoint Zealot Atirez's Avatar
    Join Date
    Nov 2000
    Location
    England
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Not having much time, if your code resides in one function the return true statement in the email check would pull you out of the function before it checks anything after. So it wouldnt check your adults field.

    Although I could be wrong.

    Just a note though try placing alerts(); inside your code and running it while changing the contents of the fields so you can follow your code and make sure certain areas are being entered.

    e.g.

    //check email field
    if(document.form1.email1.value == "")
    {
    alert('Please enter an Email address.');
    document.form1.email1.focus();
    return false;
    }
    else
    {
    alert("Email has content!");
    ...

    Hope this helps.
    Atirez


Bookmarks

Posting Permissions

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