Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to Limit the number of Checkboxes selections in Adobe Acrobat?

Contributor ,
May 07, 2025 May 07, 2025

Good day!

 

I have a fillable PDF with 7 checkboxes, namely CB1 to CB7.

 

I wish to limit the number of checkboxes selected to only 2. I searched YouTube for a solution and this is the script I should place in Custom Calculate script in the Text Field I created ...

if (event.source && /^cb\d$/.test(event,name)) {
var counter =0;
for (var i=1; i<=7; i++) {
of (this.getField("cb"+i).value!="Off")
counter++;
}
If (counter>2) (
app.alert("Error: Maximum 2 Checkboxes are allowed.");
this.resetForm([event.source.name]);
}
}

 

However, I am getting a syntax error. I hope someone can help me with this Java script.

 

Thank you in advance.

TOPICS
PDF forms
212
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
May 07, 2025 May 07, 2025

You can also use another approach, the user can select two checkboxes, the remaining checkboxes will become read-only to prevent further selection:

var boxes = [];
for (var i=1; i <=7; i++) {
 boxes.push(this.getField("CB" + i));}

var offBoxes = boxes.filter(function(f) {
 return f.valueAsString === "Off";});

var readonly = (offBoxes.length === 5);

offBoxes.forEach(function(f) {
 f.readonly = readonly;});

Just keep in mind when it comes to field names "cb" and "CB" are not the same because in your post you wrote "CB" and in script you used "cb".

 

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2025 May 07, 2025

There are issues with this script.  of in line 4 should be if.  The I in If in line 7 should be lower case, the last opening bracket in the same line should be a curly bracket, and on and on and on.  Did you copy and paste this script or retype it?  Details matter.  Check the console for errors until there are none and then try again:

https://pdfautomationstation.substack.com/p/the-javascript-console

Even after syntax errors corrected, the script will not work due to the logic.  Try this:

var counter =0;
for (var i=1; i<=7; i++) 
{
if (this.getField("cb"+i).value!="Off")
{counter++;}
}

if(counter>2)
{
app.alert("Error: Maximum 2 Checkboxes are allowed.");
this.resetForm(event.source.name)
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2025 May 07, 2025

Hi @Henry.Ong ,

 

+++ EDITED REPLY, I did not address an issue with how the regular expression is used

 

++ Adding to @PDF Automation Station  valuable guidance,

 

Below is the original script with the corrections made:

(event.value && /^cb\d$/.test(event.value)==true) ? event.value : this.resetForm([event.target.name])

var counter =0;
for (var i=1; i<=7; i++) 
if (this.getField("cb"+i).value !== "Off")
counter++;

if (counter>2) {
app.alert("Error: Maximum 2 Checkboxes are allowed.");
this.resetForm([event.target.name])
}


}

+++ EDITED ANSWER +++

 

The very first line of the script, in which a regular expression method is employed as === >>>  (event.source && /^cb\d$/.test(event.value), that is not working as intended.

 

Regular Expressions (RegEx)  with JavaScript could be fun to learn but also very tedious. Please get familiarized with the articles below:

 

 

On that very first line of the script see also the syntax : /^cb\d$/.test(event,name)  <<< ===  it has a coma when a period should be employed in the expression; it should read event.value

 

Declaring a condition with "event.source" in this case doesn't reset that text field if the user types in anything other than the values of "cb1" through "cb7". You should declare the logical statement using something like this:

 

(event.value && /^cb\d$/.test(event.value)==true) ? event.value : this.resetForm([event.target.name])

 

That said, you should also combine this script with some of Acrobat's built-in features. For instance, to avoid users typing unwanted extra data on that textfield, you may also limit the input to only a combination of 3 alphanumeric characters. See slide below:

 

regeEX.png

 

Additionally, in this other line of code see the comparison operator in use:  (this.getField("cb"+i).value!="Off")

 

While the != (not equal) comparison operator appears to function correctly, it is important to note that you are comparing an export value derived from the checked state of checkboxes as a text string.

 

Therefore, it may be more appropriate to utilize the !== operator with Acrobat JavaScript scripts, which checks for both value and type inequality. Employing it in your logical statements can enhance your script's ability to accurately assess the differences in string values.

 

The link below explain in greater context my point above. See @try67 answer on that thread:

 

 

On this other line : If (counter>2)  <<<=== already pointed out by @PDF Automation Station  "if" not "If"

 

Lastly, using the resetForm() method : this.resetForm([event.source.name]) , also observed by @PDF Automation Station , it won't work if you express it like that. If you're gonna employ the brackets use instead "event.target.name" inside the brackets, like so ===>>   this.resetForm([event.target.name])

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2025 May 07, 2025

You can also use another approach, the user can select two checkboxes, the remaining checkboxes will become read-only to prevent further selection:

var boxes = [];
for (var i=1; i <=7; i++) {
 boxes.push(this.getField("CB" + i));}

var offBoxes = boxes.filter(function(f) {
 return f.valueAsString === "Off";});

var readonly = (offBoxes.length === 5);

offBoxes.forEach(function(f) {
 f.readonly = readonly;});

Just keep in mind when it comes to field names "cb" and "CB" are not the same because in your post you wrote "CB" and in script you used "cb".

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 07, 2025 May 07, 2025

Thanks Nesa!

 

The script you have provided works ok. Unfortunately, if the user wishes to change his/her selected checkbox, the script doesn't allow me to do this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2025 May 07, 2025

The user would have to uncheck one of the boxes that are check before checking another.  The script I provided pops the warning you want then clears the check box like you were trying to do.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2025 May 07, 2025

You just need to uncheck the checkbox, and then you can select another one.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 08, 2025 May 08, 2025

It now works perfectly well.  Thank you so much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 13, 2025 May 13, 2025
LATEST

Hi again Nesa,

 

The requirement has changed. 

 

Instead of Select only two from the list, it is now Select at least two.

 

How would the scipt be if this is now the requirement. Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines