Introduction
Form fields can be computed using formulas. These formulas may reference other fields in your form using variables. Formulas are written in JavaScript and executed by the browser when filling out the form. These formulas and variables together are called Form Calculations.
- For formulas to function properly, JavaScript must be enabled in the user's browser.
- Calculations can only be used in text fields.
- While FormAssembly does not have limits on the use of this feature, extensive use in combination with other, similar features may impact performance on respondent browsers and devices. Before you begin building advanced forms, we recommend reading through our Best Practices in Form Building document to learn more about our recommended planning and testing practices.
Step 1: Define your Variables
A variable is a value that can be defined by an answer given in your form. A variable must be assigned to a question before it can be used in a formula. To define a variable, follow these steps:
- Open a form in the Form Builder.
- Click an existing question and use the Options button to open the editing sidebar.
- Expand the Calculations tab. If you do not see a Calculations option, check the box to show advanced options.
- Check the This is a variable box.
- Enter a variable name for the question. Variable names may not contain spaces or non-alphabetic characters. Please see the note below under "Create a Formula" for additional details on how to add a space if needed.
- If you selected a multiple-choice question, you must provide a value for each choice.
- Variables may not include numbers.
- Variables are case sensitive.
- JavaScript Reserved Words: https://www.w3schools.com/js/js_reserved.asp
Step 2: Create a Formula
A formula is evaluated as the form is filled out, and the form is submitted with the computed value.
Form formulas are single-line JavaScript equations. They are mostly used to perform simple arithmetic, such as computing a total in an order form.
These formulas are added to text fields that have been marked as calculated fields within the form. Once a field has been marked as a calculated field, a respondent is then unable to directly edit that field.
To create a calculated field:
- Open a form in the Form Builder.
- Click an existing question and use the Options button to open the editing sidebar.
- Expand the Calculations tab. If you do not see a Calculations option, check the box to show advanced options.
- Check the box for This is a calculated field.
- Enter an equation into the Formula text field. You can use any variables you have defined from step 1 above.
- You can concatenate a string of variables by using "\u0020" to represent a space.
- For example, variableONE+“\u0020”+variableTWO+“\u0020”+variableTHREE
- You can also add spaces to a variable value by adding \u0020 in the value.
- For example: "Variable\u0020One" Will be rendered as "Variable One"
Video Tutorial
Examples
Simple formula performing arithmetic
(a + b) / c * d
90% of the variable A, rounded to two decimals
(A*0.9).toFixed(2)
Calculating a Body Mass Index (BMI)
height?((weight/(height*height))*703):0 // This example prevents a division by zero error by testing // the height value first. It returns 0 if it's undefined or zero.
Sample If/Else Calculation
if(variableA=="Selection\u0020A"){variableA}else{variableB};
Sample If/Else/Else If Calculation
if(variableA=="Selection\u0020A"){variableA}else{if(variableA=="Selection\u0020B"){variableB}else{variableC}};
Tutorials
FAQ
I'm using Zip Codes in calculations, but the leading zero keeps getting stripped. How can I keep it at 5 digits?
if(zip){('00000'+zip).substr(-5);}else{""};
Replace "zip" with your variable.