Copy link to clipboard
Copied
Thanks in advance for reading.
My form user must fill 1, 1&2, or 1&2&3 dropdown fields with percentages that add up to 100%. Fields are titled "Distribution 1", "Distribution 2" and "Distribution 3". Each dropdown contains "Distribution" and 1%-100%".
I'd appreciate a proper javascript that will perform this validation (I am new to java and my scripts have been clumsy so far). * If it will not work with dropdowns, it is fine to use simple text fields. But I'd appreciate if "Distribution" can remain as an item list option in each field.
Copy link to clipboard
Copied
Would you mind to be more specific about what exactly you need the script to when a user selects "distribution" from any of the dropdowns or a percentage?
Are you trying to calculate something when you say validate?
It will be easier to assist if you can share an example of the script that you're working on.
Copy link to clipboard
Copied
When should this validation take place? What should happen if the total is not 100%?
Copy link to clipboard
Copied
I am interested in best-practices advice as well. If anyone has cleaner UI recommendations, I'd love to hear it!
Functional Context: The user has the option to enter between 1 and 3 Accounting Strings. 1 is visible by default. The user can add (make visible) up to two additional Accounting Strings by clicking a corresponding +Add button, and can remove it by clicking -Remove. Distribution is the first field in each Accounting String. The total of the visible Distribution fields must equal 100%.
What I need: When the user attempts to save the document, I would like a script that checks if the total distribution ("Distribution 1" + (if visible) "Distribution 2" + (if visible) "Distribution 3") add to exactly 100%. If valid, the document saves without issue. If not, thin red boxes appear around the visible distribution fields and a message reads "Please ensure your total Distribution equals 100%. Please also -Remove all used accounting strings and then save." Red Boxes should disappear upon saving if distribution(s) equal 100%.
Thanks, everyone.
Copy link to clipboard
Copied
In addition, I'd like if the user makes a second string visible (presses +Add button) that the default (editable) in the new distribution(s) is the remainder of 100% - Distribution 1.
For example, if the user selects 74% in Distribution 1 and then adds a second string, the default of the second string is 26%. Then, if the user adds a third string, the default for Distribution 2 and Distribution 3 is 13%.
Copy link to clipboard
Copied
That you will have to add to the code that makes the field visible. Shouldn't be too difficult to do.
Copy link to clipboard
Copied
I think the way you described is the best way to do it. Any other way would interfere with the user's data input. You can use this code under the file's Will Save event to achieve it:
var distTotal = Number(this.getField("Distribution 1").valueAsString) + Number(this.getField("Distribution 2").valueAsString) + Number(this.getField("Distribution 3").valueAsString);
if (distTotal==100) {
this.getField("Distribution 1").strokeColor = color.transparent;
this.getField("Distribution 2").strokeColor = color.transparent;
this.getField("Distribution 3").strokeColor = color.transparent;
} else {
this.getField("Distribution 1").strokeColor = color.red;
this.getField("Distribution 2").strokeColor = color.red;
this.getField("Distribution 3").strokeColor = color.red;
app.alert("Please ensure your total Distribution equals 100%. Please also -Remove all used accounting strings and then save.",1);
}
One thing that might be confusing for the user, though, is that the red borders will not disappear while they fill in the fields. To overcome that you can place that code (minus the alert!) as the custom calculation script of a (hidden) text field.
Copy link to clipboard
Copied
Thanks a million. These worked like a charm, and I've been able to modify for a host of new tools.
Copy link to clipboard
Copied
One thing.
Each of the three accounting string dropdowns contains items from "100%" to "1%" and "Distribution". The distTotal doesn't equal zero and returns red in every instance unless I enter each fields properties and change the export value of every item (101 items * 3 fields),
i.e. item: 1%, Export Value: 1.
I assumed that "Number(this....).valueAsString" should not require this additional step. Why does this happen and is there a better way?
Many thanks.
Copy link to clipboard
Copied
If the export values are numbers then it should work fine. Please share your file for further help with this issue.
Copy link to clipboard
Copied
Yes I can share. Is it possible to calculate the fields based on the item 'name' (i.e. 1%), without having to assign export values to each item? A script that ignores the percent sign? I believe if the items were named "100" instead of "100%", the fields would calculate without my having to add the export value. For my purpose, user experience benefits from including the percent signs in the dropdown.
In my workflow, I can generate dropdowns in indesign with large numbers of items using a simple macro, but I cannot add export values there. In order to add export values, I go into acrobat, field properties, and manually assign each item with a export value, i.e. Item: 1% ; Export Value: 1. I must do this 300 times per form. Surely you have a better way?
For example, can a script ignore the % signs in the item names (and the "Distribution" item) in order to calculate?
Copy link to clipboard
Copied
Then please share it...
Both options are possible. You can write a calculation script that will ignore the "%" symbol, or you could use a script to automatically assign the items (and export items) to the fields.
The former might be easier. All you need to do in the code above is to change this line:
var distTotal = Number(this.getField("Distribution 1").valueAsString) + Number(this.getField("Distribution 2").valueAsString) + Number(this.getField("Distribution 3").valueAsString);
To this:
var distTotal = Number(this.getField("Distribution 1").valueAsString.replace("%", "")) + Number(this.getField("Distribution 2").valueAsString.replace("%", "")) + Number(this.getField("Distribution 3").valueAsString.replace("%", ""));