• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Jan 25, 2018 Jan 25, 2018

Copy link to clipboard

Copied

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)

TOPICS
Acrobat SDK and JavaScript , Windows

Views

466

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 25, 2018 Jan 25, 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);

Votes

Translate

Translate
Community Expert ,
Jan 25, 2018 Jan 25, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 26, 2018 Jan 26, 2018

Copy link to clipboard

Copied

LATEST

Perfect!  Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines