Prevent Overspending | XM Community
Skip to main content
Question

Prevent Overspending


Forum|alt.badge.img+3
  • Level 2 ●●
  • 17 replies

Hello, I want to design a study on qualtrics. Participants earn some income in the previous stages. Then they have to spend by choosing from the spending options provided. However, what I want to do is to make sure that they cannot spend more than the net income they have. I tried validation to do this, but it didn't work. I think I need to use JS code for this. Can anyone help with this?

Thanks in advance.

Best.

11 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • February 3, 2025

Didn’t this work? 

 


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 3, 2025
ahmedA wrote:

Didn’t this work? 

 

It's not the same topic. That question was about deducting the amount spent from the net income. This question is about the amount spent not being more than the net income.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5923 replies
  • February 3, 2025

@O.kisa,

Is your spending question and Constant Sum question? If so, as long as there is no display logic on the Constant Sum choices, you don’t need JS. Use custom validation where the first choice is less than the spending limit minus all the other choices. For example:

$e{ e://Field/SpendLimit -  q://QIDx/ChoiceNumericEntryValue/2 - q://QIDx/ChoiceNumericEntryValue/3 } 

Where QIDx is the QID of your constant sum question.

 


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 3, 2025
TomG wrote:

@O.kisa,

Is your spending question and Constant Sum question? If so, as long as there is no display logic on the Constant Sum choices, you don’t need JS. Use custom validation where the first choice is less than the spending limit minus all the other choices. For example:

$e{ e://Field/SpendLimit -  q://QIDx/ChoiceNumericEntryValue/2 - q://QIDx/ChoiceNumericEntryValue/3 } 

Where QIDx is the QID of your constant sum question.

 

No, It did not work. But thank you.


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 3, 2025
ahmedA wrote:

Didn’t this work? 

 

I do use the code below. If it is not problem for you, could you please check it?

Thank you in advance. 

Qualtrics.SurveyEngine.addOnReady(function () {
    const quest = this;
    const qc = quest.getQuestionContainer();

    // Get the Net_Income value from Embedded Data
    const maxAmount = parseFloat(Qualtrics.SurveyEngine.getEmbeddedData("Net_Income"));
    const alertText = "Please ensure the amount consumed is less than";

    const nextButton = document.querySelector("#NextButton");

    // Add a row below the total row
    const totalRow = qc.querySelector("tr.CSTotal");
    const remainingRow = totalRow.cloneNode(true);
    totalRow.insertAdjacentElement("afterend", remainingRow);

    // Give the total rows new labels for clarity
    totalRow.querySelector("th").innerHTML = "Amount Consumed";
    remainingRow.querySelector("th").innerHTML = 'Amount Left <small id="amountAlert" style="color: red;display:none;">+++++ $ _____.</small>';

    const totalInput = totalRow.querySelector("input");
    const remainingInput = remainingRow.querySelector("input");

    // Monitor changes to the total
    totalInput.oninput = function () {
        setTimeout(() => {
            // Ensure we are parsing the value correctly as a number, and prevent non-numeric input
            let amountUsed = parseFloat(totalInput.value.replaceAll(/\D/gm, ""));  // Remove non-numeric characters

            // If the input is not a valid number (NaN), set it to 0
            if (isNaN(amountUsed)) {
                amountUsed = 0;
            }

            const amountLeft = maxAmount - amountUsed;

            // Update the remaining total with the amount left
            remainingInput.value = amountLeft;

            // If amountLeft < 0, disable the Next button and show the alert message
            if (amountLeft < 0) {
                // Disable the Next button and prevent page transition
                nextButton.disabled = true;  // Disable the Next button
                Qualtrics.SurveyEngine.setNextButton(false);  // Prevent moving to the next page
                alertMessage.style.display = "block";  // Show the alert message
            } else {
                // If valid, enable the Next button and hide the alert
                nextButton.disabled = false;  // Enable the Next button
                Qualtrics.SurveyEngine.setNextButton(true);  // Allow moving to the next page
                alertMessage.style.display = "none";  // Hide the alert message
            }
        }, 0);
    };

    // Start with the entire amount being available
    remainingInput.value = maxAmount;

    // Insert values into the alert message
    const alertMessage = qc.querySelector("#amountAlert");
    alertMessage.innerHTML = alertMessage.innerHTML.replace("+++++", alertText);
    alertMessage.innerHTML = alertMessage.innerHTML.replace("_____", maxAmount);
});


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5923 replies
  • February 3, 2025
O.kisa wrote:
TomG wrote:

@O.kisa,

Is your spending question and Constant Sum question? If so, as long as there is no display logic on the Constant Sum choices, you don’t need JS. Use custom validation where the first choice is less than the spending limit minus all the other choices. For example:

$e{ e://Field/SpendLimit -  q://QIDx/ChoiceNumericEntryValue/2 - q://QIDx/ChoiceNumericEntryValue/3 } 

Where QIDx is the QID of your constant sum question.

 

No, It did not work. But thank you.

It works. You must have done it incorrectly.


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 4, 2025
TomG wrote:
O.kisa wrote:
TomG wrote:

@O.kisa,

Is your spending question and Constant Sum question? If so, as long as there is no display logic on the Constant Sum choices, you don’t need JS. Use custom validation where the first choice is less than the spending limit minus all the other choices. For example:

$e{ e://Field/SpendLimit -  q://QIDx/ChoiceNumericEntryValue/2 - q://QIDx/ChoiceNumericEntryValue/3 } 

Where QIDx is the QID of your constant sum question.

 

No, It did not work. But thank you.

It works. You must have done it incorrectly.

Is it true ?

 


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 4, 2025
TomG wrote:
O.kisa wrote:
TomG wrote:

@O.kisa,

Is your spending question and Constant Sum question? If so, as long as there is no display logic on the Constant Sum choices, you don’t need JS. Use custom validation where the first choice is less than the spending limit minus all the other choices. For example:

$e{ e://Field/SpendLimit -  q://QIDx/ChoiceNumericEntryValue/2 - q://QIDx/ChoiceNumericEntryValue/3 } 

Where QIDx is the QID of your constant sum question.

 

No, It did not work. But thank you.

It works. You must have done it incorrectly.

When I use custom validation, it gives an error even when I spend less than the net income.


Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • February 4, 2025

You need to provide more details - a survey preview link, your question structure, how will the choices be made etc.

 


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 17 replies
  • February 4, 2025
ahmedA wrote:

You need to provide more details - a survey preview link, your question structure, how will the choices be made etc.

 

Here is the link: https://tilburgss.co1.qualtrics.com/jfe/preview/previewId/3b01a810-1a59-4458-9be6-dc7b05262950/SV_0U7WCRd4Rz1gtU2?Q_CHL=preview&Q_SurveyVersionID=current


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5923 replies
  • February 4, 2025
O.kisa wrote:
ahmedA wrote:

You need to provide more details - a survey preview link, your question structure, how will the choices be made etc.

 

Here is the link: https://tilburgss.co1.qualtrics.com/jfe/preview/previewId/3b01a810-1a59-4458-9be6-dc7b05262950/SV_0U7WCRd4Rz1gtU2?Q_CHL=preview&Q_SurveyVersionID=current

Now I see - it isn’t a Constant Sum. I happen to offer a function that does this.


Leave a Reply