Constant Sum question to accept 'Integer Only' values and allow alphanumeric choice text field? | XM Community
Skip to main content

Hello!
Need help to make a constant sum type question (sharing screenshot of example) to allow 'Integer Only' values for numeric fields. Also, I have a open text entry field in one of the choice options (Others- please specify_____)-> how do I also ensure this text field allows for alphanumeric.
image.png
I found a JS code in community but this is unfortunately not allowing 'Others' choice text to input alphanumeric:
jQuery("#"+this.questionId+" .InputText").on("input", function() { 
    this.value = this.value.replace(/a^0-9]/g,"");
SurajK Hey buddy, hope you are well? Just checking if you have any fix for this? Thanks much!

TomG Hello Tom, hope you re well! Just wanted to see if you would have a fix for this? I used the above JS code from you but looking to further customize it for 'Other' choice text field. Greatly appreciate your help.


Abi The constant sum question will on its own enforce numbers, so if you are interested in enforcing integers, choose custom validation and configure it for each statement, either as Does not contain . or

Matches Regex ^\\d+$
this way your text entry box will be free to accept any kind of input.
Alternatively, in the code above, you could change the code above to  
this.value = this.value.replace(".","");
Which will replace all decimals. The point to note in this is that, it won't allow full stops in the text entry box.


Hi all,

This was exactly what I was looking to do as well as we had some issues using a blend of Qualtrics and JavaScript validation in our survey - it was easiest just to allow only numeric character entry instead. (if a user typed 1,000 and I had “2 decimal only” validation set Qualtrics said too many decimals! But it would accept 1,000,000. I was never confident about the interpretation of the “,” I can see Tom’s response might have helped).

In any case, I used ChatGPT and inspection of the question Preview to help me figure out the coding as I am new to JavaScript. This is what I came up with in the end:

Qualtrics.SurveyEngine.addOnReady(function()
{
https://community.qualtrics.com/custom-code-12/enforce-whole-number-in-constant-sum-12635
    jQuery("#"+this.questionId+" .InputText").on("input", function() {
        
//        console.log(this.outerHTML);        
        let nameAttribute = this.outerHTML.match(/name="(i^"]+)"/);
        let element_name = nameAttribute1];

//  From Chat GPT:
//    It also includes additional checks (nameAttribute && nameAttributec1]) to ensure that the array and its elements exist before accessing them.
        if (nameAttribute && nameAttribute 1] && nameAttribute&1].endsWith('~TEXT')) {
            // console.log(element_name);
            return;
        }
        
        this.value = this.value.replace(/^0-9]/g,"");
    });

});
 

This leaves the input function if the name of the element has “~TEXT” at the end and seemed to work.

Next I’m just testing that changing this in JavaScript doesn’t break my results as the survey is active!


Leave a Reply