By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

The code inside the else block is not getting executed.

P: n/a
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

Sep 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Sep 20 '06 #2

P: n/a

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 '06 #3

P: n/a
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.
p.*****@ctncorp.com 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
Sep 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.