4 Replies Latest reply on Mar 21, 2011 4:38 PM by tom.beyerdesign

# Calculating a field with a minimum for answer?

Hello all -

I have a product that costs \$22 per thousand. I have a field for quantity, and a total field.

Right now I have it set up like this:

Field 1: "Qty 3" - customer enters desired qty

x

Field 2: "22" - hidden, used for multiplier

x

Field 3: "001" - hidden, .001 used for multiplier to give total price.

My problem is that the minimum price is always \$22. How do I make the total say: "If total is < or = 22, then 22. Else, Total."

• ###### 1. Re: Calculating a field with a minimum for answer?

You can use the following as the field's custom Calculate script:

```(function () {

// Get the quantity
var v = getField("Qty 3").value;

// Set the price per thousand
var costPerK = 22;  // Cost/thousand

// Set the minimum cost
var costMin = 22;

// Calculate the total cost
var costTotal = v * costPerK / 1000;

// Set this field to the total cost if it's more than the minimum, otherwise use the minimum
event.value = (costTotal > costMin) ? costTotal : costMin;

})();
```

You don't say what you want to happen if the Qty3 field is blank. If you want the calculated value to be blank, add this after the line that gets the quantity:

```if (v == 0) {
event.value = "";
return;
}
```
• ###### 2. Re: Calculating a field with a minimum for answer?

Thank you sir! Perfect!

I also have several other fields that are a qty x price = total. These all default to \$0.00. Is there a simple way for these all to display blank if there is no qty entered, as in your answer above?

• ###### 3. Re: Calculating a field with a minimum for answer?

If you're using one of the built-in methods to perform the calculation, as opposed to a custom calculation script, you can add the following custom Validate script:

```// Custom Validate script
if (+event.value === 0) event.value = "";
```