To validate user answers, you have the following options:

  • Required- this forces the user to provide an answer.
  • Date/Number Range- this allows you to set lower & upper limits for date and number answers.
  • Answer Format- this is found on text field types only. It enforces basic formatting requirements on the answer value.

Say your form involves capturing a job code into a text field. The user will need to enter a known job code, or they can put in any textual value. We know that a valid job code looks like 'JOB-XXXX', where the XXXXs are a number value.

Wouldn't it be cool if we could validate their input to ensure the value at least contains the text 'JOB-'? This is when you would use the custom validation property.

Custom validation is a general purpose property that lets you define a form formula to check whether an answer value is valid. A true outcome means that the field is valid and the user can proceed. A false outcome means that the field is not valid and the user needs to provide a valid answer.

Custom validation gives you the freedom to define pretty much any logic to validate an answer value.

Note: Custom validation only works when there is a value entered in the field. To force the user to input a value, use the required property together with custom validation.

We'll go through the steps needed to provide custom validation for the job code scenario described above.

1. Open a new or existing form in the form designer. 

2. Add a text field to your form, with title of 'Job Code' and data name of 'jobCode'.

3. Scroll down to the Custom Validation property. It's located at the bottom of the Validation and Behaviour properties. 

4. Create a formula that will give a true or false result. So, when the formula result is true, the field will be considered valid. When the result is false, the field will be invalid.

Enter the following formula into the custom validation property:

string-length(#{{jobCode}}) = 8 and substring(#{{jobCode}}, 0, 4) = 'JOB-'

Lets break that down:

  • string-length(#{{jobCode}}) = 8 First we want to ensure that the answer is exactly 8 characters long.
    Remember we expect job codes to be entered in the fixed format 'JOB-XXXX'. This is a total of 8 characters long.
  • substring(#{{jobCode}}, 0, 4) = 'JOB-' Next, we want to ensure that the job code provided starts with the prefix 'JOB-'. So we use substring() to grab the first 4 characters of the answer and check if it does indeed match the required prefix.
  • We use the and logic operator to ensure that both the above conditions must be met in order to return a TRUE result.

To understand more about how to create a formula, see the Creating a Formula help page.

5. Located near the top of the Advances properties, find the Invalid Message property on the new field. By default the app will show a generic 'Job Code is invalid' message to the user if the custom validation property result is false.

To show a more custom message set the invalid message property to something like 'You must provide a valid Job Code in the format JOB-XXXX'. This way the user will know that they must fill out the Job Code field.

Save your form and test it on your device to see your custom validation in action!

Did this answer your question?