3 Replies Latest reply on Feb 10, 2012 11:36 AM by George_Johnson

# Math.min JavaScript help needed

Hello,

I am trying to write a script for a fillable PDF in Adobe Acrobat, but I don't have much experience with JavaScript and I need some help!

Basically I have three fields as follows:

A1 = a field with a number value in it

B1 = a field with a number value in it

C1 = a field where the sum calculation will be.

I want to write a script that will enter the lesser of the values in A1 and B1 into C1. If no value has been entered into B1, then it enters the value of A1 into C1.

This is what I have come up with so far:

// Get the field values, number fields as numbers

var d1 = getField("A1").value;

var d2 = +getField("B1").value;

// Set the field value to either A1 or B1, whichever is lesser

event.value = Math.min(d1,d2);

Some of the problems with the script so far are:

1. It does not calculate correctly if there is no value entered in B1 (should give just A1).
2. It doesn't update the event.value if A1 is changed (until another form field is modified)

Can anyone help??

Thanks!

• ###### 1. Re: Math.min JavaScript help needed

The second issue has to do with the field calculation order. You should set C1 to calculate after both A1 and B1.

Use this code instead of what you currently have:

var d1 = getField("A1").value;

var d2 = getField("B1").value;

if (d2=="") event.value = d1;

else event.value = Math.min(+d1,+d2);

• ###### 2. Re: Math.min JavaScript help needed

Adjusted the field calculation order fixed question 2.

The script given as answer worked great except for one area.

B1 values can range from 0 and up. If a 0 is entered in B1, then the Math.min ignores it since d2=="".

I can get around that problem by having a default value set in B1 of -1 (outside of the range). I'd prefer that there not be a default value set.

Any suggestions? I appreciate the help.

• ###### 3. Re: Math.min JavaScript help needed

That script can be changed to the following to fix the problem:

``` var d1 = getField("A1").valueAsString;
var d2 = getField("B1").valueAsString;

if (d2 === "") event.value = d1;
else event.value = Math.min(+d1,+d2);
```