Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 10-15-2008, 10:03 AM Problem with Loop..
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Hello.

I have a form validation assignment that I'm working on. On this form, I have a section that has 4 checkboxes. I'm trying to validate that at least one of the checkboxes has been selected. If not, then it displays an error message and doesn't submit (I've got this part handled).

The problem I'm having is that my loop is only checking for the first box option only and none of the others. I can't figure out why for the life of me.

Code:
function checkCheckedOpts(check_boxes, error){

 for(var i=0; i < check_boxes.length;)
 {
   var ones_chkd = check_boxes[i++].checked;

   if(!ones_chkd)
   {error.val=5;return false;}
   else
   {return true;}
 }
}
Firebug is showing that the variable 'check_boxes' has all of the options associated with it. Nothing wrong there. However, it's not looping through all of them to check if something was selected. 'ones_chkd' is only true if the first option is selected...why?...
LayneMitch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-15-2008, 10:28 AM Re: Problem with Loop..
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
It's not looping through them all because you are returning true or false based on whether each checkbox is selected or not. As soon as you use the 'return' command, the function will exit. Therefore, you don't make it past the first cycle in the loop.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-15-2008, 10:38 AM Re: Problem with Loop..
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You'd be better off doing something like this:
Code:
function checkCheckedOpts(check_boxes, error){
var bool=false;
 for(var i=0; i < check_boxes.length; i++)
 {
   var ones_chkd = check_boxes[i].checked;

   if(ones_chkd)
   {bool=true;}
 }
return bool;
}
This way, the default (bool=false) will be returned if none of them show up checked, and if any of them are, bool gets set to true. I assume that var check_boxes is already set, and is global, though you didn't include that part.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 10-15-2008 at 10:40 AM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-16-2008, 09:22 AM Re: Problem with Loop..
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
You'd be better off doing something like this:
Code:
function checkCheckedOpts(check_boxes, error){
var bool=false;
 for(var i=0; i < check_boxes.length; i++)
 {
   var ones_chkd = check_boxes[i].checked;

   if(ones_chkd)
   {bool=true;}
 }
return bool;
}
This way, the default (bool=false) will be returned if none of them show up checked, and if any of them are, bool gets set to true. I assume that var check_boxes is already set, and is global, though you didn't include that part.
Sorry for the late response. Thanks...it worked.
LayneMitch is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Problem with Loop..
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14059 seconds with 12 queries