Have an idea?

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

Simple Check on Constant Sum

Hello there,
I have a constant sum based on a constructed list, and I'd like to just warn respondents but allow them to continue.

I ask what types of transportation they use in a commute, checkbox with 23 items.  I then ask how long the commute is overall.  From there, if they chose multiple travel modes, they will see a constant sum question asking them to break down each mode in minutes (constructed list).  They are reminded of the total travel time, but the client wants there to be no restrictions set on their answers, just warn them if it is +/- 5 minutes from their initial commute time.  They can go back and change the answers if they'd like or leave their values as is in the constant sum.

Hope this make sense.  Thanks in advance!
asked Jan 21, 2021 by gmyeaton (280 points)
This could be done with custom code, but the exact code would depend on how / when you want to warn users.  Should a message appear on the screen in real time whenever their total is off?  Should we show them a one-time-only error when they try to submit the page with an off total?  Something else?
I think after they submit a one-time-only error would work, if possible.  The only catch is I currently have the script in there to make sure there are no zeros entered into the constant sum question.

1 Answer

0 votes
I would add this to the constant sum's footer:

<input type="hidden" id="warningShown" value=""/>

Then you can use this custom JavaScript verification:

// Params
var expectedTotal = [% NumericQ %];
var overUnder = 5;

// Run
if (!$('#warningShown').val()) {
    var total = 0;
    $('input[name="hid_list_[% QuestionName() %]"]').val().split(',').forEach(function(item){
        total += SSI_GetValue('[% QuestionName() %]_' + item);

    if (total < expectedTotal - overUnder || total > expectedTotal + overUnder) {
        strErrorMessage = 'Error message...';

Line 2 can be updated with the variable with their commute time.  Line 3 can be updated with your over-under value.

To handle your doubled-up custom verification, I would place my verification after yours and change my conditional to this:

if (!$('#warningShown').val() && !strErrorMessage) {
answered Jan 21, 2021 by Zachary Platinum Sawtooth Software, Inc. (214,575 points)
Perfect.  Thanks again Zachary, as always appreciate your help!