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

How can I convert Calculation Scripts into Validation Scripts?

suewhitehead
Registered: Jun 3 2008
Posts: 232
Answered

I have 3 numerical fields in my Acrobat form that calculate miles travelled times the mileage allowance and put the product in a third field. They are named: MileageAllowance, NoMilesR1, and MileageAmtR1.

Currently I have this calculating on the Calculation tab like this:
var f = this.getField("MileageAllowance");
var g = this.getField("NoMilesR1");
event.value = f.value * g.value;

I have learned in this forum some reasons why validation scripts might help remove some errors I am having with this form, so I am trying to change the calculations to the Validation tab. How would I write this script and where would I put it, etc.?

My Product Information:
Acrobat Pro 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
What you should do is create a new document-level function and call it from the Validate event of each of your two input fields. The function could look something like:

function calcTravelCost() { // Specify the names of the input fieldsvar n1 = "MileageAllowance";var n2 = "NoMilesR1"; // Get a reference to the output fieldvar fCalc = getField("MileageAmtR1"); // Get the value of the two input fields// event.value = value of the text field that is being changed// event.target.name is the name of the text field that is being changedvar v1 = (event.target.name == n1) ? event.value : getField(n1).value;var v2 = (event.target.name == n2) ? event.value : getField(n2).value; // Set the value of the calculated fieldfCalc.value = v1 * v2; }

You'd then use the following as the custom validation script of both input fields:

calcTravelCost();
The function code could be streamlined a bit, but I wanted it to be clear.

George
suewhitehead
Registered: Jun 3 2008
Posts: 232
I have this same calculation on 12 different rows, such as NoMilesR1, NoMilesR2, NoMilesR3, etc. The calculation for row1 would show in MileageAmtR1, and the calculation for row2 would show in MileageAmtR2, etc. Will this solution work with all of these rows?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
That complicates things a bit, but it's certainly doable. Modify the function to the following:

function calcTravelCost(nRow) { // Specify the names of the input fieldsvar n1 = "MileageAllowance";var n2 = "NoMilesR" + nRow; // Get a reference to the output fieldvar fCalc = getField("MileageAmtR" + nRow); // Get the value of the two input fields// event.value = value of the text field that is being changed// event.target.name is the name of the text field that is being changedvar v1 = (event.target.name == n1) ? event.value : getField(n1).value;var v2 = (event.target.name == n2) ? event.value : getField(n2).value; // Set the value of the calculated fieldfCalc.value = v1 * v2; }

You'd then have to call this function from each of the NoMileR fields by specifying the row number:

// Update row 1calcTravelCost(1);

Since you have a number of fields that depend on the MileageAllowance field value, you'd have to use something like the following for its validation script

// Update rows 1-12 when this value changesfor (var i = 1; i < 13; i += 1) {calcTravelCost(i);}

George
suewhitehead
Registered: Jun 3 2008
Posts: 232
Cool, George. This works great. Appreciate your help very much.