Cognito Forms supports robust calculations to allow you to create really powerful customized forms. Calculations can be used to:
- compute field values
- set default/initial values
- conditionally control visibility of fields and sections
- implement rich validation, like conditionally required fields and calculated ranges
Anywhere you can enter a calculation, you can either enter just a constant/literal value or you can enter a calculation. Calculations must start with an = in order to tell Cognito Forms that you are trying to calculate a value.
For example, for the Default Value of a Number field, you could either enter 2 or enter = 1 + 1 –both are equivalent, where 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 = DateTime.Today –the first would always be the constant date you specified, whereas the second is a calculation that always represents the current date!
Calculations Made Easy
In order to help you write great calculations, 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. For developers, this form of statement completion is now expected, and for non-developers it is a great way to learn and understand what you can do when writing calculations.
Also, you may on a rare occasion create a calculation that is slightly wrong. In this case, Cognito Forms helps you understand the error of your ways by displaying an error message and telling you exactly where things went amiss. You can click on the error to take you directly to the location of the error in your calculation.
Finally, 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, shielding you from the complexities of writing the calculations yourself—though you can always take a peek at the Advanced Editor to see what these calculations look like under the hood.
While the builder works great for most conditional calculations, other types of calculations (think math) or really complex conditions require actually understanding how to write the appropriate calculations by hand. The following sections describe how to write calculations dealing with text, numbers, dates, yes/no’s and lists of items (repeating sections or tables).
Working with Text
Most data collected via forms is usually some form of text. For example, the Textbox, Choice, Rating Scale, Name, Address, Phone, Email, Website, and Calculation (Text) field types all represent text values.
First and foremost, you can specify constant text values in your expressions by simply putting text inside quotes, "like this". Also, text fields that are blank will have a value of null.
Check out our help topic for comprehensive documentation of text functions supported by Cognito Forms.
Working with Numbers
Cognito Forms makes it easy to write calculations that work with numbers. First off, start by using the appropriate field types to represent numbers, including Number, Currency and Calculation (Number, Percent & Currency).
Say, for example, you have two Number fields on your form, Adults and Children, representing the number of adults and children registering for an event. You could then add a Calculation field that shows the total number of people attending the event, using the simple calculation =Adults + Children .
When performing basic mathematical calculations, Cognito Forms will treat blank values as zeros, so for example, 2 + blank would equal 2, and 2 > blank would equal true.
Additionally, you can perform more complex mathematical calculations by calling functions starting with Math. , including important ones like Min, Max, Ceiling, Floor and Round. For example, you could write the calculation = Math.Max(Adults, Children) to calculate the maximum of these two values. Check out our help topic for comprehensive documentation of numeric functions supported by Cognito Forms.
Working with Dates
Dates are an essential component of any form involved with scheduling of activities or recording prior events. Cognito Forms makes it easy to capture real, valid dates, while also providing a rich set of calculation options to validate and manipulate these dates. To capture Date input, just add a Date field and select the Date type (instead of Time). You can also create Calculated fields of Date type.
Refer to our help topic for comprehensive documentation of Date and Time functions supported by Cognito Forms. Also see Timespan Functions for documentation of what you can do with the result of subtracting two dates or times, such as the preceding subtraction example.
Working with Yes/No (Boolean Values)
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 by developers like me). 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.
Also, just like mathematical calculations in school, 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.
So there you have it! Calculations at a glance. Use your new knowledge to try Calculations out today – for free! For more information on Calculations and how they work with Repeating Sections, visit our help topic.