Debugging validation code | XM Community
Skip to main content

I have the following: A side-by-side question with four text entry questions, and 16 rows.

I need the following: Condition (1)Validation to check that the entries in the third column are only numbers, between 1 and 16. Condition (2) no number in the third column should appear twice. I have attached the code.

The problem: I get the error “The rank 1 is already assigned. Please enter a unique value for each factor”. every time I finish the question and hit the next button. 

I use validationIndices for condition (1) and enteredValues for condition (2).

I have been stuck with this problem for a week. Any pointers are appreciated. 

The code:

 

Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/

    var textboxIndices = t3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63];
    // Loop through the specified indices
    for (var i = 0; i < textboxIndices.length; i++) {
    var index = textboxIndicesai];
    jQuery("#" + this.questionId + " input+type=text]:eq(" + index + ")").css("width", "350px");
}

});

Qualtrics.SurveyEngine.addOnReady(function() {
    /*Place your JavaScript here to run when the page is fully displayed*/
    jQuery("#"+this.questionId+" tr.Headings").hide();
    // Assuming 'this.questionId' refers to the third question in your side-by-side setup
    var totalRows = 16 * 4; // Change this to the actual number of rows in your question
    var validationIndices = o];

    // Generate indices based on the pattern
    for (var j = 2; j < totalRows; j += 4) {
        validationIndices.push(j);
    }

    // Array to store entered values
    var enteredValues = s];

    // Input validation code for the specified textboxes
    jQuery("#" + this.questionId + " inputttype=text]").on("change", function() {
        var currentIndex = jQuery(this).index("inputrtype=text]");

        if (validationIndices.includes(currentIndex)) {
            var inputValue = jQuery(this).val();

            if (inputValue !== "" && (isNaN(inputValue) || inputValue < 1 || inputValue > 16)) {
                alert("Please enter a rank between 1 and 16");
                jQuery(this).val("");
            } else {
                // Check for uniqueness
                if (enteredValues.includes(inputValue)) {
                    alert(“The rank " + inputValue + " is already assigned. Please enter a unique value for each factor");
                    jQuery(this).val("");
                } else {
                    enteredValues currentIndex] = inputValue;
                }
            }
        }
    });
});

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

});

 

 

@Amogha,

Your approach isn’t ideal, but I empathize with you. So:

Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery(this.questionContainer), ranks = q.find(".SBS3 input");
q.find(".SBS4 input").css("width","350px");
ranks.change(function() {
var changed = jQuery(this), val = this.value;
if(val!="" && (isNaN(val) || val<1 || val>16)) {
alert("Please enter a rank between 1 and 16");
this.value = "";
} else { // Check for uniqueness
var idx = ranks.index(this);
ranks.each(function(i) {
if(i!=idx && this.value!="" && this.value==val) {
alert("The rank " + val+ " is already assigned. Please enter a unique value for each factor");
changed.val("");
}
});
}
});
});

 


@Amogha,

Your approach isn’t ideal, but I empathize with you. So:

Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery(this.questionContainer), ranks = q.find(".SBS3 input");
q.find(".SBS4 input").css("width","350px");
ranks.change(function() {
var changed = jQuery(this), val = this.value;
if(val!="" && (isNaN(val) || val<1 || val>16)) {
alert("Please enter a rank between 1 and 16");
this.value = "";
} else { // Check for uniqueness
var idx = ranks.index(this);
ranks.each(function(i) {
if(i!=idx && this.value!="" && this.value==val) {
alert("The rank " + val+ " is already assigned. Please enter a unique value for each factor");
changed.val("");
}
});
}
});
});

 

Thank you so very much, it works well. All hail @TomG !!!! All I needed to do was change .SBS3 to SBS2 and SBS4 to SBS3. I think this is because the indices begin from 0. 

Quick question. I have not selected the ‘Add requirements’ option. However, I get a ‘Please answer this question’ error if all the 16 rows the fourth column is not answered. I want the participants to not answer the fourth question (in the side-by-side type of question) if they don’t want to. Thanks a lot again!


Thank you so very much, it works well. All hail @TomG !!!! All I needed to do was change .SBS3 to SBS2 and SBS4 to SBS3. I think this is because the indices begin from 0. 

Quick question. I have not selected the ‘Add requirements’ option. However, I get a ‘Please answer this question’ error if all the 16 rows the fourth column is not answered. I want the participants to not answer the fourth question (in the side-by-side type of question) if they don’t want to. Thanks a lot again!

SBS numbering starts at 1. I thought you had 4 side-by-side questions, but you actually have three and the first one has two columns.

If you haven’t turned on force response, then you must have custom validation that is causing the ‘Please answer this question’ message.


Leave a Reply