Check box validation

Validate a list of checkboxes before submitting a form, even if there is only one checkbox in the list

 

/**
 * @param fields is a  checkbox list element like: 
* document.getElementById('frmTest').elements['checkboxList[]'])
 */  
function  check_checkboxes(fields)
{    
   // There is only one checkbox
     if (fields.length == undefined)
    {        
        if(fields.checked==false)
        {           
            return  false;
        }
        else
        {                                
            return true;             
        }       
    }
    
    // there is a list of  checkboxes
    for (i = 0; i < fields.length; i++)
      {
         if(fields[i].checked == true)
        {           
            return true;                    
        }
      }                
      return false;
}