Have an idea?

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

JavaScript validation to control a constant sum question and check boxes

I have a constant sum question which contains 7 elements - Q22.

On the same page I have 2 check boxes - Q22x_1 (Not sure) and Q22x_2 (Not applicable).

The respondent can either answer the constant sum question or select one of the check boxes, but not both the constant sum and a check box.

I have some working JavaScript that I feel is inefficient and was hoping to gain a more updated approach.

Regards.
asked Nov 6, 2016 by Paul Moon Platinum (98,670 points)

1 Answer

0 votes
Giving either question this verification should work:

var q22Answered = false;
for (var i = 1; i <= 7; i++) {
    if (SSI_GetValue('Q22_' + i) != '') {
        q22Answered = true;
        break;
    }
}

var q22xAnswered = SSI_GetValue('Q22x_1') || SSI_GetValue('Q22x_2');

if (q22Answered && q22xAnswered) {
    strErrorMessage = 'Error.';
}


If you would like, you could also use SSI_CustomGraphicalCheckbox to clear out and/or disable the constant sum fields when a checkbox is selected.
answered Nov 6, 2016 by Zachary Platinum Sawtooth Software, Inc. (205,575 points)
Thanks for the verification script Zachary. Works beautifully.

Do you have the script available where ...

1/ If you answer the constant sum, the check boxes are cleared AND
2/ If you answer any of the check boxes, the constant sum is cleared.

Thanks again for your assistance.
I believe this should do it:

<script>
$(document).ready(function(){
    $('#Q22_div .numeric_input').keyup(function(){
        if (SSI_GetValue('Q22x_1')) {
            $('#Q22x_1_graphical').click();
        }
        if (SSI_GetValue('Q22x_2')) {
            $('#Q22x_2_graphical').click();
        }
    })
})

function SSI_CustomGraphicalCheckbox(graphicalObj, inputObj, bln) {
    if (bln && /^Q22x_/.test(inputObj.name)) {
        for (var i = 1; i <= 7; i++) {
            $('#Q22_' + i).val('');
        }
        $('#Q22_total').val(0);
    }
}
</script>
Thanks Zachary. I'll give it a test a little later today. Much appreciated buddy.
...