javascript logic

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
webdorkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
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
webdorkAuthor Commented:
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
TimYatesCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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


return false;

0
smolamCommented:
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
baboo_Commented:
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
TimYatesCommented:
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
smolamCommented:
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
TimYatesCommented:
;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.