How to re-order the “other” option at a Limesurvey survey

[Tested with Limesurvey v2.06LTS]
Several Limesurvey question types allow adding an option to enter a free text answer. This option can be enabled when editing a question (“Option ‘other’ -> Yes”).
By default Limesurvey always places that option at the very end of the list of answers and there is no feature to change the order of answer options.

To place the “other” option at the very beginning of the list of answers or at any other position one needs to use some custom JavaScript. We have developed such a snippet for you which is easy to adjust and allows you to easily add the code to your survey and adjust it as necessary.

Assuming your answer options are using answer codes “1”, “2”, … you can use the following code snippet to place the “other” option before the 6th answer option (“none of these”):

<script type='text/javascript'>
$(document).ready(function() {
/* Adjust these settings */
 var move_element="other";
 var place_before="6";
/* Please do NOT change the lines below */
var place_before="javatbd{SGQ}"+place_before;
var move_element="javatbd{SGQ}"+move_element;
$("#"+move_element).insertBefore("#"+place_before);
});
</script>

The best approach is to insert the code in source code mode of the integrated editor:
Source code mode of integrated Limesurvey editor

Using the survey preview you should now see something like this:

You can simply place the “other” option on top of the list by adjusting the code above as follows:

/* Adjust these settings */
 var move_element="other";
 var place_before="1";

You can find the used JavaScript (you need to switch to source code mode to see it) at the help texts of this demo survey: Limesurvey-re-order_other_option_demo_survey (use right-click -> “save link” to save LSS file!).

If you want to place a certain item of a randomized list of answer options to the end of the list, have a look at How to place a certain option at the end of a list with randomized answers at Limesurvey survey.

Adding “I agree” button at the beginning of your Limesurvey survey

Sometimes you need participants to agree to certain terms and conditions before starting to take a survey. There is no build-in feature for this at Limesurvey, but we can easily work around this with some lines of JavaScript. Here we go…

Turn of XSS filter at Limesurvey

To be able to add custom JavaScript at a certain survey question or text, we first need to disable the internal Limesurvey XSS filter. So when being logged in at Limesurvey go to Global Settings -> Security -> “Filter HTML for XSS” and set it no “No”.

Limesurvey XSS Filter

Adding your terms and conditions

Once you have turned off the filter you can add your terms and conditions to your survey’s welcome text. Click the edit icon and select “Edit text elements”. Add your text at the “Welcome message” text box.

Adding the “I agree” / “I do NOT agree” buttons

When a user calls your survey, he will at first be presented the welcome text holding the terms and conditions. There will also be a “next” button to start the survey and on option to “Exit and clear survey”. So instead of re-inventing the wheel, we simply rename these buttons for the welcome page only.

For this you need to go to show the welcome message in source code mode. You’ll see the terms and conditions you entered previously. Go to the end of that text and add the following code there to rename the buttons:

<script>
 $(document).ready(function()
 {
    $('#movenextbtn').attr('value','I agree!');
    $('.clearall').attr('value','I do NOT agree!');
 });
</script>

Of course, you can change the “I agree!” / “I do NOT agree!” texts if you wish.

The result:
Custom Limesurvey button labels

 

How to validate email addresses at Limesurvey

Update (12/2018): Since these days domain names can not only hold special characters like “ü” but also use more than 3 characters as domain name like .tech or .company, we have updated our email regex to only check if “@” and “.” exists within the entered email address.


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 use a simple text question and the following regular expression which has to be added at the “validation” field when editing the question at Limesurvey:

/^.+?@.+?\..+$/

Note that when using regular expression at Limesurvey, these always have to start and end 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.NAOK) and ! is_empty(email2.NAOK)))

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.NAOK != email2.NAOK and ! is_empty(email1.NAOK) and ! is_empty(email2.NAOK)))

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

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) -> field may not be empty
  • if the inputted text strings match the regular expression for valid email addresses
  • if both text fields contain the same strings -> emails_1 == emails_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.NAOK)
AND
regexMatch('/^.+?@.+?\..+$/',emails_1.NAOK)
AND
(!is_empty(emails_2.NAOK)
or
regexMatch('/^.+?@.+?\..+$/',emails_2.NAOK))
AND
(emails_1.NAOK == emails_2.NAOK))

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.NAOK)
or
regexMatch('/^.+?@.+?\..+$/',emails_1.NAOK),
'','Please enter a valid email address.<br />')}
{if(is_empty(emails_2.NAOK) or
regexMatch('/^.+?@.+?\..+$/',emails_2.NAOK),
'','Please verify the email address.<br />')}
{if(!is_empty(emails_1.NAOK)
and
!is_empty(emails_2.NAOK)
and
emails_1.NAOK == emails_2.NAOK,
'Email addresses match!',
'Email address and email verification do NOT match!')}

That’s it, feel free to pick the solution which matches your requirements.

Limesurvey – Conditions based on token attributes

Sometimes you want to show certain questions of a survey to a specific user group only. With Limesurvey this is no problem. If your survey is set to be non-anonymous and uses tokens, you can use user defined attributes for conditions. Here we describe the necessary steps.

1. Adding additional attributes

Set the survey to be non-anonymous:
Limesurvey survey settings

Enable tokens:
Enable tokens at Limesurvey

Add a certain number of user defined attribute fields:
Add token attributes at Limesurvey

Assign attribute names after having created the new fields:
Label additional attributes

Add some dummy tokens with user defined values

Add dummy tokens

Set conditions for a certain question

Go to the question and call the condition designer:
Limesurvey condition editor

Select the token field on which the condition should be based on (e. g. “Department”) and define the constant which the condition should match (e. g. “Management”):
Condition based on token attribute

Check condition

You can use the group preview for a first check if the condition was stored successfully,
Limesurvey group preview

because in this case the question will not be shown at the group preview (because the conditions isn’t met). The arrow indicates where the question was previously shown before setting the condition:

Test survey at runtime

Once tokens were assigned when adding users or by using the “Generate tokens” feature, the “Display tokens” screen contains an icon to test each token entry:
Limesurvey token overview

Of the above users, only “Jane” belongs to the department “Management”. Let’s check the different survey behavior to check if the condition works as expected:

Survbey at runtime Survey at runtime

More information

 

How to randomly show 1 of X questions at a Limesurvey survey

Sometimes you want to show different versions of a question at a Limesurvey survey. To do so, you need to randomly show one of the X question versions to the user so the first step would be to generate a random number between 1-X. For this we can use the powerful Limesurvey Expression Manager which is available since release 1.92.

The new question type “equation” can be used to store the result of the random function. So create an “equation” question with question code “randnumber” and the following content at the question text field, which will generate a random number between 1 and 2:
{if(is_empty(randnumber.NAOK),rand(1,2),randnumber.NAOK)}

The whole code basically means that the system should first check if the rand variable was already populated. If it is still empty, a random number between 1 and 2 is created. Else, the already created number is kept and no new random number gets created.

If you want to hide the question from the user, set “Always hide this question” to “yes” at the advanced question settings.
Now we have a question that will be filled with either “1” or “2”, we can use conditions at following questions to show version A or version B of a question. For this you can use the Limesurvey condition manager and set a condition based on a constant value of 1 or 2 for the equation question. Alternatively you can use the “relevance” field to define when a question should be shown. The appropriate relevance reads:
((randnumber.NAOK==”1″))

…and for the second question:
((randnumber.NAOK==”2″))

Don’t forget to check if you set up everything correctly by having a look at the survey logic file.

…and if this still sounds too tricky, we can set up the survey for you.

How to prevent Limesurvey email invitations or reminders being treated as spam

Spam really is a problem these days and without spam filters, email would hardly be useable. But this also causes problems when sending email invitations or reminders at Limesurvey. Often users complain not having received an invitation/reminder email because the user’s spam filter treated the email as spam.

Spam filters

If an email is treated as spam mostly depends on the spam filter being used and its settings. A spam filter usually checks the sender address and the email subject and content, so there are ways and means to influence how they treat your Limesurvey invitation and reminder emails.

Tips & Tricks

(The following list will be updated every now and then)

Sender address

  1. Use a valid sender address (can be set at the Limesurvey survey settings -> General -> Admin email).
  2. Do not use a sender address from a one time email provider. These are often used for spam and therefore blacklisted by most spam filters. Email from email providers like Gmail, Hotmail, GMX and the like might also be considered as spam quite often because everybody can get an account there.
  3. If possible use a sender address which has been in use for years and was never related to any spamming, e. g. your company’s email address.

Sending emails in batches

If you have to send several hundred or even several thousand emails this can really be painful. When sending them all at once using a huge email batch size (see details below at “Mailserver”) the script will most probably time out. Furthermore, there is a high risk of being treated as spammer so your IP address or domain could be put on a blacklist. And if you send them manually you will need a lot of coffee to not fall asleep when clicking the send button 50, 70 or 100 times.

The best approach for sending a huge amount of emails is using cronjob scripts. For this we have coded a script to automtically send email invitations. There is also another script for automatically sending email reminders.
You just upload the script to your server and set which survey(s) the script should deal with. Afterwards, you can set up a cronjob to call the script every X minutes. So if you need to send 3000 emails, just set the batch size to 50, call the script every 5 minutes and within 5 hours (50*12*5=3000) you are done without any further user interaction required. Of course these scripts only send an invitation once so users do not get spammed.

Sending emails this way significantly reduces the risk of your emails being treated as spam or being blocked. If your provider sets a certain limit of max X emails every Y minutes you can easily work around that by adjusting the cronjob details accordingly to not hit such limits.

Email content

  1. The content of your email is probably the most important part. Do never use any words like “free”, “buy”, “sex”, “shop”, “viagra” or the like at your email invitation or email reminder texts. You can change the default texts at the “email templates” link of the survey options menu.
  2. Same for the email subject: Use a meaningful text that maybe doesn’t use the word “invitation” because it can be found at spam emails quite often.
  3. If you send the exact email text several times, this might also look suspicous to spam filters. The solution is to use the {FIRSTNAME}/{LASTNAME} email placeholders at the Limesurvey email templates to create some unique strings.
  4. Often it’s worth a try to switch from HTML emails (Limesurvey default setting) to raw text emails. At Limesurvey this can be changed at Survey settings -> Tokens -> “Use HTML format for token emails” = “no”.
  5. Spammers often use images for fake emails, so better not use any images at your emails.
  6. Of course you want to add a link to the survey at your invitation and reminder emails. But generally links should be reduced to a minimum and opt-out links are often used for fishing, so if you are facing serius spam problems, not adding any links at all might help.
  7. When using HTML emails and editing the email messages, check that the HTML being used has a valid HTML syntax, e. g. all tags should be closed properly. There are several HTML validators available, e. g. the official W3C validator where you can copy paste the HTML code. You can view the HTML source code text by switching the editor to source code mode:
    Source code mode of integrated Limesurvey editor

Mailserver

  1. Emails can either be sent by the webserver using PHP emailing which is the Limesurvey default setting or by connecting to an SMTP server. Since PHP emailing is mostly used for spam, we recommend to switch to SMTP if you are facing spam problems. At Limesurvey this can be configured at Global settings -> Email settings -> “Email method” = “SMTP”
  2. Especially when using “PHP” email setting, some webservers will block emails if a certain amount of emails is send in a minute/an hour/a day. So make sure that when manually sending the emails the batch size is not too big. This can either be set when sending email at the token screen or by editing Limesurvey’s default setting (50) at Global settings -> Email settings -> “Email batch size” = “XYZ”.
  3. If possible use a webserver which hasn’t been connected to spamming anyway. Spam filters also analyse the IP range a server belongs to and if there have been spam issues with this range before. So using your company’s server or the SMTP server of known providers such as Gmail should help.
  4. A wrong mailserver configuration also influences spam filters. There are several different settings that could be wrong. We are currently collecting some of them and will update this section later.
    The experts out there can have a look at the email source code to check some of the sender server configuration settings like email header, spam score (if available), …

Additional information and tools:

Cronjob scripts for automatic email sending

Testing

Limesurvey forums / Limesurvey manual

How to use JavaScript at a Limesurvey template or survey

Limesurvey offers a lot of on board features but for certain issues you can add JavaScript to your survey to extend its abilities. Particularly when it comes to on-screen data validation, data filtering, setting default values or any kind of randomization, using JavaScript is the way to go and we have collected several workarounds in the Limesurvey wiki to deal with different problems.
Since some users have problems embedding JavaScript in their surveys, we want to give some examples on the different approaches.

To avoid re-inventing the wheel, Limesurvey makes use of the JavaScript library jQuery. This library is documented very well and we recommend having a look at some tutorials and the official documentation.

If you want to embed any of the jQuery based workarounds, then there should be a

$(document).ready(function())

function in the template.js file of your template where you can add all of this code so that it is only executed after the page is fully loaded. So option 1 to add JavaScript to your survey is to simply place it in the template.js file of your template.

Pros:

  • You don’t need to disable the internal Limesurvey XSS filter if you add the code directly at the template.js file.
  • You can add your code ot the existing $(document).ready(function()).
  • It is very easy to update/modify.

 

Cons:

  • The file is loaded at every page call, so it might cause some overhead if you only need the script for a certain survey question(s).
  • The code will be loaded for all surveys that use the template, so don’t add survey-specific JavaScript to any template which may be used for other surveys, instead create a copy of the template.

 

The second option is to add the JavaScript/jQuery code directly to the affected question or group. This can easily be done by embedding it in the question/group text for which you have to switch to source code view of the integrated editor:
Source code mode of integrated Limesurvey editorFurthermore, you have to disable the Limesurvey XSS filter at
Global settings -> Security -> “Filter HTML for XSS” = “No”.

Once you have done this, you can use the following code snippets. Copy them to the affected question/group text in source code mode and test if the alert message appears:

1. Embedding Javascript

<script type="text/javascript" charset="utf-8">
    alert("Test!");
</script>

2. Adding jQuery code

<script type="text/javascript" charset="utf-8">

	$(document).ready(function() {

		alert("Test!");
	});
</script>

Pros:

  • The code will only be loaded once (for the affected group/question).
  • You can easily have several similar code snippets and just change some IDs in the code without having any overhead.
  • Limesurvey placeholders and Expression Manager tags are parsed.

Cons:

  • You have to disable the internal XSS filter for which requires admin access to the Limesurvey installation.
  • The code will be split over several questions and gets harder to maintain if you add many different script snippets.

 

Pre-filling a text field at Limesurvey 1.92 using default answers and Expression Manager

At Limesurvey 1.92 we implemented default values for several question types that were missing this feature. In combination with the new and powerful Expression Manager, this offers new possibilities. Using the Expression Manager at Limesurvey 1.92 you can now easily populate a text field with data from previous answers.

Let’s create a simple example for demonstrating how it works:

1. Create a text question at Limesurvey and note the question code:
Limesurvey_text_question-question_code
2. Create a second question of the same type and e.g. ask for the email or last name. Again, note the question code

3. At a third text question, where you want to pre-populate the text field with the previously entered texts, click the “Edit default answers for this question” icon:
Limesurvey edit default answers icon

 

 

4. At the field to enter a default value, add the question codes in {…} brackets:
Limesurvey_default_answers

 

 

5. If you then fill in text for the first two questions, the answers should be inserted at the question for which you defined the default value.

 

Things to watch out for:

  • The question for which you set the default answer has to be shown on a following page.
  • When setting a default value use {…} brackets when referring to a question code of a previous question.

More Information:

 

How to add your own favicon at a Limesurvey template

A favicon is the little icon you see in the browser’s address bar, list of bookmarks or tab. You can display your own icon as follows:

  1. Create a favicon – google will find you lots of free favicon generators
  2. Name your new favicon “favicon.ico” and place it in your template directory. This can easily be done by using the template editor’s upload feature.
  3. Add the following code to your startpage.pstpl before the closing </head> tag:
<link rel="shortcut icon" href="{TEMPLATEURL}favicon.ico" type="image/x-icon">
<link rel="icon" href="{TEMPLATEURL}favicon.ico" type="image/x-icon">

How to restore data from a deactivated Limesurvey survey

You might run into problems if you deactivate your survey at Limesurvey instead of setting an expiry date (which we recommend to restrict access to a survey), because once a survey is deactivated, a backup of the results table “lime_survey_12345” (12345 refers to the survey ID) is created and it is renamed to “lime_old_survey_12345_20120101235959”. The last part of the table name marks the date (2012-01-01) and time (23:59:59) the survey was deactivated.

If you now want to view results, generate statistics or export the response data, you have a problem: Limesurvey doesn’t show any available data because the response table was backed up and renamed.

To get access to the results, proceed as follows:

  1. Activate the according survey again
  2. Go to the “Responses & statistics” screen
  3. Click at the “Import responses from a deactivated survey table” button:
  4. Choose your source table.
  5. Click the “Import responses” button

This is how it looks like at the Limesurvey 3.x interface:

Re-import responses from deactivated survey
Re-import responses from a deactivated survey at Limesurvey 3.x

 

And at the older Limesurvey v2.0x versions the option looks like this:

Import answers from a deactivated survey table
Import answers from a deactivated survey table at Limesurvey v2.0x