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

custom calculation script: display blank unless previous field is entered

Community Beginner ,
Sep 16, 2021 Sep 16, 2021

I have successfully added a custom calculation script to keep the fields blank until the necessary corresponding fields are filled, and then to remain blank if the calculation is <= zero. I would prefer the calculation fields to start blank, but once the calculations occur, display the total or zero if the result is <= zero. This way, if the recipient decides to print the form rather than fill it digitally, the lines won't display a zero, they will be blank for them to fill by hand. Example of my script:

event.value = this.getField("line5").value-this.getField("line6").value;
if(event.value <= 0)
    event.value = "";

 

 

 

TOPICS
How to , JavaScript , PDF forms
1.3K
Translate
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
Community Expert ,
Sep 16, 2021 Sep 16, 2021

I am not sure if you're asking a question to a problem or actually stating a solution.

 

The script work fine on my end.

Translate
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
Community Beginner ,
Sep 16, 2021 Sep 16, 2021

I was able to find this script in another thread to accomplish this. However, I'm guessing there is a way to simplify this since the fields are only used for numbers.

(function () {

    // Get the input field values, as strings
    var sA = getField("line5").valueAsString;
    var sB = getField("line6").valueAsString;

    if (sA && sB) {  // If both of these input fields are not empty...

        // If both of these field values are zero when converted to a number...
        if (sA - sB <= 0) {
            event.value = 0;  // Set this field value to zero
            return;
        } else {
            event.value = sA - sB;  // Set this field value to this product
            return;
    }

    } else {
        // Blank this field since inputs are blank
        event.value = "";
        return;
    }

})()

 

Translate
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
Community Beginner ,
Sep 16, 2021 Sep 16, 2021

These // comments may be confusing since I modified the script

 

Translate
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
Community Beginner ,
Sep 16, 2021 Sep 16, 2021
LATEST

This is probably more correct for just number fields? 

(function () {

    // Get the input field values
    var sA = getField("line5").value;
    var sB = getField("line6").value;

    if (sA && sB) {  // If both of these input fields are not empty...

        // If both of these field values are zero when calculated...
        if (sA - sB <= 0) {
            event.value = 0;  // Set this field value to zero
            return;
        } else {
            event.value = sA - sB;  // Set this field value to the result of this calculation
            return;
    }

    } else {
        // Blank this field since inputs are blank
        event.value = "";
        return;
    }

})()
Translate
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