Constant Sum Custom Validation - Must be greater than 0 | XM Community
Skip to main content

Hi,
I am trying to use a script for custom validation of a constant sum question that throws an error message if the total is zero.
The code below represents my attempt, based on other forum responses. However, I can't seem to get it to work correctly. I have gotten it to give an error, but it still gives an error once the values are updated on the screen. I'm not sure how to approach this.
Here is my code:
Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/
    var warnings = s];


    function hideEl(element) {
        if($(element)) $(element).hide();
    }   


    function createNextButton() {
        var nextButtonHTML = '';
        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('QID15_Total').value);


    /* This is where you set your condition to be accepted */  
    if (xtotal > 0) {
      jQuery('#NextButton').click();
      jQuery('#CustomNextButton').hide();
    } else {
      /* This is where you set your error message text */
      var errorMsg = "Responses must be greater than 0!!!";
      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*/
});

Hey did you ever find a solution to this?
I have the same issue now.


Leave a Reply