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.

If your form involves capturing a job code into a text field the user will need to enter a known job code, or insert 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 you could validate their input to ensure the value at least contains the text 'JOB-'? You can! This is when you would use the custom validation property.

Custom validation is a general purpose property that allows you to 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.

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.

Here are the steps 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. 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. Find the 'Invalid Message' property on the new field. It's located at the top of the advanced properties section. 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?