Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Limiting Number of Checked Checkboxes

I have a script I'm using to allow users to select several records using a checkbox. This will take those selected records and convert them to PDF files. I want to limit the number of items to be converted at one time to 5 to prevent server issues. Right now I have some code that takes the value of each checkbox, and sends it to a new window that opens. I'd like to figure out a way to limit the number of boxes checked to 5.

Here's what I am using thus far:

HTML Form Name: pdfForm
Checkbox series named: checkPDF


function passLotHist(dml,chkName){
lotStr = ''
dml=document.forms['pdfForm'];
len = dml.elements.length;
var i=0;
for( i=0 ; i<len ; i++) {
if ((dml.elements[i].name==chkName) && (dml.elements[i].checked==1))
   {
   //alert(dml.elements[i].value);
   lotStr = dml.elements[i].value +"," + lotStr ;
   }
}
if (lotStr=='')
{
 alert("Please check at least one checkbox to Create a PDF for Email");
 }
 else
 {
      var url = "createPDFEmail.asp?checkPDF=" + lotStr ;
      window.open(url,'','scrollbars=yes,width=700,height=500');
}
return false;
}


How could I limit the number of boxes checked to 5? Would it make sense to call this function and include that logic, or call a separate logic? Is the onClick even for the checkbox a good place to call the function? I'm thinking it would probably be best to call it when the form is submitted. Any suggestions would be appreciated.
0
brownmetals
Asked:
brownmetals
  • 3
1 Solution
 
RainMan82Commented:
add this to your code.....gives you a count of the # of checkboxes that are checked....

function Test(){
var count_v;

for (i=0;i<document.formname.elements.length;i++) {
  if document.formname.elements[i].type=="text") {
    //do addition of value here
      if (document.formname.elements[i].checked == true){
         count_v = count_v + 1;
      }
  }

   if (count_v > 5){
     alert("can only have 5 checkboxes checked at one time");
   }
   else
   {
      //open pdf's
   }
}
0
 
RainMan82Commented:
sorry....change text to checkbox in the code below....

for (i=0;i<document.formname.elements.length;i++) {
  if document.formname.elements[i].type=="checkbox") {
    //do addition of value here
      if (document.formname.elements[i].checked == true){
         count_v = count_v + 1;
      }
  }
0
 
RainMan82Commented:
here it is...

function Test(){
var count_v;

for (i=0;i<document.formname.elements.length;i++) {
  if document.formname.elements[i].type=="checkbox") {
    //do addition of value here
      if (document.formname.elements[i].checked == true){
         count_v = count_v + 1;
      }
  }
}

   if (count_v > 5){
     alert("can only have 5 checkboxes checked at one time");
   }
   else
   {
      //open pdf's
   }
}
0
 
brownmetalsAuthor Commented:
Hi RainMan.

Thanks for your input. That was almost there. I was able to adapt your suggestion for my application. The other condition to check for which was in the code in my original post was to make sure the user checked at least 1 checkbox.

I used the following and I was able to get it to work:

function passLotHist(dml,chkName){
lotStr = ''
countChecks = 0
dml=document.forms['pdfForm'];
len = dml.elements.length;
var i=0;
for( i=0 ; i<len ; i++) {
if ((dml.elements[i].name==chkName) && (dml.elements[i].checked==1))
   {
      countChecks = countChecks + 1;
   lotStr = dml.elements[i].value +"," + lotStr ;
   }
}
if ((lotStr=='') || (countChecks > 5))
{
 msg = "Error: You must select:\n"
 msg += "• At lease one (1) test report\n"
 msg += "• No more than five (5) test reports"
 alert(msg);
 }
 else
 {
      var url = "createPDFEmail.asp?checkPDF=" + lotStr ;
      window.open(url,'','scrollbars=yes,width=700,height=500');
}
return false;
}

Thanks for the input.

J
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now