Copy link to clipboard
Copied
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!
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";
ev
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
})();
Copy link to clipboard
Copied
Fixed some typos in the code.
Copy link to clipboard
Copied
Hi George
Apologies for not coming back to you sooner, I’ve only just been back in the office to test this.
The code you supplied works perfectly! I can’t thank you enough as this will be a huge help.
I wonder if you you might be able to advise on one more thing - would it be possible to modify the code so that the script checks if the value inputted into the ’Acquired Hash’ field matches that of another field called ‘Expected Hash’, rather than defining the expected hash value in the script itself?
Hope that makes sense!
Many thanks once again.
Copy link to clipboard
Copied
Change this line:
if (sHash === "3BF5") {
To:
if (sHash === this.getField("Expected Hash").valueAsString.toUpperCase()) {
Copy link to clipboard
Copied
Brilliant - works like a charm. Thank you.
Copy link to clipboard
Copied
Quick question - the green being used for the Match result is quite harsh and difficult to see against the background of the form field. How can I change this?
Some research suggested that I should simply modify the line:
event.target.textColor = color.green
to:
event.target.textColor = color.green= [“RGB”, 0, 100, 0]
but unfortunately this does not work....
Copy link to clipboard
Copied
You have at least three different errors in that line of code... Change it to this:
event.target.textColor = ["RGB", 0, 100/255, 0];