custom calculation script: display blank unless previous field is entered
Copy link to clipboard
Copied
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 = "";
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
})()
Copy link to clipboard
Copied
These // comments may be confusing since I modified the script
Copy link to clipboard
Copied
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;
}
})()

