How can I set 2 different error messages based on the question requirement with JS? | XM Community
Skip to main content
Question

How can I set 2 different error messages based on the question requirement with JS?

  • May 12, 2022
  • 1 reply
  • 409 views

Forum|alt.badge.img+2
  • Level 2 ●●

Hi, I have a drag and drop question with a requirement of selecting 3 items from the list. And I would like to set 2 different error messages,
1)fewer than 3 items are selected
2)more than 3 items are selected
I edit the JS below for the error message of "more than 3 items are selected" condition, may I know how to add another error message for the condition "fewer than 3 items selected"?
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
jQuery("#"+this.questionId+" .ValidationError").html(" You have selected too many items, please reduce to 3 choices by dragging extra selections out of the box");

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

1 reply

Tom_1842
Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • Level 8 ●●●●●●●●
  • September 12, 2022

Hi there, if you still need, I was able to get this to work by using a PGR question and the below JS in the OnReady section:
var group1count=jQuery("#" +this.questionId+"group0 li").length;
 
if(group1count < "3"){
    jQuery(".ValidationError").html("You selected fewer than 3.");
}

if(group1count > "3"){
    jQuery(".ValidationError").html("You selected more than 3.");
}
You might also check out this other recent post that deals with custom validation messages using JS.