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

How can you limit the number of selections in a List box

Community Beginner ,
Jun 24, 2025 Jun 24, 2025

I've been banging my head on this for a ling time.

How can write a script which prevents a List Box from accepting more than a specified number of selected items?

TOPICS
Edit and convert PDFs , JavaScript , PDF , PDF forms
328
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 ,
Jun 24, 2025 Jun 24, 2025

Enter the following On Blur script in the list box field:

var vals=event.target.value;
if(typeof vals=="object")
{
var newVals=[];
for(var i=0;i<3;i++)
{
newVals.push(vals[i])
}
if(vals.length>3)
{app.alert("The maximum number of selections is 3.",1)}
event.target.value=newVals;
}

Change the three 3s to the actual maximum number of selections.  Multiple selections in a list box are not committed until the blur event.

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 Beginner ,
Jun 24, 2025 Jun 24, 2025

Hello and thanks.

 

This script would randomly leave the fist three selected items. This is not what I'm looking for.

A script that validates would ideally return the previoulsly selected items before this change in selection was made.

 

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 ,
Jun 25, 2025 Jun 25, 2025

I don't think there's a way to commit multiple selections before the blur event.

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
LEGEND ,
Jun 25, 2025 Jun 25, 2025

Hi @rarebit_7112 ,

 

+++Adding to the valuable guidance of @PDF Automation Station , 

 

List boxes can be quite challenging to navigate, as you mentioned, because they are designed to have a somewhat similar feel to button objects and also share characteristics with dropdown menus. However, they are fundamentally different from text field objects. Therefore, managing the field properties like you would with other PDF objects is not possible unless you employ some creative methods.

 

The primary issue with list boxes is that when they read and write values, the selected (or entered) values do not immediately reflect a change upon selection. That is why @PDF Automation Station employed a Blur Event action .

 

Additionally, the script he provided works well and addresses your inquiry. It may need to be slightly modified, to avoid randomly reverting to three selections after the alert is triggered.

 

That said, the items in a listbox are accessed using methods that utilize a zero-based index. So, when you query a list box object, it returns an array (in the case of multiple selections) in the order that they were listed when that field object was created, starting from 0.

 

And this being the case, If you are working on a validation script, you might need to use a hidden text field and place the validation script there, as this is not a field property of a list box, unlike dropdown menus. Moreover, implementing this approach contradicts the simplicity and purpose of your workflow.

 

In my example script below, I utilize the "currentValueIndices" method along with an App Alert that activates when more than three items are selected from the list box.

 

To make it function like a validation script, I incorporated the setFocus() method into the script. This, combined with the alert, ensures that the focus remains on the listbox field, preventing the user from proceeding to other fields until three items are selected. By doing this, the values are neither reverted nor reset, allowing the user to see their previous selections and adjust them as needed.

 

 

var f = this.getField("List Box");

var a = f.currentValueIndices;

for (var i = 0; i < a.length; i ++) {

 (a.length > 3) ? ([app.alert("Please choose only three options."), f.setFocus()]) : f.getItemAt(a,true);

 break;

} 

 

Let us know if this information is helpful.

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 ,
Jun 25, 2025 Jun 25, 2025

"It may need to be slightly modified, to avoid randomly reverting to three selections after the alert is triggered."  It does not randomly revert to 3 selections, it reverts to the first 3 selections in the order the items appear.

The value property and the currentValueIndices both return an array of selections.  value returns the values, currentValueIndices returns the index number, which returns the value when one of the indices is passed into getItemAt() when the bExportValue parameter is set to false, or the the bExportValue is set to true and there is no export value.  This yields the exact result as getting the value from the index of the value array.

" If you are working on a validation script, you might need to use a hidden text field and place the validation script there"

Validation scripts only run when the value of field containing the validation script changes so this script will never run unless the user changes the value of the text field after having made selections in the list box that exceed the maximum - that's not even as good as the blur event in the script I created.  The bottom line is, it does not work the way the poster wants.  If selections in a list box do not commit the values and list boxes do not have a custom keystroke option, I don't think it's possible to make this work the way @rarebit_7112 wants it to.

I haven't seen an AI generated answer work on this forum yet.  Thank you for your attention to this matter.

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
LEGEND ,
Jun 26, 2025 Jun 26, 2025

Thank you for clarifying @PDF Automation Station , and your welcome!

 

Maybe my answer didn't came out right. It happens to me a lot.

 

To be clear, I never use ChatGPT nor AI generated answers... online paraphrasing tools and grammar checker yes (randomly).  Main reason, so that I don't sound rude and synical to customers and other community experts.

 

Both of the Acrobat JavaScript hack and the answer that I posted are based off of the old Acrobat JavaScript API Reference 8 for currentValueIndices and setFocus().

 

I do take pride in reading and self-teaching myself regardless of mistakes and my ignorance in some topics.

 

Anyway, I just chopped off a lot of the things in my script that you've explained because that was not working, plus none of that was what I was aiming for.

 

And the idea that I suggested of employing a hidden field comes from a technique that @try67  posted before in these forums to address similar inquiries with checkboxes in order to be able to run a validation when a selection is made on such objects.

 

 

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 ,
Jun 27, 2025 Jun 27, 2025

Thank you for the clarification and thank you for your contributions to the forum.

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 ,
Jun 25, 2025 Jun 25, 2025

OK, never say never.  Here's the solution:

The only script in the list box field is the following Mouse Up action:

this.getField("Decoy").setFocus();

Create a text field called Decoy and enter the following On Focus script:

if(this.getField("List Box").value.length==3)
{
var vals=this.getField("List Box").value;
}
else if (this.getField("List Box").value.length>3)
{
this.getField("List Box").value=vals;
app.alert("The maximum number of selections is 3.",1);
}

Decoy can't be hidden or read only or it will not receive the focus and script will not run.  So make the field kind of hidden by making it invisible by setting all its coordinates to zero.  Run the following script in the console:

this.getField("Decoy").rect=[0,0,0,0];

 

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 Beginner ,
Jul 02, 2025 Jul 02, 2025
LATEST

I ended making a dummy text box where I save the selected values of the list box. They are saved as comma delimated string. If the selection exceed the limit it is reverted tot the items from the text box.

 

var max = 3;

selected = this.getField("Selected Advantages").value;

if (typeof selected == "object") {
    if (this.getField("Selected Advantages").currentValueIndices.length > max) {
        app.alert("The maximum number of selections is " + max,1);
        if (this.getField("Advantage Dummy").value == ""){
            this.resetForm(["Selected Advantages"]);
        } else {
            this.getField("Selected Advantages").value = this.getField("Advantage Dummy").value.split(",");
        }
    } else {
        this.getField("Advantage Dummy").value = selected;
    }
} else {
    this.getField("Advantage Dummy").value = selected;
}

Since I have the options in the checkbox change the selection is preserved for any options which are still a part of the list.

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