These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Limit the number of checkboxes selected

greenflash
Registered: Mar 24 2011
Posts: 5
Answered

I have 10 checkboxes on a form and I would like to limit the selection to a maximum of 3 out of 10. If the user attempts to check a 4th checkbox an app.alert() would be great!
 
I am 3 days into programming (never done it before) and I am still unclear about the correct way to use loops, counts and passing functions...It's been 3 days now and I now have a headache. My debugging is also non existent
  
I have been looking at lots of script, tryin glots of stuff, I am almost half way through the book ""Creating Adobe Acrobat Forms"-Ted Padova. I understand all that I have read so far...but cannot bend my mind around this one.
 
Check boxes are numbered:
Boox.0 ... box.9 (in think a table array would work so that I can "place multiple fields")
 
I think I need a function at document level and a script to be executed on the checkbox mouseup level.
 
Can anyone show and explain what I need to do?
   
Thanks

My Product Information:
Acrobat Pro Extended 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
Yes, that what you'd do. Here's a document-level script that you can try. Call it in the Mouse Up event of each check box:

  1. function limitCBs() {
  2.  
  3. // Initialize counter
  4. var sum = 0;
  5.  
  6. // Loop through the check boxes
  7. for (var i = 0; i < 10; i++) {
  8.  
  9. // Increment counter if check box is selected
  10. if (getField("box." + i).value !== "Off") sum++;
  11.  
  12. // If more than three are selected, deselect the one that was just selected,
  13. // alert the user, and return
  14. if (sum > 3) {
  15. event.target.value = "Off";
  16. app.alert("You may not select more than three, so stop trying!", 3);
  17. return;
  18. }
  19. }
  20. }
This assumes the check boxes are named box.0 ... box.9
greenflash
Registered: Mar 24 2011
Posts: 5
Hi George_Johnson,

Truly a legend. Your code worked without a glitch :-)

I was really frustrated that I was not able to crack it myself. But in the true spirit of trying to learn for myself I have been playing around with the code to enhance my level of understanding (3 days into doing my first programming).

The first part of the loop code is:

for (var i = 0; i < 10; i++) {action}I understand this a being:

for(first loop condition;last loop condition; update expression){action}

the loop {action} contains the following

if (getField("box." + i).value !== "Off") sum++;
if (sum > 3) {
event.target.value = "Off";
app.alert("You may not select more than three, so stop trying!", 3);
return;
}

Could this be expressed as:

if(condition){action1; if(condition){action1;action2;action3}}

Putting the two together

for(first loop condition;last loop condition; update expression){if(condition){action1; if(condition){action1;action2;action3}}}}

I pieced in your code and arrived at:

function limitCBs(){
// Initialize counter
var sum = 0;

// Loop through the check boxes
for (var i = 0; i < 10; i++) {// Increment counter if check box is selected
if(getField("box." + i).value !== "Off"){sum++;

//If more than three are selected, deselect the one that was just selected,
if(sum > 3){//return the event value to OFF
event.target.value = "Off";

//alert the user
app.alert("You may not select more than three, so stop trying!", 3);

//and return to the beginning of the loop
return}}}
}

IT WORKED AND I UNDERSTAND which is most important. Oh Yea! I loved the little ,3 snuck onto to the end of the app.alert. I was wondering what that did...and through deduction realised that it changed the style of the app.alert...there is a beauty in coding and I need to learn more.

Thanks for enhancing my knowledge.

Kind regards

Greenflash
greenflash
Registered: Mar 24 2011
Posts: 5
Wow,

The more I play with this one the better it gets. I liked the way you Incremented the counter if a checkbox is selected by using the "off" state !== "Off", which leaves me free to pass a value when the checkbox is checked; neat. I was originally incrementing the counter on the "Yes" state. Your way makes perfect sense.

why !=="Off" and not !=Off as I read somewhere that == are used to compare the results of variables. However,
! = "Off" did actually work in the script.


FranChessie
Registered: May 20 2011
Posts: 2
I have a similar issue and have no coding knowledge at all. I have 17 items and only want to be able to choose 5. I tried modifying your script for the selection of 5 but I'm sure I'm missing something else.

Also, does it matter what order or how the checkbox is created?

Thanks for any help.

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Can you post the code you're using? The order that the fields were created does not matter, but it would matter if you didn't create the form in Acrobat, and instead used LiveCycle Designer.
FranChessie
Registered: May 20 2011
Posts: 2
All in Acrobat. I've never even heard of LiveCycle Designer. :)

I was using the code you gave the guy above and modifying to suit but it wasn't working. I finally got my code guy here at work and he has solved the problem. I had the name of my check boxes wrong and didn't realized the format needed for scripting (too many extra lines & spaces, etc).Thanks!