These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Javascript Calculations

sarahjensen
Registered: Feb 9 2009
Posts: 7
Answered

I'm new to Java and Acrobat 9. I want to calculate a form and have the submit button grayed out until all the check boxes have been checked. This is my code, it works checking up to 17, but if I uncheck any boxes the submit button becomes available at 15 and 18, what am I doing wrong?

// Function to enable form fields

function EnableFormField (Print)
{
// First acquire the field to be enabled
var oFld = this.getField(Print)
// Next acquire the hidden field with the normal colors
var oNmlFld = this.getField("Print/Send");
if(oFld)
{
// Make field interactive
oFld.readonly = false;
// Restore Normal Colors
oFld.fillColor = oNmlFld.fillColor;
oFld.borderColor = oNmlFld.borderColor;
oFld.textColor = oNmlFld.textColor;
}
}

// Function to disable form fields

function DisableFormField(Print)
{
// First acquire the field to be disabled
var oFld = this.getField(Print)
if(oFld)
{
// Make field Read-Only
oFld.readonly = true;
// Set Grayed out colors
oFld.fillColor = ["G", 0.75];
oFld.borderColor = ["G", 2/3];
oFld.textColor = ["G", 0.5];
}
}

And in the actual field under the "custom validate" tab:

if(this.getField("Sum").value =="16")
EnableFormField("Print");

if(this.getField("Sum").value <"16")
DisableFormField("Print");if(this.getField("Sum").value >"16")
DisableFormField("Print");

My Product Information:
Acrobat Pro 9.0, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Are your sum fields character strings or numeric values?

If they are numberic values, test them as numbers and not strings.

Because check boxes can have assigned values or a value of "Off", what is the script that sums the check boxes?

George Kaiser

sarahjensen
Registered: Feb 9 2009
Posts: 7
The check boxes have numeric value of 1, and the sum field is set to numeric. The script is the generic calculate, where you can choose the fields to add together.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
It appears you are checking the wrong value. The field value is only set upon exiting the field, so you are testing the previous value of the sum field.

// let's see what is happeningconsole.show();console.clear();console.println("Sum = " + this.getField("Sum").value)console.println("event.value = " + event.value); // by default disableDisableFormField("Print/Send");// if we have 16 selected enableif(event.value == 16)EnableFormField("Print/Send");

I would still not use the character string representation for a number in compairng numbers because if there is a change in the format of the field, like a change in the number decimal places, your script would no longer work.

George Kaiser

sarahjensen
Registered: Feb 9 2009
Posts: 7
Thanks for your help, when I copy and paste that code into Document Scripts, this is what came up in the console


Sum =
event.value = undefined

I don't quite understand the string concept, what would be a better way to validate that one check box per line is checked, with a total form validation of 17 lines?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
The script should be on the vailidation tab as a custom script for the "Sum" field.

George Kaiser