Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Excerpt

Validation Conditions are used to require that a user enters a certain type of response. A simple example could be requiring that a date be in the future, all the way through writing complex expressions. These help guide users in their responses

...

The sections below focus on how to help validate answers in your CommCare application

, leading to a better user experience and data quality.

Table of Contents:

Table of Contents
minLevel1
maxLevel32
outlinefalse
stylenone
typelist
printabletrue

...

In the Validation Message field, enter the desired message that will pop up if the entered information does not meet your validation condition.  You can enter a different message for each language of your application.  If you do not specify a validation message the user will see a generic message like "The response is out of range." It is strongly recommended to always use a useful validation message.

Examples

...

This Validation makes sure that the phone number entered contains 10 digits.

...

Complex Example

Icon

This Validation makes sure that the date of birth is both in the past, and between the ages of 10 and date of birth is both in the past, and between the ages of 10 and 59. 

...

Advanced Validation Conditions

This page contains advanced information.

Icon

If you are looking for basic information on validation conditions please visit the Basic Validation Conditions section.

 
Sometimes  Sometimes there are inputs which require not only a valid range (IE: between 96 and 105), but a specific number of significant figures (96.6, not 96.65 or 96) or other specific structure (A1234). To set this up, you can use a constraint in the Validation Condition section for a field with a Regular Expression.

...

  • It is important that the input type for this question be text, not number, integer, or decimal; therefore you should use the question type "Text" or "Numeric ID/Phone Number" (this question type is technically text with a numeric keyboard appearance). Numbers are represented and compared by their numeric value, so "65.00" and "65" are equivalent. 

  • These validation example often don't validate the actual size of the number at all. You can similarly restrict the values before the decimal sign in your expression using regular expressions, but remember that if it is difficult to express whether a value is valid, users may have a hard time entering the correct value.

  • It is possible to restrict the input range in a language that uses different Unicode characters, to do that you would input the range of Unicode characters for that language in the regex expression

...

ExamplesExamples 

 

Example 1: Require a specific number of significant figures

Icon

To require an input with an arbitrary size, but always with two significant figures, you can use the following:

...

Invalid Inputs: 34, 0.0, 2.3, 34.2222 

Enter into Validation Condition = regex(., '^[0-9]*\.[0-9][0-9]$' )

the two [0-9] elements at the end are the placeholders for the significant figure digits. You can add or remove more to manipulate the number of digits

Example 2: Require up to a specific number of significant figures

Icon

 If you want to specify that the expression can have up to two significant figures the expression is somewhat more complex:

...

Invalid Inputs: 34.444, 34.0000, 23. (with nothing after the ".) 

Enter into Validation Condition = regex(., '^[0-9]*(\.[0-9][0-9]?)?$')

 

In this case, you can add additional significant figures by appending more [0-9]? elements after the final one

Example 3: Require certain types of significant figures

...

 If you want to specify that expression can end in either .0 or .5, the expression is again somewhat different:

...

Invalid Inputs: 20, 20.2, 97.7 

Enter into Validation Condition = regex(.,'^[0-9]*\.(0|5)$')

 

Example 4: Require a phone number to be entered in a specific format

...

 If you want your phone number to appear in the format 123-456-7890 you can use the following validation condition:

 

regex(.,'^[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$')

...

regex(.,'^[0-9]{3}-[0-9]{3}-[0-9]{4}$')

Example 5: United Kingdom NHS Number

...

The United Kingdom NHS number is a 10 digit number (9 real, 1 check digit). It is validated using the Modulus 11 algorithm Details about it are here: http://www.datadictionary.nhs.uk/version2/data_dictionary/data_field_notes/n/nhs_number_de.asp?shownav=0 check digit). Here's the expression syntax to validate it (assuming your Question ID is 'NHS_Number'):

11 - (substr(#form/NHS_Number, 0, 1) * 10 + substr(#form/NHS_Number, 1, 2) * 9 + substr(#form/NHS_Number, 2, 3) * 8 + substr(#form/NHS_Number, 3, 4) * 7 + substr(#form/NHS_Number, 4, 5) * 6 + substr(#form/NHS_Number, 5, 6) * 5 + substr(#form/NHS_Number, 6, 7) * 4 + substr(#form/NHS_Number, 7, 8) * 3 + substr(#form/NHS_Number, 8, 9) * 2) mod 11 = substr(#form/NHS_Number, 9, 10)For more examples of phone number and email address validation, see the App Email and Phone Number Input Guidance section.

Icon

Specific Validation Condition Examples

...

Regular expressions are used frequently in Computer Science, but they are not something that you find in the real world. Wikipedia as  There is a nice article on the history and use of regular expressions at https://en.wikipedia.org/wiki/Regular_expression. They are simply a sequence of characters that define a pattern. Sometimes, you will also see them referred to as "regex". While it's easy to say what regular expressions are, creating the sequence of characters can be tricky.

...

If you would like to test a specific Email Address you can use this https://regex101.com/.

Validating Phone Numbers in CommCare Apps with Regular Expressions

...

If you would like to test a specific phone number you can use this this: https://regex101.com/.