Skip to main content
benadamsphoto
Known Participant
September 19, 2018
Answered

Script for Dependant Visual Status

  • September 19, 2018
  • 1 reply
  • 1011 views

Hi All - really hoping someone can help me out with their JavaScript knowhow!

I‘m creating a PDF form for my team which will be used to record the results of testing. What I’d like to do is have a text field which will change its text value and text colour, depending on the value inputted into another field.

I have a text field in which a user will copy and paste a value which they have acquired - in this case it will be a hash value. There are expected reference hash values, and those acquired by the user during tests should ideally match these, but of course they may not. I want confirmation and a strong visual indication of this using a calculation script.

As an example:

I have a text field which the user will paste their acquired hash value into; this can either be upper-case or lower-case - we’ll call this field ‘Acquired-Hash’.

For ease, let us pretend for this example that the expected value is “3bf5”.

Next to the field ‘Acquired-Hash‘, I’d like a dynamic text field (called ‘Result’) which will automatically display the following:

If the ‘Acquired-Hash’ field is left blank, the ‘Result’ field will display “Awaiting Input” in BLACK lettering.

If the ‘Acquired-Hash’ field value is “3bf5” OR “3BF5”, the ‘Result’ field will display “MATCH” in GREEN lettering.

If the ‘Acquired-Hash’ field has a value of anything other than “3bf5” or “3BF5” entered into it, the ‘Result’ field will display “MIS-MATCH” in RED lettering.

Now, I have actually managed to achieve this using custom calculation scripts, but this is by having 3 separate ‘Result’ fields, with different default values, laid on top of each other - each one having a calculation script which affects their visibility depending on the entry into the ‘Acquired-Hash’ field. Although this works, it’s a bit clunky and time consuming to do in Acrobat, because I have multiple sections of the form where I will need to configure this validation of an inputted result.

Is there a way I can just have one ‘Result’ field with a single custom calculation script that will display the values as described above in the different colours, depending on the entry into the neighbouring ‘Acquired-Hash’ field? I’m building in InDesign, so this would save me so much time only having to create one Result field for each section of the form!

Many thanks in advance!

This topic has been closed for replies.
Correct answer George_Johnson

I think this should do what you want as the custom calculate script for a single Result field:

// Custom calculation JavaScript for text field

(function () {

    // Get the field value, as a string, converted to uppercase

    var sHash = getField("Acquired_Hash").valueAsString.toUpperCase();

    if (sHash) {  // Not blank

        if (sHash === "3BF5") {

            event.value = "Match";

            event.target.textColor = color.green;

        } else {

            event.value = "MIS-MATCH";

            event.target.textColor = color.red;

        }

    } else {  // Input is blank

        event.value = "Awaiting Input";

        event.target.textColor = color.black;

    }

})();

1 reply

Inspiring
September 19, 2018

Are you saying that you want the Result field to be able to account for multiple valid Acquired-Hash values, rather than just one? If not, I'm not clear on your objective.

benadamsphoto
Known Participant
September 19, 2018

No - I just need the Result field to account for one of three options; No entry, a matching entry (either upper case or lower case) or a mismatched entry.

There is only one expected/valid hash value; the user will copy and paste their acquired hash value into the ‘Acquired’ field for accuracy, but this could be in either upper or lower case depending on the software report being copied from.

George_JohnsonCorrect answer
Inspiring
September 19, 2018

I think this should do what you want as the custom calculate script for a single Result field:

// Custom calculation JavaScript for text field

(function () {

    // Get the field value, as a string, converted to uppercase

    var sHash = getField("Acquired_Hash").valueAsString.toUpperCase();

    if (sHash) {  // Not blank

        if (sHash === "3BF5") {

            event.value = "Match";

            event.target.textColor = color.green;

        } else {

            event.value = "MIS-MATCH";

            event.target.textColor = color.red;

        }

    } else {  // Input is blank

        event.value = "Awaiting Input";

        event.target.textColor = color.black;

    }

})();