简体   繁体   中英

Checkbox validation in Javascript

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM