Hi,
I need to compare the sum of a constant sum question against an embedded field data and if the survey should not allow the user to continue if the sum is more than then embedded data. The embedded data comes from the contact panel and will have a numeric value. The Qualtrics only provide validations to check the sum against a static number. I do not know anything about java script, but I think this can be done using a js in the question. Any help is very much appretiated.
Thanks!
Sree
You don't need javascript. Use the must total validation and refer to your embedded data over there. Something like ${e://Field/your_required_value}
Hi ahmedA , thanks for your response on this. That worked, but in my case, the sum can be less than the embedded field. When I give this validation, it forces the user to make the sum equal to the embedded data. I'm looking for less than or equal to the embedded field.
Try if this works for you.
Qualtrics.SurveyEngine.addOnload(function () {
//Diables the next button at the start
// You can delete this if you are fine with the sum as being zero
this.disableNextButton();
});
Qualtrics.SurveyEngine.addOnReady(function () {
//Get the Question Id
const qid = this.questionId;
// Get the number of choices
n_choices = Qualtrics.SurveyEngine.registryqid].getChoices();
const base_msg = jQuery("#"+qid+" .QuestionText").html();
// Detect a change in any of the choices
n_choices.forEach((item) => {
console.log("input detected");
document.querySelector("#QR\\\\~" + qid + "\\\\~" + item).oninput = function () {chk_sum();};
});
that = this;
function chk_sum() {
var max_sum = parseInt("${e://Field/max_sum}");
var current_sum = 0;
padding_pre = '
';
padding_post = '';
err_msg = "The total should be more than zero and less than " + max_sum + " to proceed";
//Iterate over each of choices
n_choices.forEach((item) => {
curr_val = parseInt(document.querySelector("#QR\\\\~" + qid + "\\\\~" + item).value);
// Check for empty cells
if (isNaN(curr_val)) {
if (document.querySelector("#QR\\\\~" + qid + "\\\\~" + item).value == "") {
curr_val = 0;
} else {
err_msg = "Please enter only valid integer numbers.";
}
}
//Check for invalid characters
if (document.querySelector("#QR\\\\~" + qid + "\\\\~" + item).value.search(/\\D/) != -1){
err_msg = "Please enter only valid integer numbers.";
curr_val = max_sum + 1;
}
current_sum += curr_val;
});
err_msg = base_msg + padding_pre + err_msg + padding_post;
//Checks for zero and the value being more than zero and less than the max_sum
// If you are fine with zero then delete "current_sum >0 &&"
if (current_sum > 0 && current_sum <= max_sum) {
that.enableNextButton();
jQuery("#"+qid+" .QuestionText").html(base_msg);
} else {
that.disableNextButton();
jQuery("#"+qid+" .QuestionText").html(err_msg);
}
}
});
Hi ahmedA, thanks for the java code. But I got a different idea for this using custom validation. If the constant sum has 3 choices, we can add a custom validation to make sure that the 1st choice is less than or equal to the max count minus the sum of the other 2 choices. This was done using the math operators in the conditional format. So it looks like the below formula.
q://QID19/ChoiceNumericEntryValue/1 <= $e{ e://Field/Max_Count - ( q://QID19/ChoiceNumericEntryValue/2 + q://QID19/ChoiceNumericEntryValue/3 ) }
When the count goes more than the max_count, this condition will fail and will trigger an error message. Once again thanks for looking into this.
Ah! That's brilliant. I didn't even think going that way.
They say that when you have a hammer, everything looks like a nail. :-)
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.