[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

javascript logic

Posted on 2004-11-17
9
Medium Priority
?
180 Views
Last Modified: 2010-04-17
I want to modify the following js code to reverse the logic where if the conditions are met it will drop to the next statement.

 
var box = document.form1.zipselect;
 for(i=0;i<box.length;i++)
   if(box[i].checked == true)
            return true;
 alert("You must select at lease one region.")
 return false;
} //end validate
0
Comment
Question by:webdork
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 12605338
var box = document.form1.zipselect;
 for(i=0;i<box.length;i++)
   if(box[i].checked != true)
          return true;
 alert("You must select at lease one region.")
 return false;
} //end validate
0
 

Author Comment

by:webdork
ID: 12605500
Even I'm smart enough (that's not saying much) to know that's not right.

It should look something like the code below with the return true; at the very end of all statments: But the following gives me an alert for every single checkbox, and only validates if any box is unchecked.

 var box = document.form1.zipselect;
 for(i=0;i<box.length;i++)
   if(box[i].checked != true)
      alert("You must select at lease one region.")    
      return false;
} //end zipselect validate

//next statment
return true
0
 
LVL 35

Accepted Solution

by:
TimYates earned 2000 total points
ID: 12605557
Do you mean like:

   var box = document.form1.zipselect;
   atLeastOneSelected = false ;
   for( i = 0 ; i < box.length ; i++ )
   {
      if(box[i].checked == true)
      {
         atLeastOneSelected = true ;
         break ;
      }
   }
   if( !atLeastOneSelected )
   {
      alert("You must select at least one region.")    
      return false;
   }

//end zipselect validate
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 48

Expert Comment

by:Mikal613
ID: 12605576
for (i=0;i<box.length;i++)   {
  if(box[i].checked != true)
      alert("You must select at lease one region.")    
         }


return false;

0
 
LVL 7

Expert Comment

by:smolam
ID: 12609755
Or this
var box = document.form1.zipselect;
for (i=0;i<box.length;i++)   {
      if(box[i].checked != true){
            alert("You must select at lease one region.");
            return false;
            break;
      }else{
            return true;
      }
}

but  TimYates solution will work just as well.
0
 
LVL 3

Expert Comment

by:baboo_
ID: 12615808
Or even this!


var box = document.form1.zipselect;
var i = 0;
while ( box[i].checked != true && i++ < box.length );
return ( i < box.length );


No, seriously - don't use this - it's intentionally hideous. (but it *will* work) ;\

baboo_
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12615888
smolam, will your one actually work?

Isn't your code "all boxes MUST be checked" rather than "AT LEAST ONE box must be checked"
0
 
LVL 7

Expert Comment

by:smolam
ID: 12616405
Ummmm Nope haha, I actually did it late last night and when I woke up I looked at it and thought I had messed it up, hoped noone would notice LOL.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12623006
;-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question