Welcome, Guest
Username: Password: Remember me

TOPIC: Making a question mandatory dependant on previous answers

Making a question mandatory dependant on previous answers 2 years 10 months ago #67289

  • wabrit
  • wabrit's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 17
  • Karma: 0
I have a survey with a page that asks a multiple choice question e.g. "What is your preferred method of communication?" with answers such as email, letter, text, phone etc.

On the following pages I have questions that gather more details about communications; e.g. there's a question that asks for address details, another for phone numbers, etc.

What I need to do is force the user to enter the details of the communication method they have chosen. e.g. if they choose email as their preferred communciation, then when they get to the details page on email then the question "what is your email address" becomes mandatory.

I'm aware that one way of tackling this problem is to have two copies of each detail question, one mandatory and one not and put a condition on them that displays the appropriate question depending on the previous choice.

However, for reasons which I won't go into here that option is not available; I need to have just one email question.

So I'm assuming I will need to write some script which dynamically emulates the behaviour of limesurvey when a statically-defined mandatory question is not answered, but I'm a bit stuck as to how to start this.

Can anyone give me pointers as to how to go about this, or some sample script?

Many thanks.
The administrator has disabled public write access.

Re: Making a question mandatory dependant on previous answers 2 years 10 months ago #67294

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5324
  • Thank you received: 294
  • Karma: 249
I don't have a ready to go script for you, some examples can be found at manual -> workarounds. Let me remind you that all questions need to be placed on the same page if you need a customized mandatory function. Other solutions are possible but require cookies and more complex Javascript coding.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.
The following user(s) said Thank You: wabrit

Re: Making a question mandatory dependant on previous answers 2 years 10 months ago #67315

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4139
  • Thank you received: 767
  • Karma: 348
Here is an example of how to store and retrieve a cookie - www.limesurvey.org/en/forum/advsearch?q=...name=1&childforums=1
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: wabrit

Re: Making a question mandatory dependant on previous answers 2 years 10 months ago #67317

  • wabrit
  • wabrit's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 17
  • Karma: 0
Thanks - found some useful examples on the workarounds page, particularly the "Making one (or more) rows of an array mandatory" example; once I had figured out whether the user was navigating forwards or backwards (using document.limesurvey.move.value) then it wasn't too difficult.:)
The administrator has disabled public write access.

Re: Making a question mandatory dependant on previous answers 2 years 10 months ago #67320

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5324
  • Thank you received: 294
  • Karma: 249
Please post your solution afterwards and add it to the wiki workarounds page so other benefit from it as well.

Thanks!

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: Making a question mandatory dependant on previous answers 2 years 10 months ago #67511

  • wabrit
  • wabrit's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 17
  • Karma: 0
Here's an outline of the solution. Think of the survey as having two pages; the first page asks (as e.g. a dropdown) what contact method the user prefers (email or phone), and the second page asks for email address or phone number. On the second page the email address will be mandatory if the user has indicated they prefer email on page 1; otherwise if they indicated they prefer phone then the phone number will be mandatory.

In what follows:
  • <sid>X<gid>X<qid> is the SGQAID of the question on page 1 of the survey that asks the user whether they would prefer to be contacted by email or telephone.
  • <contactDetailsGid> is the question group id of the current page (2) of the survey that will ask for more details of email and phone number.
  • <emailQid> is the question id for the question that asks for an email address.
  • <telephoneQid> is the question id for the question that asks for an phone number.

The script attached to either question on page 2 is as follows:
<script>
// Called when question page loaded; will indicate 
// email or phone question as mandatory depending on
// answer on previous page
$(document).ready(function() {
   if ('{INSERTANS:<sid>X<gid>X<qid>}' == "By email") {
      setQuestionMandatory(<emailQid>);
   } else if ('{INSERTANS:<sid>X<gid>X<qid>}' == "By telephone") {
      setQuestionMandatory(<telephoneQid>);
});
 
// Called when user hits the Previous/Next navigation buttons to
// move off this page
$('form#limesurvey').submit(function () {
 // If we are moving back do no checking
 if (document.limesurvey.move.value == 'moveprev') {
   return true;
 }
 
  // Override the built-in "disable navigation buttons" feature
    $('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
  var empty = 0;
  var alertStr = "";
 
  if ('{INSERTANS:<sid>X<gid>X<qid>}' == "By email") {     
     empty = checkAnswer($('input#answer<sid>X<contactDetailsGid>X<emailQid>'));
 
     if (empty != 0) {
        alertStr = "You indicated email as your preferred contact method; please enter your email address or go back and change your preference.";
     }
  } else if ('{INSERTANS:<sid>X<gid>X<qid>}' == "By telephone") {
       empty = checkAnswer($('input#answer<sid>X<contactDetailsGid>X<telephoneQid>));
 
       if (empty != 0) {
        alertStr = "You indicated home telephone as your preferred contact method; please enter your home telephone number or go back and change your preference.";
       }
   }
 
    if (empty == 1) {
      alert(alertStr);
      return false;
    } else {
      return true;
    }
});
 
 
   // Checks if answer has been supplied; if not decorates it
   function checkAnswer(answer) {
     if (answer.val() == '') {
          answer.css('background-color', 'pink');
          return 1;
     } else {
       return 0;
     }
   }
 
   // Makes a question appear mandatory by inserting a "*" in the
   // appropriate span (normally defined in the survey template).
   function setQuestionMandatory(qid) {      
      $('#question'+qid+' span.asterisk').each(function(i) {
         this.innerText = "*";
      });
   }
</script>
The administrator has disabled public write access.

Re: Making a question mandatory dependant on previous answers 2 years 10 months ago #67528

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5324
  • Thank you received: 294
  • Karma: 249
Can you please add your solution to the workaround section of our manual so other users can beneffit from your solution, too!?

Thanks!

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.192 seconds
Donation Image