Skip to main content
Known Participant
October 31, 2023
Question

Using Calculate function And The Text Only Prints when Field Boxes are Filled

  • October 31, 2023
  • 3 replies
  • 1669 views

Hello all,

 

I am still in the process of perfecting an expense sheet for my job.  I would like for the grand total to self calculate when the form fields have data entered into them.  However, if no data is entered and the user just wants to print a form to hand write their expenses I would like for the Grand Total to NOT print.  How do I go about this? I have tried visible but doesn't print and then the calculated total doesn't appear.  I have tried print but hidden and that makes it print out a 0 when no data is entered and you can't see the total on the sheet when using it on a computer.

 

I attached the document for you to see and a .jpg explaining what I am asking to learn how to do.  I appreciate your time and thank you.

 

J

3 replies

JR Boulay
Community Expert
Community Expert
November 1, 2023

You can use this as a Custom Format script, if the field value is zero, it is not displayed (and not printed):

 

if (event.value && !isNaN(Number(event.value)) && event.value != 0) {AFNumber_Format(2, 0, 0, 0, "$ ", true);}
else {event.value = "";}

 

See attachment.

Acrobate du PDF, InDesigner et Photoshopographe
Nesa Nurani
Community Expert
Community Expert
October 31, 2023
Participating Frequently
September 22, 2025

I need help with creating a caluculation that will add (4 fields) three fields, subtract one field for a grand total.  Can you help me?

TOTALONE+TOTALTWO+TOTALTHREE-LESS ADVANCES= GRANDTOTAL.

Nesa Nurani
Community Expert
Community Expert
September 22, 2025

You can't use that in custom calculation script, use the same but under simplified field notation.

Amal.
Community Manager
Community Manager
October 31, 2023

Hi @johnsbox 

 

Hope you are doing well and thanks for reaching out.

 

To achieve the behavior you described, you can use JavaScript to dynamically calculate and display the grand total when data is entered into form fields and hide it when no data is entered. Here's a sample JavaScript code for this purpose

Note: I am not an expert on JavaScript, Please check the code below and see if that helps.

 

<!DOCTYPE html>
<html>
<head>
<title>Expense Form</title>
</head>
<body>
<form id="expenseForm">
<label for="expense1">Expense 1:</label>
<input type="number" id="expense1" class="expenseInput"><br>

<label for="expense2">Expense 2:</label>
<input type="number" id="expense2" class="expenseInput"><br>

<label for="expense3">Expense 3:</label>
<input type="number" id="expense3" class="expenseInput"><br>

<label for="grandTotal">Grand Total:</label>
<span id="grandTotal"></span><br>

<button type="button" id="calculateButton">Calculate</button>
</form>

<script>
// Get references to form fields and the grand total element
const expenseInputs = document.querySelectorAll('.expenseInput');
const grandTotalElement = document.getElementById('grandTotal');
const calculateButton = document.getElementById('calculateButton');

// Add a click event listener to the Calculate button
calculateButton.addEventListener('click', calculateGrandTotal);

function calculateGrandTotal() {
let total = 0;

// Calculate the total by summing the values of filled input fields
expenseInputs.forEach(input => {
if (input.value) {
total += parseFloat(input.value);
}
});

// Display the grand total if it's greater than zero, hide it otherwise
if (total > 0) {
grandTotalElement.textContent = total;
} else {
grandTotalElement.textContent = '';
}
}

// Initially, hide the grand total
calculateGrandTotal();
</script>
</body>
</html>

 

This code creates an expense form with three input fields for expenses, a grand total field, and a Calculate button. When you click the Calculate button, it sums the values of the filled input fields and displays the grand total. If the grand total is zero or no data is entered, it hides the grand total.

 

~Amal

try67
Community Expert
Community Expert
October 31, 2023

This code is way off. It's not even valid Acrobat JS code.

It's better not to post code at all, than to post incorrect code.