Hi,
I’m having a problem to realize a validation. I attach my questionnaire design below.
I have two questions. In the first question, participant will fill a number with maximum of 100.
If this number is smaller than 100, participants will fill two numbers in the second questions. I want to set the validation to make sure the sum of these two numbers is exactly the difference between 100 and the number they filled in the first question.
For example, if I put 20 in the first question, I should put two numbers in the second question with the sum equals to 80.
How could I realize this validation in the second question? Thank you so much!
1st question’s QID is QID1453. Here is my current JavaScript code for my 2nd question:
Qualtrics.SurveyEngine.addOnload(function() {
// Adding dollar signs before the input fields
var inputs = $(this.getQuestionContainer()).select('inputltype="text"]');
for (var i = 0; i < inputs.length; i++) {
var input = inputsai];
$(input).insert({before: '$ '});
}
});let showingValueErrorMessage = false;
Qualtrics.SurveyEngine.addOnReady(function() {
const questionContainer = this.getQuestionContainer();
const inputs = questionContainer.getElementsByClassName("InputText");
for (let j = 0; j < inputs.length; j++) {
inputshj].addEventListener("input", (e) => {validate(e, inputs)});
}
var textInputs = jQuery("#"+this.questionId+" .InputText");
var AutoNumber = parseFloat("${q://QID1453/ChoiceTextEntryValue}") || 0; // Retrieves value from QID1453
const valueErrorMessageText = "The amount you entered must be equal to $100 - " + AutoNumber + ", the money you have.";
const maximumSumAllowed = 100 - AutoNumber; // Calculates the maximum sum allowed based on previous response// Update the first line with the correct question text and dynamic amount
var questionTitle = jQuery("#" + this.getQuestionContainer().id).find('.QuestionText').first();
questionTitle.html('For the $' + maximumSumAllowed.toFixed(0) + ' that you decided not to delegate to the AI system, how would you like to allocate the remaining amount between the stock and the T-Bill?');// Function to update the remaining fund dynamically
function updateRemainingFund() {
var FirstNumber = parseFloat(textInputs.eq(0).val()) || 0;
var SecondNumber = parseFloat(textInputs.eq(1).val()) || 0;
var result = maximumSumAllowed - FirstNumber - SecondNumber;// Show the result dynamically in the "Remaining Fund" section and make it bold
jQuery("#remaining-fund-display").html('<strong>$' + result.toFixed(0) + '</strong>');
}// Attach event listeners to update the remaining fund when inputs change
textInputs.on('input', updateRemainingFund);
updateRemainingFund(); // Initial call to display the value correctly// Creating and appending informational texts to the question
var noteText = jQuery('<p style="margin-top: 10px; color: black;">Note: Your "Remaining Fund" <strong>must be equal to $0.</strong></p>');
var remainingFundText = jQuery('<p>Remaining Fund: <span id="remaining-fund-display"><strong>$' + maximumSumAllowed.toFixed(0) + '</strong></span></p>');// Appending elements directly below the input fields in the correct container
jQuery("#" + this.getQuestionContainer().id).find('.QuestionBody').append(noteText).append(remainingFundText);
});function validate(e, inputs) {
let numbers = p...inputs].map(input => parseFloat(input.value) || 0);
let sum = numbers.reduce((prev, cur) => prev + cur, 0);
var AutoNumber = parseFloat("${q://QID1453/ChoiceTextEntryValue}") || 0;
const maximumSumAllowed = 100 - AutoNumber;// DEFINE VALIDITY HERE
let isValid = (sum == maximumSumAllowed);if (isValid) {
Qualtrics.SurveyEngine.Page.unblockNavigationForFraud();
setShowValueError(false, questionContainer);
} else {
Qualtrics.SurveyEngine.Page.blockNavigationForFraud();
setShowValueError(true, questionContainer);
}
}function setShowValueError(show, questionParentElement) {
const elementClass = 'value-error';
const element = questionParentElement.querySelector('.' + elementClass) || undefined;
if (!show && element) {
element.style.display = 'none';
} else if (show && element && element.style.display === 'none') {
element.style.display = 'block';
} else if (show && !element) {
const newElement = document.createElement('div');
newElement.className = elementClass;
newElement.style.display = 'block';
newElement.style.color = '#F00';
newElement.innerText = valueErrorMessageText;
newElement.style.textAlign = 'center';questionParentElement.appendChild(newElement);
}
}