Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > The code inside the else block is not getting executed.

Reply
Thread Tools

The code inside the else block is not getting executed.

 
 
divya
Guest
Posts: n/a
 
      09-20-2006
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

 
Reply With Quote
 
 
 
 
divya
Guest
Posts: n/a
 
      09-20-2006
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


 
Reply With Quote
 
 
 
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      09-20-2006

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

 
Reply With Quote
 
divya
Guest
Posts: n/a
 
      09-20-2006
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.


http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the use of the else in try/except/else? kj Python 15 05-23-2009 02:18 AM
else block is not processed shanutallfat@gmail.com Java 10 07-27-2008 03:47 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
for: else: - any practical uses for the else clause? metaperl.etc@gmail.com Python 25 09-30-2006 11:01 PM



Advertisments