# Constant Sum Question

In constant sum, how can I make the total to be the below

1- NOT EQUAL To 100
2- LESS THAN 100
3- GREATER THAN 100
You want a constant sum where the question verification verifies (a) the total is less than N, (b) the total is greater than N, or (c) both, correct?
Yes, i have three constant sum questions one i want it to be greater than the second one to be smaller than N and the third one is not eaual to N

N in the 3 questions is 100

So the first question the summetion should be starting from 101 and any other number less than 101 gives an error

the second question the summetion should from 0 to 99 and any other number more than 99 gives an error

the thired one the summetion is any number but not 100

This isn't possible with the constant sum question, so let's do it in a free format instead.  Start by adding one variable to the free format for each item you want to appear in the "constant sum."  Each variable should be numeric with require response disabled.

The HTML should look something like this:

<table class="inner_table">
<tbody>
<tr>
<td valign="top" align="left">
<table>
<tbody>

<tr>
<td class="input_cell">
<input name="[% QuestionName() %]_Variable1" id="[% QuestionName() %]_Variable1" class="numeric_input" type="text" size="3">
</td>
<td class="option_cell">
<div class="options">
<label for="[% QuestionName() %]_Variable1">
VARIABLE 1 LABEL
</label>
</div>
</td>
</tr>

<tr>
<td class="input_cell">
<input name="[% QuestionName() %]_Variable2" id="[% QuestionName() %]_Variable2" class="numeric_input" type="text" size="3">
</td>
<td class="option_cell">
<div class="options">
<label for="[% QuestionName() %]_Variable2">
VARIABLE 2 LABEL
</label>
</div>
</td>
</tr>

<tr>
<td class="input_cell">
<input name="[% QuestionName() %]_Variable3" id="[% QuestionName() %]_Variable3" class="numeric_input" type="text" size="3">
</td>
<td class="option_cell">
<div class="options">
<label for="[% QuestionName() %]_Variable3">
VARIABLE 3 LABEL
</label>
</div>
</td>
</tr>

<tr>
<td class="input_cell">
<input name="[% QuestionName() %]_total" id="[% QuestionName() %]_total" class="numeric_input total" type="text" size="3">
</td>
<td class="option_cell">
<div class="options">
<label for="[% QuestionName() %]_total">
TOTAL LABEL
</label>
</div>
</td>
</tr>

</tbody>
</table>
</td>
</tr>
</tbody>
</table>

<style>
#[% QuestionName() %]_total {
background-color: #c0c0c0;
}
</style>

<script>
\$(document).on('keyup', '#[% QuestionName() %]_div .numeric_input', function(){
var sum = 0;
\$('#[% QuestionName() %]_div .numeric_input:not(.total)').each(function(){
sum += parseInt(\$(this).val()) || 0;
})
\$('#[% QuestionName() %]_total').val(sum);
})
</script>

Note the three blocks in the code for _Variable1, _Variable2, and _Variable3.  You'll need to copy-and-paste those blocks so that you have one for each variable.  Then you can replace "_VariableN" with the name of your free format variable.

Finally, you need custom JavaScript verification.

Verification for requiring the total be greater than 100:

var sum = 0;
\$('#[% QuestionName() %]_div .numeric_input:not(.total)').each(function(){
sum += parseInt(\$(this).val()) || 0;
})

if (sum <= 100) {
strErrorMessage = 'ERROR TEXT';
}

Verification for requiring the total be less than 100:

var sum = 0;
\$('#[% QuestionName() %]_div .numeric_input:not(.total)').each(function(){
sum += parseInt(\$(this).val()) || 0;
})

if (sum >= 100) {
strErrorMessage = 'ERROR TEXT';
}

Verification for requiring the total not be 100:

var sum = 0;
\$('#[% QuestionName() %]_div .numeric_input:not(.total)').each(function(){
sum += parseInt(\$(this).val()) || 0;
})

if (sum != 100) {
strErrorMessage = 'ERROR TEXT';
}

Please tell me if you have any problems getting this to work.
answered Aug 18, 2017 by Platinum (98,000 points)