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.
@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.
@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.
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);
});
@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.
@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.
You need to provide more details - a survey preview link, your question structure, how will the choices be made etc.
Now I see - it isn’t a Constant Sum. I happen to offer a function that does this.