«

»

Sep 14 2012

How to validate email addresses at Limesurvey

[updated December 2014]
At many surveys you want to ask the user for his/her email address, be it for a lottery or to add the email address to your address data. If you later want to send an email to such an email address, a simple typo or missing @-sign will cause problems and you might not know how to correct these invalid email addresses.
So let’s be careful right from the start and do the email validation right when asking for the data. We will show you different approaches below.

Limesurvey email validation using a regular expression

The easiest way to validate an email address is to use a regular expression which checks for certain syntax details like the @-sign, a dot and a valid domain. You can find such regular expressions (and many more) at the Limesurvey manual section about Regular Expressions.

For this example we ise a simple text question and the following regular expression which has to be added at the “validation” field when editing the question at Limesurvey:

/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/
Limesurvey text validation field

Note that when using regular expression at Limesurvey, these always have to start with a slash (“/”).

Unfortunately, this solution can’t auto detect typos, so let’s create an advanced example at which the user has to verify the email address he has typed in.

Verify email addresses at Limesurvey

We can extend the above example by adding a second text question which also uses a regular expression for validating the second email address. To give some feedback about whether the email addresses the user has entered really match, we will also add two questions of type “text display” with texts like “Sorry, these email addresses don’t match. Please correct.” (warning message) and “Email addresses match, well done!” (success message).

Of course, we only want to show this feedback after the user has entered some texts at both fields. To achieve this we add the following relevance equation to both text display questions, assuming the email questions use question codes “email1″ and “email2″:

((! is_empty(email1) and ! is_empty(email2)))

This equation should now be extended so that the warning only shows up if both text inputs don’t match and the success message is only shown if both strings are equal:

Warning message:
((email1 != email2 and ! is_empty(email1) and ! is_empty(email2)))

Success message:
((email1 == email2 and ! is_empty(email1) and ! is_empty(email2)))

This is not yet a perfect solution because the user can still proceed, even if the warning message is shown. So let’s improve this a little at the next example.

Interrupt a Limesurvey survey if email addresses don’t match

To stop the user from continuing a survey even if the email and email verification address don’t match we have to create our own validation function. Since Limesurvey 1.92 this can be done by using the Expression Manager and the “Whole question validation equation“. We can even create our own error messages.

The difference at the survey design is that we are now using a multiple short text question type to have two text fields within the same question for later question validation.

The validation equation we want to set up should check

  • if text was inputted at field 1 (email address) -> field may not be empty
  • if text was inputted at field 2 (email verification) -> v
  • if the inputted text strings match the regular expression for valid email addresses
  • if both text fields contain the same strings -> email_1 == email_2

Assuming that the question code of our question is “emails” and the sub-question codes are “1″ and “2″, we can use the following string at the “Question validation equation” field which can be found at the advanced question settings:

(!is_empty(emails_1)
AND
regexMatch('/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/',emails_1)
AND
(!is_empty(emails_2)
or
regexMatch('/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/',emails_2))
AND
(emails_1 == emails_2))

To show different error messages according to the current state of the two input fields, we can put the following equations into the “Question validation tip” below:

{if(is_empty(emails_1)
or
regexMatch('/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/',emails_1),
'','Please enter a valid email address.<br />')}
{if(is_empty(emails_2) or
regexMatch('/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/',emails_2),
'','Please verify the email address.<br />')}
{if(!is_empty(emails_1)
and
!is_empty(emails_2)
and
emails_1 == emails_2,
'Email addresses match!',
'Email address and email verification do NOT match!')}

That’s it, feel free to pick the solution which matches your requirements. You can also test these different methods at this Limesurvey email validation test survey.

The survey uses an adaptation of the “Litem” Limesurvey template which you can download at the Limesurvey Template Shop which offers professional Limesurvey templates.