Skip to main content
Michelle38051592s1e9
Participating Frequently
July 1, 2024
Answered

I Need to Have a Custom Script Which Terminates Calculations if No Input is Entered.

  • July 1, 2024
  • 1 reply
  • 1909 views

I am creating a mileage log for our fleet vehicles. I know how to do basic calculations. What I need is a custom script that will terminate the calculations if no input is entered on the EndOdomReadingRow. Also I need it to calculate if there is input. Because if I go with the basic calculation EndOdomReadingRow3 - EndOdomReadingRow2  which equals MilesTraveledRow3 and there is no input in EndOdomReadingRow3 it puts a negative number in there. So far the only way I have to do it is to go in and correct every line in which there is no input. I know there must be an easier way. Any help will be greatly appreciated.

This topic has been closed for replies.
Correct answer Thom Parker

Yes, I missed a closing parentheses on the "if" condition. Here's the updated code

 

var nOdomRow3 = this.getField("EndOdomReadingRow3").valueAsString;
var nOdomRow2 = this.getField("EndOdomReadingRow2").valueAsString;
if(nOdomRow3 && nOdomRow3 && !isNaN(nOdomRow3) && !isNaN(nOdomRow2))
     event.value = Number(nOdomRow3) - Number(nOdomRow2);
else
     event.value = "";

 

If there is an error in the code, then everything after the error is meaningless, because code execution stops at the error. 

 

1 reply

Thom Parker
Community Expert
Community Expert
July 1, 2024

Here's a calculation script that checks the input values and sets the output to a blank if either input is empty, or zero, or a non-numeric value. 

 

var nOdomRow3 = this.getField("EndOdomReadingRow3").valueAsString;

var nOdomRow2 = this.getField("EndOdomReadingRow2").valueAsString;

if(nOdomRow3 && nOdomRow3 && !isNaN(nOdomRow3) && !isNaN(nOdomRow2)

      event.value = Number(nOdomRow3) - Number(nOdomRow2);

else

     event.value = "";

 

Another way to do this is to use a custom format script that sets the output to a blank if the calculation is <= 0, or not a number

 

Custom Format Script

 

event.value = (isNaN(event.value)|| (event.value <= 0))?"":event.value;

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Michelle38051592s1e9
Participating Frequently
July 1, 2024

var nOdomRow3 = this.getField("EndOdomReadingRow3").valueAsString;
var nOdomRow2 = this.getField("EndOdomReadingRow2").valueAsString;
if(nOdomRow3 && nOdomRow3 && !isNaN(nOdomRow3) && !isNaN(nOdomRow2)
event.value = Number(nOdomRow3) - Number(nOdomRow2);
else
event.value = "";

The first one said it had a SyntaxError: missing ) after condition 4: at line 5 and the second one does not subtract if there is input.

I know that it is close though. I appreciate your help and thank you so much.

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
July 1, 2024

Yes, I missed a closing parentheses on the "if" condition. Here's the updated code

 

var nOdomRow3 = this.getField("EndOdomReadingRow3").valueAsString;
var nOdomRow2 = this.getField("EndOdomReadingRow2").valueAsString;
if(nOdomRow3 && nOdomRow3 && !isNaN(nOdomRow3) && !isNaN(nOdomRow2))
     event.value = Number(nOdomRow3) - Number(nOdomRow2);
else
     event.value = "";

 

If there is an error in the code, then everything after the error is meaningless, because code execution stops at the error. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often