Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Java validation to ensure question answers are not the same

I have 3 multiple response questions (say E1, E2 and E3). Code frames are the same across all 3. There is 20 codes in the code frame used. People can select a max of 5 per multiple response question.
I need to create a validation at E2 and E3.
-    check at E2 that their responses are not the same as the ones selected at E1.
-    Check at E3 that their responses are not the same as either E1 or E2.
Each combo selection at E1, E2 and E3 must be different. Cannot be exactly the same combo.

cheers,
Paul
asked Aug 23, 2021 by PaulK (420 points)
Are these questions on the same page or different pages?
On different pages, thanks Zachary

1 Answer

0 votes
 
Best answer
Try giving E2 this custom verification:

var previousResponses = [
    [[% E1_1 %],[% E1_2 %],[% E1_3 %],[% E1_4 %],[% E1_5 %]]
];

var responses = [];
$('input[name="hid_list_[% QuestionName() %]"]').val().split(',').forEach(function(item){
    responses.push(SSI_GetValue('[% QuestionName() %]_' + item));
});

previousResponses.forEach(function(previousResponse){
    var same = true;
    for (var i = 0; i < responses.length && same; i++) {
        same = responses[i] == previousResponse[i];
    }
    if (same) {
        strErrorMessage = 'error...';
    }
});


And E3 this custom verification:

var previousResponses = [
    [[% E1_1 %],[% E1_2 %],[% E1_3 %],[% E1_4 %],[% E1_5 %]],
    [[% E2_1 %],[% E2_2 %],[% E2_3 %],[% E2_4 %],[% E2_5 %]]
];

var responses = [];
$('input[name="hid_list_[% QuestionName() %]"]').val().split(',').forEach(function(item){
    responses.push(SSI_GetValue('[% QuestionName() %]_' + item));
});

previousResponses.forEach(function(previousResponse){
    var same = true;
    for (var i = 0; i < responses.length && same; i++) {
        same = responses[i] == previousResponse[i];
    }
    if (same) {
        strErrorMessage = 'error...';
    }
});
answered Aug 24, 2021 by Zachary Platinum Sawtooth Software, Inc. (205,575 points)
selected Aug 25, 2021 by PaulK
I tried this code but it didn't work. Have emailed you my file. Thanks again.
You will need to extend the list of Sawtooth Script calls to represent all your list items.
...