Introduction
Dynamic Operations is a very powerful feature that allows you write custom JavaScript be included in a question in your TaroWorks form.
You can write custom JavaScript to run as either a calculation or a validation:
- Calculations will pre-populate a response to the question on the form, e.g. automatically display a single select value or automatically set a date value to Today's date. The calculation is run when the page is loaded.
- Validations will validate the response to a question that is entered by the mobile user, e.g. validate that a phone number only contains digits. An error will be thrown if the response entered does not meet the validation rule set preventing the user from proceeding to the next page.
The validation will run when a user attempts to proceed from a page. Validations only run when there is an answer/response to run on and must therefore be stored on a question that is required.
Validation | Calculation | |
Action | Display custom error message | Automatically generates responses to questions |
Use of other responses | Yes | Yes |
Response editable | Yes | No |
Execution | User clicks next/leaves page | Page is loaded |
A. How do Dynamic Operations work?
You can configure a Dynamic Operation on any form question type except Static Content and End of Form. When you save the JavaScript in your question, the system will confirm that the code is valid JavaScript.
The syntax to refer to a question in a regular section is:
tw.sectionname.questionname.value
The syntax to refer to a question in a repeat section is:
tw.repeatsectionname.current.questionname.value
or
tw.repeatsectionname[i].questionname.value
where i represents the iterations of the repeat section starting from 0.
Starting TaroWorks 6.5, you have the option to test the JavaScript code snippet and ability to validate that all the references on the JavaScript code exist.
Starting TaroWorks 7.4, you have the option to determine whether Dynamic Operations should be run or not when the question is hidden via the “Run Hidden Dynamic Operations” checkbox.
Functions
Function | Description |
tw.sectionname.questionname.value |
Returns the value of the response to a question |
string.length |
Returns the length of the response |
new Date() |
Returns today’s date |
tw.sectionname.questionname.isNaN |
Checks whether response is not a number |
tw.sectionname.questionname.hasAnswer |
Checks whether a question has a response. This function cannot run on local variables, the syntax to the left is required. It also can't run on an optional question. |
Operators
|
Methods
JavaScript Method | All in one Example |
Array functions |
var cars = ["Saab", "Volvo", "BMW"]; total = total + "Boolean " + Boolean(10 > 9); total = total + "Gloabl " + isNaN(123); var myObj = { "name":"John", "age":31, "city":"New York" };
|
B. How to Add a Calculation Operation to a Form question
- Click on the Forms Tab.
- Create or Edit a Form.
- Create or Edit a Question.
- Check Add Dynamic Operations Using JavaScript.
- Select Calculation for your Operation.
- Enter your custom JavaScript or copy and paste from the code below.
tw.register.registration_date.value= new Date();
- Click Save.
Response on Mobile
C. How to Add a Validation Operation to a Form question
- Click on the Forms Tab.
- Create or Edit a Form.
- Create or Edit a Question.
- Check Add Dynamic Operations Using JavaScript.
- Select Validation for your Operation.
- Enter your custom JavaScript or copy and paste from the code below.
if (isNaN(tw.register.valid_phone_number.value))
{
throw "Phone Number should consist of only digits";
}
if (tw.register.valid_phone_number.value.length !== 10)
{
throw "Phone Number should be 10 numbers long";
} - Click Save.
Response on Mobile
Comments
0 comments
Please sign in to leave a comment.