How to check whether users have entered valid information
Introduction
There are a couple of checks that can be performed to ensure users are entering the correct information.
- One of the most simple checks you can do is known as 'stringlength'. This check will alert the users if the text they have entered is not the required length, for example, if a phone number is too short or long. You will see how to use this function below.
- A slightly more sophisticated check is 'patternmatch'. This function will check the text input by the user is the correct combination of letters and/or numbers. See more detailed explanation below.
stringlength
This command tells the intervention how many characters a user has entered into a free text interaction.
This can be useful if you want your users to create usernames that are a specific number of characters.
To use this function you will need to write an error message:
show namemessage if (stringlength (username) < 5)
In this example, the error message called “namemessage” will be shown to users if they have entered less than 5 characters in the interaction called “username”.
patternmatch
The patternmatch
command can be used when you require users to enter a specific combination of characters into a text entry interaction. The logic patternmatch(match,input)
will check if the characters entered by the user (input
) matches the characters that you have pre-defined(match
). Patternmatch
uses something called Regular Expressions to define a pattern of characters. Some basics are described below but for more detailed information, you can find many guides to "Regular Expressions" on the Internet.
Pattern | Example Match | Comment |
---|---|---|
a | a | match a single character. Can be any character except .?+*[]\^$()|{} which are reserved
|
A | A | matches are case-sensitive |
abc | abc | match a sequence of characters |
. | a | . means any character |
a? | ? means zero or one of the preceding thing | |
a* | aaa | * means any number of the preceding thing |
a+ | aaaa | + means one or more of the preceding thing |
[abc]+ | cab | [] means any character in a set |
[^abc]+ | fed | ^ in a [] means none of these |
[0-9] | 1 | specify a range of characters in a set with - |
[A-Za-z]+ | LifeGuide | Any uppercase or lowercase letters |
a(bc)+d | abcbcbcd | () groups things together |
ab|cd | cd | | means 'or' |
N.B. Please note that you cannot use patternmatch with a free-entry text box which is set to password. The text box must be set to Text, which is the default setting.
Example 1
This function may be useful if you have assigned each participant in your study a different code which they will need to enter in order to take part. This will ensure that only users who have been invited to use the intervention can gain access to it. To do this, you will need to write an error message on the page which contains your text entry interaction e.g.
Show nomatch if( not( patternmatch( “study[0-9][0-9]id[0-9][0-9]”, password ) ) )
In this example, the error message named nomatch will be shown to users if they enter a string of characters in the text entry interaction named password which does not fit with the pattern specified in the square brackets.
The string of characters needed in this example is studyxxidxx, where x is any number between 0 and 9.
See also Adding Error Messages (example 7).
Example 2
The patternmatch
function could also be used to direct users to specific conditions, sections or pages within an intervention e.g.
show page1 if (patternmatch(“[0-9][0-9]a[0-9][0-9]”, interaction1))
show page2 if (patternmatch(“[0-9][0-9]b[0-9][0-9]”, interaction1))
In this example, if users enter the string xxaxx, where x represents a number between 0 and 9 they will see page 1 of the intervention. If they enter the string xxbxx they will see page 2 of the intervention.
Instead of 0-9, you could use [a-z], which means the user will need to enter any letter between a and z in the alphabet. You could also specify [a-z0-9] – this will mean the user will need to enter either a letter or a number.
Please note that the patternmatch
function is case sensitive. If you specify [a-z], the user will need to enter a lowercase letter. If you specify [A-Z], the user will need to enter an uppercase letter. If you specify [a-zA-Z] the user may enter either an uppercase or a lower case letter.