I have a question which is of the matrix table form with text entry and 2 statements. I want to add custom validation to ensure that the first value if greater than 0 and the second value is greater than the first value. I am using the following code but the second part of the code is not working. Any help in debugging would be much appreciated.
Thanks
Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery("#" + this.questionId);
var nextButton = jQuery("#NextButton");
// Attach blur event to text entry fields in the Matrix Table
q.find("input.type='text']").on("blur", function() {
var cell = jQuery(this).closest("td");
var row = cell.closest("tr").index();
var col = cell.index();
// Perform validation based on the row and column indices
if (row === 0) {
// Validation for the first statement's text entry column
validateNumberForStatement1(q, row, col, this.value);
} else if (row === 1) {
// Validation for the second statement's text entry column
validateNumberForStatement2(q, row, col, this.value);
}
});
// Function to validate the entered number for the first statement
function validateNumberForStatement1(q, row, col, value) {
var ve = q.find(".ValidationError").eq(0);
if (value <= 0) {
ve.html("Statement 1, Cell (" + (row + 1) + ", " + (col + 1) + "): " + value + "cannot be less than 0");
ve.show();
q.find("input"type='text']").eq(row * q.find("tr:first td").length + col).val(""); // Clear the invalid value
nextButton.prop("disabled", true);
} else {
ve.html(""); // Clear any existing error message
ve.hide();
nextButton.prop("disabled", false);
}
}
// Function to validate the entered number for the second statement
function validateNumberForStatement2(q, row, col, value) {
var ve = q.find(".ValidationError").eq(0);
var cell1_1_value = q.find("inputttype='text']").eq(row * q.find("tr:first td").length).val();
if (col === 1 && value < cell1_1_value) {
ve.html("Statement 2, Cell (" + (row + 1) + ", " + (col + 1) + "): " + value + " cannot be less than"+ cell1_1_value);
ve.show();
q.find("input,type='text']").eq(row * q.find("tr:first td").length + col).val(""); // Clear the invalid value
nextButton.prop("disabled", true);
} else {
ve.html(""); // Clear any existing error message
ve.hide();
nextButton.prop("disabled", false);
}
}
});