0

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>
3
  • Why can't you use JQuery? What are the limitations? Just curious. Commented Jan 5, 2011 at 2:28
  • What are your certain limitations for not using jQuery? Commented Jan 5, 2011 at 2:28
  • I am using this scripts on Blackbery 5 and Blackberry 5 does not supports JQUERY we are using Rhodes framework Commented Jan 5, 2011 at 16:34

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

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
-1
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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.