Create Ticket My Tickets Post Discussion
Welcome
Login  Sign up

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

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.