Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello All I am having 2 groups of checkboxes which are been generated dynamically through java depending the code generator tool generate the following HTML

I am having the following JS to validate that atleast one of the checkbox is been selected from the each row which is not working I know if we give the same name it would work , just wanted to check if there is any work around for this with the name been changed

I cannot use JQUERY due to certain limitations

function validate()
{
      var e = document.form.elements;
    for ( var elem, i = 0; ( elem = e[i] ); i++ )
    {
         if (  elem.type == 'checkbox' )
        {
            if (!checkCheckBox (form, elem))
            {
                alert('Please check atleast one checkbox.');
                return false;
            } 
        } 
     } 
    document.form.submit();
    return true;
} 

 function checkCheckBox (form, elem)
{
     var check= form.elements[elem.name];
     var flag = false;
     for (var i=0; i <check.length; i++)
    {
        //alert(" radios[i].checked "+elem[i].checked);
        if (check[i].checked)
        {
             flag = true;
             break;
        }
    }
}





    <form name="form"> 
<table>
 <tr bgcolor='lightgray' width='100%' colspan='3'><td>KS3 QCheckbox 1</td></tr><tr><td>
<input type="checkbox" name="form[checkbox][KS31][KS31 1][]" id="COption 1" value="Option 1" /> 
<input type="checkbox" name="form[checkbox][KS31][KS32 1][]" id="COption 2" value="Option 2" /> 
<input type="checkbox" name="form[checkbox][KS31][KS33 1][]" id="COption 3" value="Option 3" /> 

<tr bgcolor='lightgray' width='100%' colspan='3'><td>KS3 QCheckbox 2</td></tr><tr><td>
<input type="checkbox" name="form[checkbox][KS32][KS31 2][]" id="COption 1" value="Option 1" /> 
<input type="checkbox" name="form[checkbox][KS32][KS32 2][]" id="COption 2" value="Option 2" /> 
<input type="checkbox" name="form[checkbox][KS32][KS33 2][]" id="COption 3" value="Option 3" /> 

<input type="submit" onClick="validate()">
</table>
</form>
share|improve this question
    
Why can't you use JQuery? What are the limitations? Just curious. –  jmort253 Jan 5 '11 at 2:28
    
What are your certain limitations for not using jQuery? –  Marcus Whybrow Jan 5 '11 at 2:28
    
I am using this scripts on Blackbery 5 and Blackberry 5 does not supports JQUERY we are using Rhodes framework –  Ameya Thakur Jan 5 '11 at 16:34
add comment

2 Answers

I think your checkCheckBox function must return the contents of the variable "flag":

function checkCheckBox (form, elem)
{
   var check= form.elements[elem.name];
   var flag = false;
   for (var i=0; i <check.length; i++)
   {
     //alert(" radios[i].checked "+elem[i].checked);
     if (check[i].checked)
        {
         flag = true;
         break;
        }
   }
   return flag;  // return true or false
}
share|improve this answer
    
The problem here is the grouping of the checkbox I want to validate the 1st Group of checkbox which are defined by name <br> form[checkbox][KS31] so that atleast one of the checkbox is selected the problem with the above loop is that it would iterate through all the checkboxes and if any one of the checkbox is not checked it would still throw the exception that you need to select atleast one checkbox –  Ameya Thakur Jan 5 '11 at 16:36
add comment
var chk = document.getElementsByName('checkbox_name[]');
   var len = chk.length;
   var has_program = false;
 for(i=0;i<len;i++) {
     if(chk[i].checked) {
        has_program = true;
        break;    
      } 
}
 if( !has_program )
    {
           alert("field with * is required");
         return false;
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.