Hi,

I have a dynamically generated form, and each listing contains a checkbox named 'del'.
The users checks the 'del' checkbox next to each listing they wish to delete, and click the submit button. If there are multiple listings/checkboxes on the page, then the script I have works quite well, but if there is only one checkbox displayed on the page. If there is only one checkbox, and the user clicks the submit button, it still submits the form even if the single checkbox is not checked.

Here is my script:

<SCRIPT language="JavaScript1.2">
function valDelForm(theForm)
{
var filledIn = false;    

    // Use the length property to iterate through each Checkbox
    // to determine if a selection has been made
       for (var counter=0; counter<theForm.del.length; counter++)
       if (theForm.del[counter].checked == true){
       filledIn = true;
       
       //else{
       if (theForm.del.checked == true){
       filledIn = true;
       }
      // }
    }

       if (filledIn == false){
       alert('Please select a listing to delete!');
       return(false);
    }  

 return(true);
}
</script>

This is how the script is called from the opening form tag: onSubmit="return valDelForm(this)"

Recommended Answers

All 2 Replies

Shouldn't for (var counter=0; counter<theForm.del.length; counter++) be for (var counter=0; counter<theForm.del.length; counter++){ and return(true); should be return filledIn;

thanks buddylee....it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.