We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Limiting Number of Checked Checkboxes

brownmetals
brownmetals asked
on
Medium Priority
245 Views
Last Modified: 2008-03-04
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.
Comment
Watch Question

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
   }
}
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;
      }
  }
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
   }
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.