Skip to main content
stepheng54012748
Known Participant
Dec 18, 2017 3:47 PM UTC
Answered

Writing a validation script to javascript......

I wrote a Javascript in Acrobat for a form that I'm working on.  The result could be either a positive number or a negative number that populates as my answer. I30 is supposed to yield the difference between I28 less I29.  If I28 populates a number $1,000 and nothing shows in I29 then the result in I30 would be $1,000.  This is correct.  If nothing shows in I28 and $1,000 populates in I29 then I30 is supposed to show as (1,000) because I28 less I29 would equal (1,000).  This result is not showing up.  I cannot figure out why.  I'm wondering if my validation script is the issue.  I've included this below the script.  Any suggestions?  Thank you so much for any input.  (Note:  I am learning and trying to figure out how to write this code)

var theField = this.getField("l28");

var theValue = theField.value;

var theField2 = this.getField("l29");

var theValue2 = theField2.value;

if (+theValue - +theValue2 > -10000000000.01) {

    this.getField("l30").value = (+theValue - +theValue2);

}

else {

     this.getField("l30").value = "";  

}

Custom validation script as follows:

if (event.value < .00001) event.value = "";

This topic is closed to new replies. Start a new post to keep the conversation going.
George_JohnsonCorrect answer
Inspiring
Dec 18, 2017 7:48 PM UTC

To be honest, I think I know why I'm writing the validation and my thought to ensure I get the result I'm looking for which is the negative or positive or even 0.  I really wasn't sure I was even doing it correctly.  I am truly learning as I go with this.


If you remove the validation script, I should work, if I'm understanding your intent.

When doing subtraction, it's possible to end up with a number that's very close to zero and expressed using scientific notation. As an example, when using JavaScript to do the following calculation:

0.3 - 0.2 - 0.1

It results in: -2.7755575615628914e-17

The reason for this is due to the way JavaScript handles numbers. This in itself isn't a bug, but the way Acrobat formats such a result can be considered a bug. When this field value is formatted by Acrobat's built-in formatting code, it generates an error. You can avoid this by converting any value that is very close to zero, to zero. In the last script I posted, you can add a line of code after the one that begins with var diff =

if (Math.abs(diff) < 0.00001) diff = 0;

And that should take care of it, after you remove the validation script.

9 replies

Inspiring
Dec 18, 2017 5:53 PM UTC

Where did you place that first script, what field and what event?

Translate
stepheng54012748
Known Participant
Dec 18, 2017 6:07 PM UTC

First of all, Thank you for responding.  I'm not sure if I'm answering you correctly.  But I will try.  The first script is placed in Field I30.

Translate
Inspiring
Dec 18, 2017 6:10 PM UTC

OK, is it placed as a custom calculation script, or something else? Just to confirm, the validation script that you showed is for I30 as well, correct?

Translate