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

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

Explorer ,
Aug 19, 2020 Aug 19, 2020

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
989
Translate
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

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.

Translate
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

The line that reads:

 

var total= fieldA.value + fieldB+value;

 

should be

var total= fieldA.value + fieldB.value;

 

A common typo we all make

Translate
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

Thanks Malcolm,

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

 

Translate
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

Format field as number.

Translate
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

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

Translate
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

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

Translate
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

correct

Translate
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
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".

Translate
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