User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 429,899 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,308 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2216 | Replies: 2 | Solved
Reply
Join Date: Mar 2008
Posts: 7
Reputation: infernodeep is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
infernodeep infernodeep is offline Offline
Newbie Poster

Single / Multiple Checkbox Validation

  #1  
Jun 2nd, 2008
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)"
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Arkansas
Posts: 399
Reputation: buddylee17 will become famous soon enough buddylee17 will become famous soon enough 
Rep Power: 2
Solved Threads: 78
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Whiz

Re: Single / Multiple Checkbox Validation

  #2  
Jun 2nd, 2008
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;
Reply With Quote  
Join Date: Mar 2008
Posts: 7
Reputation: infernodeep is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
infernodeep infernodeep is offline Offline
Newbie Poster

Re: Single / Multiple Checkbox Validation

  #3  
Jun 3rd, 2008
thanks buddylee....it works
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC