Sometimes an expected text response needs to conform to existing rules, e.g. a beneficiary ID number such as "adtqr" that had previously been assigned by the program prior to distribution. KoBo allows restricting possible responses to text questions (and technically any other question) by using the very powerful regular expression syntax, or regex.
 To use a regex to restrict possible options, follow these steps for your text question:

  1. Go to the question's Settings
  2. Go to Validation Criteria, choose the manual syntax option
  3. Enter your regex between the quotation marks of this formula regex(., " ")

For reference, "." (the period) refers to 'this question', while the regular expression inside the quotation marks needs to conform to the established regex rules. To build your regular expression see for help and examples.


Below is an example of how to restrict text entry to exactly five lowercase letters (e.g. abcde) using the code regex(.,'^([a-z]){5}$'):

Other examples:

  • Only allow a number of nine digits (e.g. a phone number): regex(.,'^[0-9]{9}$')
  • For a ten digit phone number use regex(.,'^[0-9]{10}$')
  • If the first number can’t be 0, use regex(.,'^[1-9][0-9]{8}$')
  • Enter a code (e.g. beneficiary ID) consisting of numbers and lowercase letters, up to 6 characters long regex(., '^[a-z]{1,6}$')
  • Only allow a valid email address regex(., '^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})')
  • Enter a beneficiary ID corresponding to a specific existing format, e.g. "mz-00-0000" regex(.,'^mz-[0-9]{2}-[0-9]{4}$') (see screenshot below)
  • Enter text that includes a certain keyword, e.g. regex(.,'^.*keyword+.*$')*

*Note that the Collect app will always check if the entire string of your regular expression matches the text provided in a response. This is equal to writing ^...$ around the regex code. Enketo Express instead will treat the regular expression literal and searches for any matches within the text. See this example for the regular expression regex(.,'[0-9]{9}'):

  • KoBoCollect: The validation is successful and the enumerator can continue to the next question if the response is exactly a 9 digit number
  • Enketo: The response is valid if it contains a 9 digit number. To get the same result as in KoBoCollect simply write regex(.,'^[0-9]{9}$') to indicate that the whole string must be matched. 
Did this answer your question?