Validating Answers (Custom Validation)

Validating Answers (Custom Validation)

TABLE OF CONTENTS


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 "Job Code" and Data Name "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! Let's 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.
  • substr({{jobCode}}, 0, 4) = 'JOB-'
    Next we want to ensure that the job code provided starts with the prefix "JOB-".
    So we use substr() 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 instead. 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.

Example: Social Security Number

To validate a field to format for social security number, with the format XXX-XX-XXXX, where each X is a number and dashes are explicit, consider using the following formula:

REGEX({{SSN}}, "^\d{3}-\d{2}-\d{4}$") 

In the above, {{SSN}} is the name of the field containing your social security number. You may also decide to put in the following properties for your form, providing for the best end user experience:

Title Text: Social Security Number
Hint Text: Please use the format 123-45-6789
Placeholder Text: XXX-XX-XXXX
Validation Message: Social Security Number must be XXX-XX-XXXX

Download the Excel form attached and import it to a brand new form and try it out!

Example: Time

In cases where you want the user to manually enter a time into a field rather than using the date field, which will always pre-populate, consider using the following RegEx formula in a text field:

REGEX(., "^(0?[0-9]|1[0-9]|2[0-3])\:[0-5][0-9]$")

This uses the 24-hour format and will flag an error if the user enters an invalid time value.


Example: Multiple Email Addresses

The answer format of a text field enforces validation against the text field for a single email address. For cases where you wish to restrict input to potentially multiple email addresses, consider the below:

REGEX(., '^[\W]*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]+[\W]*[,;]{1}[\W]*)*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]+)[\W]*$')

This allows for multiple email addresses separated by either commas or semicolons.


    • Related Articles

    • Custom SQL

      There are two ways in which you can control the flow of data to your SQL tables using one of our SQL form connectors (SQL Server, MySQL and PostgreSQL), "Auto Maintain" and "Custom SQL" In this article, we'll look at "Custom SQL" and how you can user ...
    • Dynamic Features

      The Form Designer provides advanced functionality such as: Custom Invalid Messages Your own message to display to the App User if the answer to the question is invalid. Conditional Logic (Visibility of Questions) Whether or not a question or group of ...
    • Mapping- Custom Layers

      Mapping - Custom Layers With this feature, you can now add visualization layers from your GIS (Geographic Information System) to provide context and information to your users. The layer data must be provided in GeoJSON format. Once in GeoJSON format, ...
    • Custom Icons

      We provide icon customization areas that allow you to choose from a few built-in icon sets as well as the ability to replace icons with your own custom images. All custom icons replacement images MUST be 192 x 192 pixels in dimension and must be PNG ...
    • Mapping - Custom Tiles

      To understand the implications and uses of this feature, you'll fist need to understand what map tiles are. Briefly, web maps use a tile server to obtain map tiles for display. A Map Tile is a small, square image that represents a portion of a map. ...