Javascript custom validation error message displaying twice | XM Community
Skip to main content
Question

Javascript custom validation error message displaying twice

  • January 12, 2024
  • 1 reply
  • 153 views

Forum|alt.badge.img+1

I am trying to have a custom validation error which gives out a specified error message when that error occurs. Here is my code for the same: 
 

Qualtrics.SurveyEngine.addOnload(function() {
    var q = jQuery("#" + this.questionId);
    var ve = q.find(".ValidationError");
    var inputText = q.find(".InputText");
    var nextButton = jQuery("#NextButton");

    inputText.on("blur", function(e) {
        var inputValue = parseFloat(this.value);

        if (isNaN(inputValue)) {
            ve.html("Please enter a valid number within the range of 0-100. <br> กรุณากรอกตัวเลขที่ถูกต้องในช่วง 0-100");
            ve.show();
            this.value = "";
            // Disable the "Next" button
            nextButton.prop("disabled", true);
        } else if (inputValue > 100) {
            ve.html(this.value + " is greater than 100. Please enter a valid number within the range of 0-100. <br>" + this.value + " มีค่ามากกว่า 100 กรุณากรอกตัวเลขที่ถูกต้องในช่วง 0-100"
);
            ve.show();
            this.value = "";
            // Disable the "Next" button
            nextButton.prop("disabled", true);
        } else if (inputValue < 0) {
            ve.html(this.value + " is less than 0. Please enter a valid number within the range of 0-100.  <br>" + this.value + " มีค่าน้อยกว่า 0 กรุณากรอกตัวเลขที่ถูกต้องในช่วง 0-100"
);
            ve.show();
            this.value = "";
            // Disable the "Next" button
            nextButton.prop("disabled", true);
        } else {
            nextButton.prop("disabled", false);
            ve.hide();
        }
    });
});

 

However, in the survey, the error message is getting displayed twice like below. Any help figuring out why this is happening/ how to fix this would be appreciated. 

 

 

Thanks

1 reply

Shashi
Level 8 ●●●●●●●●
Forum|alt.badge.img+34
  • Level 8 ●●●●●●●●
  • 654 replies
  • January 12, 2024

Use below line instead of existing one:

var ve = q.find(".ValidationError:eq(0)");