Skip to main content
Known Participant
August 19, 2020
Question

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

  • August 19, 2020
  • 1 reply
  • 996 views

Hello, 

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

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

Thanks

This topic has been closed for replies.

1 reply

BarlaeDC
Community Expert
Community Expert
August 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.

Known Participant
August 20, 2020

Thanks Malcolm,

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

 

Nesa Nurani
Community Expert
Community Expert
August 20, 2020

correct


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