Skip to main content
Known Participant
September 9, 2022
Question

Forms: Javas: Calculation script returns error before user finishes typing their entry

  • September 9, 2022
  • 1 reply
  • 258 views

Hey all,

I'm relatively new to java. 

 

Question: Is there a code that functions like 'event.target'--e.g. triggers when user completes an entry, clicks away from the field--that can be used external to the field in which the event occurs? Specifically, in the calculation script of a hidden field.

 

I am writing numerous 'field validations' that display or hide fields ('error messages'). I can mod/copy this script into each field as an onBlur action, which effectively performs the 'validation' after the user clicks away. I can also paste it in each field as a validation script. This works fine enough. But it occurs to me it would be simpler if all the codes were consolidated in one place.

 

My idea is to consolidate all my scripts, including the 'field validations', into one hidden field, as a calculation script. 

 

Problem is: when I change event.target to the field name, the code calculates as soon as a field is entered, and calculates again after each character is entered. This is not desirable.

 

Here's the code i'm using in the calculation script below. The script functioned properly if internal1.value is replaced by event.target.value.

 

var errorInternal1 = this.getField("errorInternal1");
var errorInternal2 = this.getField("errorInternal2");
var errorInternal3 = this.getField("errorInternal3");
var internalTest = /^[A-Z]{3}[0-9]{6}$/;
var internal1 = this.getField("Internal 1");
var internal2 = this.getField("Internal 2");
var internal3 = this.getField("Internal 3");

if(internal1.display==display.hidden){
errorInternal.display = display.hidden;
} else if (internal1.value=="Internal") {
errorInternal1.display = display.hidden;
errorInternal2.display = display.hidden;
errorInternal3.display = display.hidden;
} else if (internalTest.test(internal1.value) == false && internal2.display==display.hidden && internal3.display==display.hidden)
{ errorInternal1.display = display.visible;
errorInternal2.display = display.hidden;
errorInternal3.display = display.hidden;
} else if (internalTest.test(internal1.value) == false && internal2.display==display.hidden && internal3.display==display.visible)
{ errorInternal1.display = display.hidden;
errorInternal2.display = display.hidden;
errorInternal3.display = display.visible;
} else if (internalTest.test(internal1.value) == false && internal2.display==display.visible && internal3.display==display.visible)
{ errorInternal1.display = display.hidden;
errorInternal2.display = display.hidden;
errorInternal3.display = display.visible;
} else if (internalTest.test(internal1.value) == false && internal2.display==display.visible && internal3.display==display.hidden)
{ errorInternal1.display = display.hidden;
errorInternal2.display = display.visible;
errorInternal3.display = display.hidden;
} else {
errorInternal1.display = display.hidden;
errorInternal2.display = display.hidden;
errorInternal3.display = display.hidden}
This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
September 9, 2022

First, the trigger field for a calculation, (i.e., the field that caused the calculation event by changing) is event.source.  Not all calculations are triggered from a source field, for example on reset, so sometimes event.source is null

 

The calculation script is only triggered after a field value is committed. So if you are seeing a calculation on every keystroke, then there are other field scripts causing this issue. 

 

event.target always refers to the field that contains the script being run. So if this script is in a hidden calculation it could not possibly work for the "internal1" field.

 

Now, the title of the post suggest an error occurs in this script. What is the error?

 

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