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

How can I revise the script below to only allow 8 selections without deleting the first and generate a pop up to alert user that they have reached the maximum allowed. Also, is it possible to code text box to stack answers vertically without comma, curre

New Here ,
Jul 23, 2018 Jul 23, 2018

Copy link to clipboard

Copied

This can be done, but is a bit complex. Let's assume you have one dropdown and a text field named "Text1", then do the following:

Create a document level script using the following script:

function UpdateTextField(doc, fieldName, newValue) { var ar = []; // Get the values currently stored in the text field and  // convert them to an array var v = doc.getField(fieldName).value; if (v.length > 0) { var ar = v.split(","); } // Do we already have newValue in the array?  var foundIt = -1; for (var i=0; i<ar.length; i++) { if (ar[i] == newValue) { foundIt = i; break; } } if (foundIt != -1) { // remove the item ar.splice(i, 1); } else { // add the item at the end ar.push(newValue); } // If we have more than four elements,  // remove the oldest one while (ar.length > 4) { ar.shift(); } // conver the array to a string this.getField(fieldName).value = ar.join(); }

In your dropdown control, go to the Options tab and make sure that "commit selected values immediately" is selected, then go to the "Validation" tab and select to use a custom validation script. Use this one line script:

UpdateTextField(this, "Text1", event.value);

As you can see, we are specifying the field name as the 2nd parameter to this function call.

This should do the trick. When you select a value that's already in the list, it will be removed, and if you select more than four values, the oldest value will be removed.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com

Karl Heinz Kremer  

TOPICS
Acrobat SDK and JavaScript

Views

249

Translate

Translate

Report

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 ,
Jul 23, 2018 Jul 23, 2018

Copy link to clipboard

Copied

It looks like you have copied a response to a different question into the body of your post. It appears we are to assume how you want this code changed.

Also the code that was posted has no line breaks and because there are JavaScript comments present the line breaks are needed to visually show to others and the JavaScript engine where the comment ends and the executable code continues. Without this line break, everything that follows the comment start symbols becomes a single comment.

To add the new line after each item selected, enable the "Multiple lines" for the text field, change the line in the document level function that reads:

this.getField(fieldName).value = ar.join();

to:

this.getField(fieldName).value = ar.join("\n");

Votes

Translate

Translate

Report

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
New Here ,
Jul 23, 2018 Jul 23, 2018

Copy link to clipboard

Copied

Thanks.  I copied/pasted from a previous response because that item would no longer allow questions and sent me here.  However, it was very useful.  Here is the script I used.  My questions is "How can I revise the script to only allow 8 answers without deleting the first if a user selects more than 8.  If a user makes more than 8 selections I would like a alert pop-up window telling them they have reached the maximum allowed.  Also the script populates the answers horizontally and separates each answer with a comma.  I would like to populate the answers in the text box vertically if possible.

function UpdateTextField(doc, fieldName, newValue)

{

    var ar = [];

    // Get the values currently stored in the text field and

    // convert them to an array

    var v = doc.getField(fieldName).value;

    if (v.length > 0) {

        var ar = v.split(",");

    }

    // Do we already have newValue in the array?

    var foundIt = -1;

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

        if (ar == newValue) {

            foundIt = i;

            break;

        }

    }

    if (foundIt != -1) {

        // remove the item

        ar.splice(i, 1);

    }

    else { // add the item at the end

        ar.push(newValue);

    }

    // If we have more than eight elements,

    // remove the oldest one

    while (ar.length >= 9) {

        ar.shift();

    }

    // conver the array to a string

    this.getField(fieldName).value = ar.join();

}

// Custom Keystroke script for text field

(function () {

    // When the user has committed the value...

    if (event.willCommit) {

        // ...check to see if what was entered is valid...

        var isValid = checkFormat(event.value);

        // ...and if not, alter the user and reject the value

        if (!isValid) {

            app.alert("Please enter a valid value.", 1);

            event.rc = false;

        }

    }

})();

Votes

Translate

Translate

Report

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 ,
Jul 23, 2018 Jul 23, 2018

Copy link to clipboard

Copied

LATEST

In a way, the answer is already in your script, where the code removes all elements over 8.

You'll to put a similar test above the code that adds the new entry

like this.

  if (foundIt != -1) {

        // remove the item

        ar.splice(i, 1);

    }

    else if(ar.length >= 8)

    {

      app.alert("More than 8 entries selected");

    }

    else { // add the item at the end

        ar.push(newValue);

    }

Also, the next time you ask a question it would very helpful to us if you explain how your scenario is setup.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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