Validation of Total in Constant Sum question | XM Community
Skip to main content
I have a Constant Sum question asking about hours/week spent on 5 different tasks. If the Total is greater than 100, I need to generate an error message and not allow the survey to proceed until the correction(s) are made.



I've seen several validation questions in the community discussions, but haven't found any anseres.



I can access the total if I begin a new block after the question, but this doesn't do what I need.



Any ideas?
If you are trying to validate that the constant sum is 100, then it's super easy to use the built-in tools:

!



However, if you're trying to create a continuous sum type of question where the total isn't a set constant, this gets a bit trickier. You can't use custom validation because that won't let you compare all of the categories as a sum. I think you'd likely need a custom code solution because I can't think of a way to do it with existing tools. You can check out some javascript resources here. Or if you're lucky, maybe one of our coding gurus will be willing to help out.
Hi Charlotte - This might be too late to help you out but I was struggling with something similar myself. Here's my solution...



This can be achieved by using custom coding on the question i.e. adding JavaScript. If I understand correctly then any responses <= 100 are fine. Anything >100 requires a custom validation message. First choose the option to add JavaScript on the Constant Sum Question. Use the following code as a guide (I have commented the bits that will be specific to your requirements and will need to be adjusted). To get the id's of the questions you need to sum in the code below you will need to preview the question and Inspect the HTML elements for the inputs that you need to sum.



Qualtrics.SurveyEngine.addOnload(function()

{

/*Place your JavaScript here to run when the page loads*/

var warnings = [];



function hideEl(element) {

if($(element)) $(element).hide();

}



function createNextButton() {

var nextButtonHTML = '<input id="CustomNextButton" class="NextButton Button" title="→" type="button" name="NextButton" value="→" aria-label="Next">';

jQuery('#Buttons').append(nextButtonHTML);

}



hideEl.defer('NextButton');

createNextButton();



});



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/



jQuery('#CustomNextButton').on('click', function() {



/* This is where you sum the values entered on the current page - Inspect the HTML to find the correct Element ID */

var xtotal =Number(document.getElementById('QR~QID37~1').value) + Number(document.getElementById('QR~QID37~2').value) + Number(document.getElementById('QR~QID37~3').value) + Number(document.getElementById('QR~QID37~4').value);



/* This is where you set your condition to be accepted */

if (xtotal <= 100) {

jQuery('#NextButton').click();

jQuery('#CustomNextButton').hide();

} else {

/* This is where you set your error message text */

var errorMsg = "Responses must be less than, or equal to 100";

var x = document.createElement("DIV");

var t = document.createTextNode(errorMsg);

x.className = "custom-warning";

x.appendChild(t);

document.getElementById('Questions').parentElement.appendChild(x);

jQuery('.custom-warning').css("background", "pink");

jQuery('.custom-warning').css("color", "red");

jQuery('.custom-warning').css("font-size", "12px");



}

});



});



Qualtrics.SurveyEngine.addOnUnload(function()

{

/*Place your JavaScript here to run when the page is unloaded*/

});
I'll be trying that next week - thanks!
@GrayMatterThinking



Thanks for the helpful comment. I've altered the code to fit my survey (so I think), but it doesn't seem to be working for me. Any advice on what I'm doing wrong? One suspicion is the spaces I included between my values being summed. I did this because I have so many that the line was getting too long to read. I'm not familiar with Java, so not sure if putting spaces/line breaks in there is ok or not. Thanks!



Qualtrics.SurveyEngine.addOnload(function()

{

/*Place your JavaScript here to run when the page loads*/

var warnings = [];



function hideEl(element) {

if($(element)) $(element).hide();

}



function createNextButton() {

var nextButtonHTML = '<input id="CustomNextButton" class="NextButton Button" title="→" type="button" name="NextButton" value="→" aria-label="Next">';

jQuery('#Buttons').append(nextButtonHTML);

}



hideEl.defer('NextButton');

createNextButton();



});



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/



jQuery('#CustomNextButton').on('click', function() {



/* This is where you sum the values entered on the current page - Inspect the HTML to find the correct Element ID */

var xtotal =Number(document.getElementById('QR~QID9~2').value) + Number(document.getElementById('QR~QID9~3').value) + Number(document.getElementById('QR~QID9~4').value) + Number(document.getElementById('QR~QID9~5').value) +

Number(document.getElementById('QR~QID9~6').value) + Number(document.getElementById('QR~QID9~7').value) + Number(document.getElementById('QR~QID9~8').value) + Number(document.getElementById('QR~QID9~9').value) + Number(document.getElementById('QR~QID9~10').value)

+ Number(document.getElementById('QR~QID9~11').value) + Number(document.getElementById('QR~QID9~12').value) + Number(document.getElementById('QR~QID9~13').value) + Number(document.getElementById('QR~QID9~14').value) + Number(document.getElementById('QR~QID9~15').value)

+ Number(document.getElementById('QR~QID9~16').value) + Number(document.getElementById('QR~QID9~17').value) + Number(document.getElementById('QR~QID9~18').value) + Number(document.getElementById('QR~QID9~19').value) + Number(document.getElementById('QR~QID9~20').value)

+ Number(document.getElementById('QR~QID9~21').value) + Number(document.getElementById('QR~QID9~22').value) + Number(document.getElementById('QR~QID9~23').value) + Number(document.getElementById('QR~QID9~24').value) + Number(document.getElementById('QR~QID9~25').value)

+ Number(document.getElementById('QR~QID9~26').value) + Number(document.getElementById('QR~QID9~27').value) + Number(document.getElementById('QR~QID9~28').value) + Number(document.getElementById('QR~QID9~29').value) + Number(document.getElementById('QR~QID9~30').value)

+ Number(document.getElementById('QR~QID9~31').value) + Number(document.getElementById('QR~QID9~32').value) + Number(document.getElementById('QR~QID9~33').value) + Number(document.getElementById('QR~QID9~34').value) + Number(document.getElementById('QR~QID9~35').value)

+ Number(document.getElementById('QR~QID9~36').value) + Number(document.getElementById('QR~QID9~37').value) + Number(document.getElementById('QR~QID9~38').value) + Number(document.getElementById('QR~QID9~39').value)

+ Number(document.getElementById('QR~QID9~40').value);



/* This is where you set your condition to be accepted */

if (xtotal <= 160) {

jQuery('#NextButton').click();

jQuery('#CustomNextButton').hide();

} else {

/* This is where you set your error message text */

var errorMsg = "Responses must be less than, or equal to 160";

var x = document.createElement("DIV");

var t = document.createTextNode(errorMsg);

x.className = "custom-warning";

x.appendChild(t);

document.getElementById('Questions').parentElement.appendChild(x);

jQuery('.custom-warning').css("background", "pink");

jQuery('.custom-warning').css("color", "red");

jQuery('.custom-warning').css("font-size", "12px");



}

});



});



Qualtrics.SurveyEngine.addOnUnload(function()

{

/*Place your JavaScript here to run when the page is unloaded*/

});

GrayMatterThinking This works well, and thanks for sharing. Wondering if I could pick your brain about how to set up a loop so that the user only gets the error message two or three times and then moves forward despite the error?


Leave a Reply