tags:

views:

242

answers:

4

Hi guys i am creating a validation for checking if the user has selected at least one stand by checking it, if no stands are selected then an alert box should display telling the user to select at least one stand. my javascript function is triggered onsubmit.

my code is

function checkstanddocument(){
 ab = document.getElementById("repo_document_form").elements[['stands[]']];   
  for (var i = 0; i <= ab.length; i++){

    if (ab[i].checked == true){
      return true
    }    
 }  
   alert("Please select at least one stand");
   return false
}

I thought the system will only proceed to the alert box if no checkbox is checked, but it doesn't even reach the alert box even if you checked or did not check it, and i want it to display an error if there is no checkbox checked.. can anyone tell me what have i done wrong.please cause it looks 110% correct to me.

A: 
function checkstanddocument(){
 var hasOne = false;
 ab = document.getElementById("repo_document_form").elements[['stands[]']];   
  for (var i = 0; i < ab.length; i++){

    if (ab[i].checked == true){
      hasOne = true;
    }    
 }  

  if (hasOne == false) {

   alert("Please select at least one stand");
   return false

  } else {

   return true;

  }

}
code_burgar
+3  A: 

the condition in your for loop is wrong, use:

for(var i = 0; i < ab.length; i++) {

< and not <=

edit:

what’s this: document.getElementById("repo_document_form").elements[['stands[]']];?

that doesn’t make sense at all to me …
i suspect the error is somewhere around that line. (elements[[ looks really weird—is your loop entered at all?)

knittl
+1 agreed. There is definitely something weird going on there...
peirix
The loop is doing just fine i even counted the number and is displaying the number of array elements
Donald
@knittl, you're original suggestion fixes the problem 100%, < and not <=.
Mr. Smith
+1  A: 

The other answers are great, but they left out one thing. You gotta check to ensure that the ab is infact an array. If there is only 1 checkbox, it will come back as a singular HTML Element.

function checkstanddocument(){
 var hasOne = false;
 ab = document.getElementById("repo_document_form").elements[['stands[]']];   
 if (ab.length != null){
    for (var i = 0; i < ab.length; i++){

      if (ab[i].checked == true){
        hasOne = true;
      }    
    }
 }
 else hasOne = ab.checked;

  if (hasOne == false) {

   alert("Please select at least one stand");
   return false

  } else {

   return true;

  }

}
Zoidberg
You should add a break inside the if statement in the loop. No need to keep looping if you found 1 already.
epascarello
which is basically the same as returning true instantly, and thus making this the same function as Donald already is using...
peirix
A: 

I find that PrototypeJS makes this sort of thing very easy using the $$() function with a CSS selector to get the controls you need.

This has the advantage of only looping over as many checkboxes are actually found so you don't have to worry about all of the loop edge conditions.

This function will display an alert if none of the checkboxes are checked (or if no checkboxes are found) and will also return true or false depending on what it finds.

function checkstanddocument() {
    var ret = false;

    $$('#repo_document_form input[type="checkbox"][name="stands[]"]').each( function(checkbox) {
        if(checkbox.checked) {
            ret = true;
            throw $break;
        }
    });

    if(!ret) {
        alert('Please select at least one stand.');
    }

    return ret;
}
Mark Biek
I am using ruby on rails, anyway thanx..
Donald
Actually, PrototypeJS is just a Javascript library. There's no reason why you couldn't use it on a Rails project. Just download the prototype.js file and add a <script> tag in your HTML page header.
Mark Biek
but why use a js framework for such a simple task?
knittl
Because it lets you do more with less code. Think about all of the discussion revolving around the correctness of the for loop. All of that can be avoided with the function above. Take a look at this blog post from our fearless leader: http://www.codinghorror.com/blog/archives/001163.html
Mark Biek