Skip to main content
Participating Frequently
April 19, 2020
Answered

JavaScript Calculation: Value does not enter the format of a field?

  • April 19, 2020
  • 2 replies
  • 1396 views

Hi,

I have an adobe form in which I need to calculate a value's variance from a range. For example, if the range is $1,000.00 to $2,500.00, and the value is $3,000.00, I need it to return a variance of $500.00. If the value was $1,500.00, I need it to return a variance of $0.00 since it falls in the range. There may be occasions in which the range is $0.00 to $0.00, in which case any value would be a variance.

 

I have a built an adobe form with the following form fields:

1.) Low End of Range (Set to $ Format) 
2.) High End of Range (Set to $ Format)

3.) Actual  (Set to $ Format)

4.) Average (Set to $ Format, automatically generates the average based on the entry to "Actual" field)

5.) $ Variance (Set to $ Format, automatically calculates the $ variance of the average to the ranges defined in 1 and 2 above)

6.) % Variance (Set to % Format, to automatically valculate the % variance of the average to the ranges defined in 1 and 2 above)

 

The problem I am having is when the user enters the high range they recieve an error saying "The valued entered does not match the format of the field [% Variance]". It does not seem to be causing an error with the calculation; however, I would like to find a way to avoid the error from displaying. 

 

I have the following custom JavaScript calculation in the % Variance field:

 

var Average = this.getField("Avg").value
var Low = this.getField("Low").value
var High = this.getField("High").value
var Variance = this.getField("Variance$").value

if (High== 0)
    event.value = "";
else if(Average <= Low)
     event.value = Variance / Low;
else if (Average >= High)
    event.value = Variance / High;

 

I am pretty new to JavaScript, so I thank you in advance for your assistance!

 

This topic has been closed for replies.
Correct answer try67

Use this:

 

var Average = Number(this.getField("Avg").valueAsString);
var Low = Number(this.getField("Low").valueAsString);
var High = Number(this.getField("High").valueAsString);
var Variance = Number(this.getField("Variance$").valueAsString);

if (Average <= Low && Low!=0)
     event.value = Variance / Low;
else if (Average >= High && High!=0)
    event.value = Variance / High;
else event.value = "";

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
April 20, 2020

Use this:

 

var Average = Number(this.getField("Avg").valueAsString);
var Low = Number(this.getField("Low").valueAsString);
var High = Number(this.getField("High").valueAsString);
var Variance = Number(this.getField("Variance$").valueAsString);

if (Average <= Low && Low!=0)
     event.value = Variance / Low;
else if (Average >= High && High!=0)
    event.value = Variance / High;
else event.value = "";
bgb6Author
Participating Frequently
April 21, 2020

I think that worked! Thank you!

try67
Community Expert
Community Expert
April 19, 2020

When the divisor is zero the result of your calculation is Infinity, which is not a valid value for a field with the Number format. You need to first check for that, and only if the divisor is not zero proceed with the calculation.

bgb6Author
Participating Frequently
April 19, 2020

Hello,

Thanks for the reply. I am very inexperienced when it comes to JavaScript, so how would I go about that?

 

I thought that is what I was doing with the

if (High== 0)
    event.value = "";

 part of my formula?

 

Thank you in advance for your assistance!

try67
Community Expert
Community Expert
April 19, 2020

What about the Low value?