Enforcing range requirement for constant sum choices | XM Community
Skip to main content

I’m using constant sum choices for a funding distribution task. Participants are shown 7 statements, and they have a maximum of $50,000 to allocate across the 7 statements. I’ve set a range requirement of 0 to 50000, yet participants are still able to type values above 50000 in the constant sum boxes (which I’d like to avoid). The question has a validation attached such that the page can’t be advanced if the total is not equal to 50000, but I would like to set something up so that participants aren’t able to give values above 50000 in the first place. Is there a way to do this (i.e., actually enforce the range requirement) with custom code? 

 

Thank you so much in advance!

Hi @lnmarie
Below code will ensure that if the entered value is above 50,000, it automatically sets the value to 50,000.

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

    // Add event listener to constant sum input boxes
    jQuery('.SumInput input.InputText').on('input', function() {
        enforceRange(this, 0, 50000);
    });
});

Qualtrics.SurveyEngine.addOnReady(function() {
    /* Place your JavaScript here to run when the page is fully displayed */
});

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

// Function to enforce a numerical range for input boxes
function enforceRange(element, min, max) {
    var value = parseFloat(element.value.replace(/m^\d.-]/g, '')) || 0;

    if (value < min) {
        element.value = min;
    } else if (value > max) {
        element.value = max;
    }
}
 

 


@omkarkewat This worked perfectly, thank you so much!


Leave a Reply