Calculations

Cognito Forms supports robust calculations that allow you to create powerful and customized forms. Calculations can be used to compute field values, set default values, conditionally control visibility of fields/sections, and implement rich validation, like conditionally required fields and calculated ranges.

The basics

In the Calculation box of a field, you can enter a constant/literal value, like so:

Entering a literal value.

Or, you can enter a more complex calculation. Calculations must start with an equal sign (=) in order to calculate a value:

Entering a calculation.

For example, for the default value of a Number field, you could either enter 2 or enter = 1 + 1. Both are equivalent, but the first is a constant value and the second is a calculation. For the default value for a Date field, you could either enter “1/1/2015” or enter “Today”. The first would always be the constant date you specified, whereas the second is a calculation that always represents the current date.

Set the field default value to today's date.

Cognito Forms supports auto-completion to show you the values that are appropriate to enter in the calculation, including field names, names of functions, etc.

Auto-completion

Similarly, you can type a “.” to access a field or function on a section or value.

Type a "." to access a f field or function.

Cognito Forms also supports an error indicator to help you know when a calculation is not working and exactly where the calculation stopped working.

Calculation error message.

If your form contains any sections, repeating sections, or Lookup fields, refer to the instructions below to learn how to reference calculation values across different levels of the form.

Conditional logic

Cognito Forms provides a builder for creating complex conditional calculations (those that evaluate to true or false), such as when you are controlling the visibility of a section or making a field conditionally required. The builder lets you specify conditions for one or more fields, in an intuitive user interface that eliminates the complexities of writing the calculations yourself. Though, you can always switch to the Advanced Editor to see what these calculations look like behind the user interface. Learn more about what you can do with conditional logic.

Conditional logic builder.

Working with Yes/No

Cognito Forms makes it easy to add Yes/No fields to any form, but the real secret here is that these fields are also great to use in calculations. Unlike Choice fields that are really just Text values, Yes/No fields represent actual Yes/No values, called Boolean values. Also, many of the calculation operations and functions return Yes/No values, so it is important to understand what you can do with these types of values within your calculations.

Working with Yes/No

Type Example Results for GolfOnFriday = false
Equals =GolfOnFriday = true false = true = false
Not Equals =GolfOnFriday != true false != true = true
Not or Negation =!GolfOnFriday !false = true
Anding =GolfOnFriday and true or =GolfOnFriday && true false and true = false
Oring =GolfOnFriday or true or =GolfOnFriday !=true false or true = true
If Yes Then This Else That =GolfOnFriday ? 25 : 0 or =if GolfOnFriday then 25 else 0 False ? 25 : 0 = 0

Also, just like mathematical calculations, you should put parenthesis () around your Yes/No calculations, especially when using Or (a or b), or And (a and b), or the (Yes/No ? Yes Value : No Value) operations.