The code inside the else block is not getting executed.

Discussion in 'Javascript' started by divya, Sep 20, 2006.

  1. divya

    divya Guest

    Hiii,
    Please read the following piece of code:-


    function SendToWebServer1(sendto) //sendto string contains a URL
    {

    if (document.form1.txtbookedby.value == "")
    {
    alert('your name field cannot be empty');
    }
    else if (document.form1.txtContact.value == "")
    {
    alert('The extension number field cannot be empty');
    }
    /* else if (document.form1.txtContact.value != "") //To chk user has
    added only digits
    {
    for (var i = 0; i <
    document.form1.txtContact.value.length;i++){
    var ch=document.form1.txtContact.value.charAt(i);
    alert (i);
    if((ch < "0" || ch > "9")){
    alert('the extension field can only have numbers');
    }
    }
    alert("successfully come out of loop");
    }*/
    else
    {
    alert("hey youve filled alll the fields correct ");
    document.form1.action=sendto;
    document.form1.submit();
    }
    }


    Now the problem is with the part of code I have commented .

    Problem:-
    When I remove the coments ,it properly checks the first three
    conditions and gives respective alert boxes when the conditions are
    true.But when all the 3 conditions are false (that is conditions given
    in the if ,1st elseif and 2nd elseif ) it doesnot go inside the Else
    block.

    Now when I add coments to the 2nd else if block ,it works fine .When
    the if and the first elseif are false,it goes into the else block
    displays the alert box and also performs these actions.
    document.form1.action=sendto;
    document.form1.submit();
    as needed and takes me to the URL given in "sendto".

    But when I remove the coments and all the fields are properly filled
    it doesnot direct me to the string"sendto" page.

    Please let me know where is the problem in the code.

    Regards
    Divya
     
    divya, Sep 20, 2006
    #1
    1. Advertising

  2. divya

    divya Guest

    Yep I got my mistake. Because the second Elseif condition is satisfied
    it enters inside executes the block and hence else is not executed.

    divya wrote:
    > Hiii,
    > Please read the following piece of code:-
    >
    >
    > function SendToWebServer1(sendto) //sendto string contains a URL
    > {
    >
    > if (document.form1.txtbookedby.value == "")
    > {
    > alert('your name field cannot be empty');
    > }
    > else if (document.form1.txtContact.value == "")
    > {
    > alert('The extension number field cannot be empty');
    > }
    > /* else if (document.form1.txtContact.value != "") //To chk user has
    > added only digits
    > {
    > for (var i = 0; i <
    > document.form1.txtContact.value.length;i++){
    > var ch=document.form1.txtContact.value.charAt(i);
    > alert (i);
    > if((ch < "0" || ch > "9")){
    > alert('the extension field can only have numbers');
    > }
    > }
    > alert("successfully come out of loop");
    > }*/
    > else
    > {
    > alert("hey youve filled alll the fields correct ");
    > document.form1.action=sendto;
    > document.form1.submit();
    > }
    > }
    >
    >
    > Now the problem is with the part of code I have commented .
    >
    > Problem:-
    > When I remove the coments ,it properly checks the first three
    > conditions and gives respective alert boxes when the conditions are
    > true.But when all the 3 conditions are false (that is conditions given
    > in the if ,1st elseif and 2nd elseif ) it doesnot go inside the Else
    > block.
    >
    > Now when I add coments to the 2nd else if block ,it works fine .When
    > the if and the first elseif are false,it goes into the else block
    > displays the alert box and also performs these actions.
    > document.form1.action=sendto;
    > document.form1.submit();
    > as needed and takes me to the URL given in "sendto".
    >
    > But when I remove the coments and all the fields are properly filled
    > it doesnot direct me to the string"sendto" page.
    >
    > Please let me know where is the problem in the code.
    >
    > Regards
    > Divya
     
    divya, Sep 20, 2006
    #2
    1. Advertising

  3. divya

    Guest

    divya wrote:
    > if (document.form1.txtbookedby.value == "")
    > {
    > alert('your name field cannot be empty');
    > }
    > else if (document.form1.txtContact.value == "")
    > {
    > alert('The extension number field cannot be empty');
    > }
    > else if (document.form1.txtContact.value != "")
    > {


    [...]

    > }
    > else
    > {
    > alert("hey youve filled alll the fields correct ");


    [...]

    > }


    > When I remove the coments ,it properly checks the first
    > three conditions and gives respective alert boxes when
    > the conditions are true.But when all the 3 conditions are
    > false (that is conditions given in the if ,1st elseif and
    > 2nd elseif )...


    You're mistaken. 'A or not A' can't *ever* be false (if you
    stick to binary logic, of course). The condition in the
    second else if is a negation of a condition in the first
    else if. They can't both be false at the same time.

    --
    Pavel Lepin
     
    , Sep 20, 2006
    #3
  4. divya

    divya Guest

    Yes thats what clicked me after posting that at a time one of the
    first elseif or second elseif has to be true and in either cases Else
    will never be executed.Anewayz thanks for replying.


    wrote:
    > divya wrote:
    > > if (document.form1.txtbookedby.value == "")
    > > {
    > > alert('your name field cannot be empty');
    > > }
    > > else if (document.form1.txtContact.value == "")
    > > {
    > > alert('The extension number field cannot be empty');
    > > }
    > > else if (document.form1.txtContact.value != "")
    > > {

    >
    > [...]
    >
    > > }
    > > else
    > > {
    > > alert("hey youve filled alll the fields correct ");

    >
    > [...]
    >
    > > }

    >
    > > When I remove the coments ,it properly checks the first
    > > three conditions and gives respective alert boxes when
    > > the conditions are true.But when all the 3 conditions are
    > > false (that is conditions given in the if ,1st elseif and
    > > 2nd elseif )...

    >
    > You're mistaken. 'A or not A' can't *ever* be false (if you
    > stick to binary logic, of course). The condition in the
    > second else if is a negation of a condition in the first
    > else if. They can't both be false at the same time.
    >
    > --
    > Pavel Lepin
     
    divya, Sep 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sk
    Replies:
    1
    Views:
    438
    Karl Seguin [MVP]
    Apr 19, 2006
  2. Michal Wyrebski
    Replies:
    6
    Views:
    481
    Krishanu Debnath
    Jun 2, 2005
  3. Neroku
    Replies:
    9
    Views:
    1,021
    Chris Uppal
    Feb 7, 2007
  4. morrell
    Replies:
    1
    Views:
    965
    roy axenov
    Oct 10, 2006
  5. Mike Austin
    Replies:
    18
    Views:
    221
    Mike Austin
    Mar 10, 2006
Loading...

Share This Page