When it comes to validating user answers, you have the following standard options:

  • Required - forces the user to provide *something* as an answer
  • Date/Number Range - allows you to easily set lower & upper limits for date and number answers
  • Answer Format - found on Text field types only, this enforces basic formatting requirements on the answer value

However, imagine if your Form involved capturing a job code into a text field.

The user should always enter a known job code, but the truth is that 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 should 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.

False 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.

IMPORTANT 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.

Let's go through the steps needed to provide some custom validation for the Job Code scenario described above.

Open up a new or existing Form in the Form Designer, and perform the following steps:

1. Add a text field to your Form, with Title of "Job Code" and Data Name of "jobCode".

2. Find the Custom Validation property on this Job Code field.  

It's located at the bottom of the properties list, in the Advanced section.

3. Now we need to 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-'

Woah that's a complicated formula!

Lets break it 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.

4. Find the Invalid Message property on the new field.  

It's located near the top of the Advanced 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.

Let's rather show a more custom message.

So set the Invalid Message property to something like "You must provide a valid Job Code in the format JOB-XXXX".

This way the user understands how they must fill out the Job Code field.

All done!

Save your Form and Test it on your device to see your Custom Validation in action :)