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.
LVL 4
brownmetalsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.