• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

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

Community Beginner ,
Apr 18, 2020 Apr 18, 2020

Copy link to clipboard

Copied

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!

 

TOPICS
Acrobat SDK and JavaScript

Views

677

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 20, 2020 Apr 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 = "";

Votes

Translate

Translate
Community Expert ,
Apr 19, 2020 Apr 19, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 19, 2020 Apr 19, 2020

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2020 Apr 19, 2020

Copy link to clipboard

Copied

What about the Low value?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 19, 2020 Apr 19, 2020

Copy link to clipboard

Copied

When I tried using the same formula for the low value it wouldn't calculate anything.

 

In some instances, if I have a range of lets say $0.00 to $1,000.00, and the average is $1,500.00. I need a percentage variance for the $500.00.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2020 Apr 20, 2020

Copy link to clipboard

Copied

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 = "";

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 20, 2020 Apr 20, 2020

Copy link to clipboard

Copied

LATEST

I think that worked! Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines