Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Multiple Short Text with some (not all) fields mandatory.

  • nique
  • nique's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
6 years 4 months ago #161559 by nique
Hi there,

I found this old thread with some JS on making only a few questions mandatory in a multiple short text question.

The select questions as mandatory is working but I am finding that the script is interfering with another question and quotas I have set up.

Q1 asks the level of interest a participant has in our research - Radio List - Yes / No / Someone Else.
Yes / Someone Else triggers Q2 which has the multiple short text question asking for further details.
If someone says No, then they are directed to the end of the survey via a Quota.

The JS for Q2 is somehow interfering with this Quota or Q1 as when I select No - and click next, the same pop up box regarding an answer needed appears. Even though Q2 is never presented to this person.

Could someone please let me know where my code is going wrong?

I tried changing #movenextbtn to #movesubmitbtn but then the code doesn't work at all.
I figure I need some identifying JS regarding 'if this question is presented' but am at a loss.

All help is greatly appreciated.

Here is also my exact code in the survey.
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
  $('#movenextbtn').click(function(){
    var needAnswer=false;
    if ($("#answer448284X2875X55236fn").val()==""){
      needAnswer=true;
      $("#answer448284X2875X55236fn").css('background','red');
      }
    if ($("#answer448284X2875X55236sn").val()==""){
      needAnswer=true;
      $("#answer448284X2875X55236sn").css('background','red');
      }
    if ($("#answer448284X2875X55236org").val()==""){
      needAnswer=true;
      $("#answer448284X2875X55236org").css('background','red');
      }
    if ($("#answer448284X2875X55236ph").val()==""){
      needAnswer=true;
      $("#answer448284X2875X55236ph").css('background','red');
      }
    if ($("#answer448284X2875X55236em").val()==""){
      needAnswer=true;
      $("#answer448284X2875X55236em").css('background','red');
      }
    if (needAnswer){
    alert ('Answer needed');
      return false;
    }else{
      return true;
    }
  });
});
  </script>
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago - 6 years 4 months ago #161570 by Joffm
Hello, nique,
your mentioned thread is 5 years old. So obviously the JS is only working in an outdated version of LS.
We do not know which LS version you are using.

But the easiest way to do what you write in the subject , is:
Question is set to not mandatory.
By subquestion relevance force some fields to be filled
Code:
!is_empty(Q1_SQ002) AND !is_empty(Q1_SQ003)



Regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 6 years 4 months ago by Joffm.
The topic has been locked.
  • nique
  • nique's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
6 years 4 months ago #161571 by nique
I am using LS - Version 2.59.1+170116

I forgot that this newer version allows for relevance equations to be added to subquestions.

If I have 10 multiple short answers, and I only want 4 to be mandatory, do I have to insert this code into each of the ones I want to be mandatory? Just unsure where to place this.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161572 by Joffm
Sorry,
I wrote "subquestion relevance". Of course it is "subquestion validation".

Advanced settings:


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: nique
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161574 by holch
Watch out with Joffms code. It will pass if ONE of the subquestions is answered. From what I understood, all 4 must be answered, so I guess you need to use "OR" instead of "AND". But I might also have understood the task wrong.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161578 by Joffm
Holch, please, don't scare me.

The validation is (both subquestions have to be answered):
"The second subquestion is not empty AND The third subquestion is not empty"

If you say "OR", it passes if the second OR the third are not empty.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161581 by holch
Hmmm, I might be thinking wrong. But if SQ2 and SQ3 are mandatory, they should not be empty, correct? So far so good.

Wait a second. I think I mixed my thinking up with conditions in EM. So basically the message shows if what is in the question validation is not correct. If this is the case, your code is correct. Sorry for the confusion.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #161589 by tpartner
I'm wondering why place it in the Sub-question validation and not in the Question validation.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • nique
  • nique's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
6 years 4 months ago #161611 by nique
Thanks Joffm. This works much better than the old JS I was using.
And you are correct that it is an AND function - all 4 are mandatory and required before proceeding.

Following tpartner's suggestion, I added this into the whole question validation. Appears to be a bit cleaner that way.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose