The code inside the else block is not getting executed.

D

divya

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
 
D

divya

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

p.lepin

divya said:
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.
 
D

divya

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.


divya said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top