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

if A+B>C then A+B=C

Explorer ,
Aug 19, 2020 Aug 19, 2020

Copy link to clipboard

Copied

Hello, 

could you please help me to script the following in a pdf form:

if A+B>10 then A+B=10

Thanks

TOPICS
Acrobat SDK and JavaScript

Views

498

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 ,
Aug 19, 2020 Aug 19, 2020

Copy link to clipboard

Copied

Hi,

 

It would help if we knew the names of the fields you are trying to use ( I will assume they are called 'A', 'C', with the total going into 'C'

 

 

var fieldA = this.getField("A");
var fieldB = this.getField("B"):

var total= fieldA.value + fieldB.value;

if ( total > 10 ) {
    total = 10;
}

this.getField("C").value = total;

 

 

 This code could be added to the validation of 'A' and 'B', which would mean it would be run everytime the values changed.

 

Hope this helps.

 

Malcolm

 

edit - to fix typo.

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
LEGEND ,
Aug 19, 2020 Aug 19, 2020

Copy link to clipboard

Copied

The line that reads:

 

var total= fieldA.value + fieldB+value;

 

should be

var total= fieldA.value + fieldB.value;

 

A common typo we all make

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
Explorer ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

Thanks Malcolm,

How to add a line to remove any trailing zero from the end of the result?

 

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
Enthusiast ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

Format field as number.

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
Explorer ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

If I format as (None), no trailing zero will appear.

But if I want to allow for 2 decimels without a trailing zero, I think a script has to be added to remove the trailing zero.

(I am a learner and beginner, please bear with me)

Thanks

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
Enthusiast ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

So you wan't if result is 2.22 to show like that but if result is 2.00 to just show 2?

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
Explorer ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

correct

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 ,
Aug 20, 2020 Aug 20, 2020

Copy link to clipboard

Copied

LATEST

Hi, try this code and see if it is what you wanted:

var a = this.getField("A").value;
var b = this.getField("B").value;
var x = a+b;
var x1 = x.toFixed(2);
var x2 = x1.replace(".00", "");
event.value = x2;
if( x2 > 10 ){
x2 = 10;
}
event.value = x2;

Don't format field, leave it at "None".

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