Your organization will need to have Dynamic Surveys enabled to use this feature. Click here for more information.
This feature allows you to define a content requirement for the Open Field question element. You can require the given response to contain a certain word, contain at least one of a few words, or match with a complex pattern of characters and numbers.
1. Open a survey and create an Open Field question element.
2. Enter the element settings from the gear menu.
3. Select "Specific Format" from the "Input type" selection.
4. You will see this view:
General
A Regular Expression is a string that specifies a requirement against which another string can be tested. Regular Expressions can check very simple requirements, such as the input ending in a particular word, or very complex requirements, such as the ISBN number of a book.
In this setting for the Open Field element, there are two kinds of expressions you can use: Simple Expressions and Full Expressions. Simple Expressions work when you only need to check that the input contains, ends with or starts with a specific word. If you need to check for the number, type and positioning of individual characters, you need Full Expressions.
Settings in the settings window
- Regular expression type: this lets you choose whether you want to use Simple Expressions or Full Expressions for this Open Field element.
- Expression: This is the validation expression itself.
- Case sensitive: When this option is checked, the expression also checks letters for uppercase and lowercase.
- Test: This lets you test your given expression with different kinds of inputs that you can think of.
- Language for error message and Error message: You can set an error message for an invalid input for each language that the survey is in. This message will be displayed to the responder if their input does not satisfy the given expression.
Simple Expressions
In Simple Expressions, you can use the asterisk * to mean "any string", including no string at all. This can be used to check, for example, that the given input is an email address from a certain domain.
- *@domain.com
- This expression requires that the given input ends in "@domain.com", i.e. is an email address from the given domain.
You can also include alternative simple expressions by separating them with " OR ". For example, if your company has multiple domains for email addresses, you can require the input to be an email address from one of them.
- *@company.com OR *@yritys.fi OR *@bolaget.se
- This expression requires that the given input is an email address from either company.com, yritys.fi or bolaget.se.
If you use the "OR" keyword without giving two expressions around it, it is considered an invalid expression.
N.B. The keyword "OR" is familiar to many people from database search queries. This may make some users also want to try other such keywords, such as "AND". However, there are no other keywords for Simple Expressions in this feature besides "OR", as anything that could be done with "AND" will either result in contradictory requirements or requirements that can be expressed without the "AND" as well.
- *@company.com AND *@lacompagnia.it
- This expression would require that the input end in both @company.com and @lacompagnia.it, which is contradictory and not possible.
- firstword* AND *lastword
- This expression would be identical in effect to firstword*lastword.
Full Expressions
In Full Expressions, the whole scope of JavaScript Regular Expressions is usable. Here we describe some of the features that are the most commonly needed in this question type.
Start and end
The beginning of the input is marked with ^ and the end is marked with $.
It is common to require the input to be only the specified content, instead of just containing the specified content among other content.
For example, ^ABC$ would require the entire input to be just "ABC".
Wildcards and character classes
Wildcards are characters that represent classes of characters, for example "any number" or "any space character". In fact, the asterisk in Simple Expressions is a wildcard, and it represents "any string".
- .
- In general, the period stands for "any single character". Exceptions are line separators. For example, .s matches with "nt" and "it" in "currently sitting".
- \d
- Backslash-d stands for a single Arabic numeral digit, i.e. the number characters 0-9. For example, \d matches with "4" in "A4 paper size".
- \w
- Backslash-w stands for a single alphanumeric character from the standard Latin alphabet, i.e. number 0-9 and letters A-Z, both uppercase and lowercase.
- [ ]
- Square brackets can be used to create your own character classes. For example, [AEIOU] would only match with any single English vowel letter in uppercase.
- You can use hyphens to define ranges, such as [D-H] to only match with a single letter within D to H in uppercase.
- You can also combine these. For example [D-H13579] would only match with a single letter within D to H in uppercase, or a single odd digit.
- The preset character classes can also be used within square brackets. For example, [AEIOU\d] would only match with a single English vowel letter in uppercase or any single digits.
- Note that ranges of digits check for individual digits, not numbers. A range like [0-99] means any digit from 0 to 9, and a superfluous 9. It does not mean any number from 0 to 99.
Repetitions
There are ways to define that a single character or character class should be repeated. When you insert a repetition marker, it means that the preceding character or character class is repeated a specified number of times.
- *
- The asterisk matches with the preceding character or character class any number of times, including zero times. For example, o* would match with "gal", "goal" and "gooooooooooooal".
- +
- The plus sign matches with the preceding character or character class at least once. For exampe, o+ would match with "goal" and "goooooal" but not "gal".
- { }
- The curly braces let you define a custom number of times for the preceding character or character class to appear.
- {n} means that the preceding character or character class needs to repeat exactly n
- {n,} means that the predceding character or character class needs to repeat at least n
- {n,m} means that the preceding character or character class needs to repeat at least n times and at most m
These can be combined with the given wildcards and character classes. Examples:
- \d{4}
- would match with a digit for exactly 4 times. The exact digit can be different.
- [AEIOU]+
- would match with an English vowel letter at least one time.
- .*
- would match with any character any number of times, i.e. any string at all, including no string. This is the Full Expressions equivalent of Simple Expressions *.
Groups and alternatives
Characters in a row can be combined so that a repetition marker covers more than a single character. A group is surrounded with parentheses ( ). For example:
- (five){5}
- would match with the word "five" repeated at least five times.
Alternatives within a group can be separated with the pipe character |.
- (wide|full)screen
- would match with either "widescreen" or "fullscreen".
Groups can of course be grouped as well, and can be alternatives to each other.
- ((five){5}|(four){4})
- would match with either "five" repeated five times or "four" repeated four times.
Escaping
If you need to match with one of these special characters as itself, add the backlash \ before that character. For example, \. will specifically search for a period rather than "any single character".
Note that the backslash itself is a special character. If you need to search for the backslash, you need to add another backslash before it: \\.
Building the expression and sample expressions
These directives can be stringed together, and they will be tested against all parts of the given input. If a single match is found, the input is considered to match with the expression.
For example, let's build an expression that requires the input to alternate between a vowel letter and a digit three times.
A vowel letter can be expressed as [AEIOU] and a digit is expressed as \d. Therefore a combination of a vowel and a digit is simply a combination of these: [AEIOU]\d.
To repeat these, we need need to group them: ([AEIOU]\d).
Finally, add the repetition directive: ([AEIOU]\d){3}.
This expression will match with any input that includes a portion where there is a three-time alternation of vowels and digits. If you need to only allow an input that is nothing but this sequence, add the string start and end characters: ^([AEIOU]\d){3}$
Here are some example expressions. Some of them are quite simple, but some of them can get very complicated, and even use some advanced features that haven't been covered in this article.
- ^((0[1-9])|(1[0-2]))[-\/]\d\d$
- would only match with a credit card expiration date.
- ^[A-ZÅÄÖ]{1,3}\-([1-9]\d{0,2})$
- would only match with a Finnish car registry number.
- ^((.*)@company\.com|(.*)@lacompagnia\.it)$
- is the Full Expressions equivalent of Simple Expressions *@company.com OR *@lacompagnia.it.
- ^(?:ISBN(?:-1[03])?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$
- would only match with the ISBN number of a book.
- Note that the ISBN format includes a checksum, i.e. one of the characters varies based on the other characters in order to prevent misspelling. Regular Expressions cannot implement checksums, so this expression only checks for the general format.
- ^((((0?[1-9])|([1-2][0-9])|(30))\.((0?)4|(0?)6|(0?)9|11))|(((0?[1-9])|([1-2][0-9])|(3[01]))\.((0?)1|(0?)3|(0?)5|(0?)7|(0?)8|10|12))|(((0?[1-9])|([1-2][0-9]))\.((0?)2)))\.(19([0-9][0-9])|20([0-1][0-9]|20))$
- would only match with a date in the DD.MM.YYYY format from 1900 to 2020.
- This expression will not check for leapdays outside leap years.