[Javascript] Checkbox validation

Cutter (JS) java.script at seacrets.com
Wed Jan 4 09:50:34 CST 2006


OK, strange one here. Writing some custom form validation scripting on 
some dynamically created forms. Currently I have a form where a user can 
delete files from the system. They have the ability to check a checkbox 
(name='deletefiles' value=[filename]) for each file they wish to remove. 
When the form is submitted a formValidate() function is called and, in 
this instance, an isRequiredCheckbox(fldObj) function is called. This is 
to verify that at least one selection has been made. This works great if 
there are multiple files (which, in turn, means more than one checkbox), 
but doesn't work properly when there is only one file (and one 
checkbox)? Below is the basic code affected. Any ideas would be appreciated.

Cutter

//first of all you must know that the fldObj object has a property 
'FieldName' that is the form field object
// as in fldObj.FieldName = document.forms.myform.mycheckboxfield;
function isRequiredCheckbox(fldObj){
    valCheck = 0;
    selChoices = -1;
    for (counter = 0; counter < fldObj.FieldName.length; counter++){
       if (fldObj.FieldName[counter].checked) {
          selChoices = selChoices + 1;
       }
    }
    if (selChoices < 0){
       alert('You have not made any selections.');
    } else {
       valCheck = 1;
    }
    return valCheck;
}



More information about the Javascript mailing list