Skip to main content
Participant
January 26, 2018
Answered

Conditional calculation script in field that allows input if conditions not met

  • January 26, 2018
  • 1 reply
  • 736 views

Greetings,

I am new to the group and to JavaScript and I am trying to create a calculation script on a field that if two conditions are not met, the field allows the user to enter data manually.

In the script below, the calculation is performed if a value is entered in Field1 but I'd like it to allow the user to manually enter data into the field if no data is entered in Field1 and Field2 (and no calculation is performed).  I hope my question makes sense...  Sometimes there is no value for Field2 so the user must be able to populate the field.  Even an override would work that if the user enters a value into the field, the calculation script is ignored.

// Get first field value (Condition one)

     var f1 = getField("Field1").valueAsString;

// Get second field value (Condition two)

     var f2 = getField("Field2").valueAsString;

// Get Third field value

     var f3 = getField("Field3").valueAsString;

if (f1.length>0) {

// Set this field value equal to the sum in inches

    event.value = (Number(Number(f1)+Number(f3)) * 12).toFixed(2);

}

else {

    event.value = ""; 

}

Thank you,

John

Acrobat Standard 2017 (Windows)

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

A calculation is blocked if event.rc is set to false. This allows the user to enter data into the field that is being calculated,otherwise the calculation always overwrites any user input.

So allow the calculation to proceed as normal and then separately, set event.rc based on your conditions. You don't need the "if" at all.

event.rc = (f1.length > 0);

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
January 26, 2018

A calculation is blocked if event.rc is set to false. This allows the user to enter data into the field that is being calculated,otherwise the calculation always overwrites any user input.

So allow the calculation to proceed as normal and then separately, set event.rc based on your conditions. You don't need the "if" at all.

event.rc = (f1.length > 0);

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jlggpsAuthor
Participant
January 26, 2018

Perfect!  Thank you!