Validate Textbox Answer Formats

When using Textbox questions, often times you'll want to validate the data your respondents enter. It makes analyzing, reporting and then acting on your data easier when it all conforms to certain formatting.

Basic Validation

Email addresses, phone numbers, even IP addresses are easy to collect with the proper answer formatting that is built into SurveyGizmo. With a little extra effort, you can create much more complicated validation patterns on your own!

As you initially create a Textbox question, you are given the option to choose from Number, Email, Percent and Date (either MM/DD/YYYY or DD/MM/YYYY) as the type of validation. These choices will automatically apply answer formatting that can be adjusted from the Validation tab.

You can also apply formatting validation to a textbox question after the fact on the Validation tab. If you select a numeric validation, you can choose to Force Whole/Positive Numbers and/or Allow Values Between a minimum and maximum range.

(Optional) When using validation we recommend displaying an example format to your survey respondents. This is particularly useful if you are using complex validation patterns. A good place for this is the additional instructions section of your question title.

To add example formatting, edit your question and click the link to Add Additional Instructions.

Add your example format in the Additional Instructional Text field and click Save Question.

RegEx Patterns for Complex Validation

A Regular Expression pattern or RegEx pattern is a sequence of characters that is used to validate specific string formats in user input. These patterns are commonly used to ensure the data collected is properly formatted.

For example, if you were collecting telephone numbers and it was essential they were valid US telephone numbers, you could apply a RegEx pattern to the question. In doing so, all input for the telephone number field would have to match an (XXX) XXX-XXXX pattern. If the input did not match that pattern, an error message would display indicating that it must match the appropriate format. 

Preloaded RegEx Patterns in SurveyGizmo

Below are the commonly used RegEx patterns readily available inside SurveyGizmo:

Setting Up RegEx

To set up a RegEx pattern on a specific question, select Edit > Validation and click the Answer Format dropdown menu. Choose Regex

Now you will see a field for Regex Validation. This is where you can select the RegEx pattern that you wish to implement or write your own. You can also specify a message to display if the respondent's input does not match the given pattern.

Other Useful RegEx Patterns

If the RegEx pattern you are looking for is not available in the above list, here are a few additional RegEx patterns that are helpful. 

  • Match only email addresses from Gmail: [^ ]*@gmail\.com$
  • Match only serial numbers with 4 numbers, dash, 4 numbers: ^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$
  • Match only first names that start with R or r and have a last name: ^[Rr][^ ] [a-zA-Z]+$
  • Match the numbers 30, 31 or 32: ^30$|^31$|^32$
  • Match any dates in July in the format MM/DD/YYYY: ^07/[0-9][0-9]/[0-9][0-9][0-9][0-9]$
  • Match only pure alpha strings: ^[a-zA-Z]+$
  • Match only letters, numbers and spaces (Removes special characters): ^[a-zA-Z0-9 ]+$
  • Match only letters, numbers and underscores: ^[a-zA-Z0-9_]*$
  • Character Limit, min=1 max=10: ^(.|\s){1,10}$
  • Number between 1-10: ^([0-9]|1[0])$
  • UK ZIP codes: ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$
  • Allow apostrophes in email addresses: ^([0-9a-zA-Z](['-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w']*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$
  • Smart telephone number (allows 1234567891, (123) 456-7891, and 123-456-7891): [(]?[2-9][0-9][0-9][)]?[-| ]?[0-9][0-9][0-9][-| ]?[0-9][0-9][0-9][0-9]
  • UK phone number: ^\s*\(?((\+0?44)?\)?[ \-]?(\(0\))|0)((20[7,8]{1}\)?[ \-]?[1-9]{1}[0-9]{2}[ \-]?[0-9]{4})|([1-8]{1}[0-9]{3}\)?[ \-]?[1-9]{1}[0-9]{2}[ \-]?[0-9]{3}))\s*$
  • US ZIP, US ZIP +4, Canada postal code: ^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$
  • Time in 24 hour format: ^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$
  • Match English characters only: ^[A-Za-z0-9'\.,&@:;?!()$#' ']+$ 

If the RegEx pattern you are looking for is not listed here, you can always write your own RegEx pattern in the Regex Validation field. 

Other Useful RegEx Resources

Test your regular expressions: RegEx Tester

A RegEx library to search other commonly used patterns: RegExLib

Reg Ex Cheat Sheet: Cheat Sheet

Logic and RegEx 

Let's say you asked for respondents' email addresses in a survey. Let's also assume that you wanted to ask only respondents who have Gmail accounts a follow-up question. SurveyGizmo offers two operators: matches regex pattern and does not match pattern for setting up a logic condition like this.

Click Edit on the follow-up question that you want to show to respondents who answered that they have Gmail accounts, then click Logic.

In the Logic tab, you should see a link to Add logic to control when this question is displayed. Give that link a click then select the source question from the first dropdown menu (the question where the email is collected). Now specify if you want the RegEx pattern to be matched or not for the logic to run.

Finally, input the following RegEx code into the third text field to complete the logic:  >[^ ]*@gmail\.com$

This will show our follow-up question only if the email collected ends in "@gmail.com".

HTML 5

The HTML5 formatting type allows you to use HTML5 input types to validate Textbox questions. These input types are simple, one or two-word commands that function in similar ways to RegEx but allow for more flexibility. In the example below, you can see the input type URL inside this Textbox question, validating the response to ensure a URL is entered.

HTML5 Supported Browsers

Not all browsers (or versions) support HTML5. Google Chrome is the most compatible with HTML5, but Firefox is a close second. If you'd like to see what your particular browser can support, please visit HTML5 Test. If you visit the Forms section, you'll see what input types are supported.

Setting Up HTML5 Input Validation

Click Edit > Validation and click the Answer Format dropdown menu. Choose HTML5. Enter the input type you'd like to use in the HTML formatting type field and then click Save Question to see the input type take effect!

Available HTML5 Input Types

We recommend checking out html5 doctor for all the input types that are available: http://html5doctor.com/html5-forms-input-types/. The following are the ones we find most useful for SurveyGizmo customers.

If you'd like to use the URL input type, all you have to do is type URL into the HTML Formatting Type field.

If you want a nice looking date picker, use date.

If you want to mask the input of a password, use password. The password input type is not available on Free or Trial accounts.

We've found that search, datetime, email, tel are not as useful as their RegEx or built-in counterparts and color is very temperamental. If an input type is not supported by the browser viewing that question, it will behave as a regular Textbox.

Basic Standard Market Research HR Professional Full Access Reporting
Free Individual Team & Enterprise
Feature Included In