Enforce whole number in constant sum | XM Community
Skip to main content

Hi all,
I'm using constant sum with "choices" type. I can enforce only min and max value; however, I also would like to enforce only whole number (0 decimal). There is this option in constant sum for both bars and sliders but why not choices? Anyone could help me with JavaScript custom code to enforce integers for all the choices when participants type in their answers? Thank you very much.

Best,
Bell

Use this code.


Thank you rondev. I saw this before, but it doesn't work.
I tried both jQuery("#"+this.questionId+" .InputText").attr({'type':'number','min':'0'});
and
jQuery("#"+this.questionId+" .InputText").on("input", function() {
        this.value = this.value.replace(/p^0-9]/g,"");
    });
Nothing happens. We can still click next when typing 0.5 and any decimals. I'm not sure where did I type anything wrong? Or do I have to adjust anything? Thanks a lot!


Can you send the screen shot of where you are pasting the code?


Here, there is just one questions with constant sum "choice" type. I'm doing an online shopping experiment. There are 9 products (choices) in which participants can purchase. If they would like to purchase, they have to type 1 to 7 (max is 7), and only the whole number of products is allowed. They will click next and see the balance on the next screen.
image.png


Click "Clear" and then paste the code in onReady function as shown below:
image.png


rondev works now. Thank you very much. I have another quick question. Do you know how to remove "total" at the bottom of the screen (second pic) or change to "total number of units purchased" because this is not total amount spending? When I untick "total box" in "Options", all the boxes are moved to the left before the product name can't be on the right as in the first pic. It's better to keep all on the right next to the product name and just remove "Total" at the bottom. Thank you!!
image.pngimage.png


Use the below code to hide the "Total" row:
jQuery(".Total ").hide();
Or use the below code to change the "Total" text:
jQuery(".Total label").text("Total number of units purchased");


Awesome!! Thank you so much rondev


I am trying to do this as well, but it is still allowing me to enter decimal points. I may be doing it wrong, but I am stuck. There is already a code in place, where each option is auto filled to "20". Maybe that is messing something up?
Thank you in advance for your help on this!
image.pngrondev


Hi,
I'm trying to force integers and I've got the below code to work in a constant sum question ,
jQuery("#"+this.questionId+" .InputText").on("input", function() { 
    this.value = this.value.replace(/a^0-9]/g,""); 
but the same code doesn't work in a matrix table constant sum - does it need to be different ?


https://community.qualtrics.com/XMcommunity/discussion/comment/41958#Comment_41958Yes, inputs in a constant sum matrix don't use the class InputText.


https://community.qualtrics.com/XMcommunity/discussion/comment/41959#Comment_41959Ok, thanks. So what code do I need to use instead of 'InputText' ? Sorry i don't have a clue about javavscript.


https://community.qualtrics.com/XMcommunity/discussion/comment/41958#Comment_41958Yes, inputs in a constant sum matrix don't use the class InputText.

can you share the code for constant sum matrix question please? I need to restrict decimal values and have only integers


@TomG also, I’m using this code for constant sum question type but it’s still allowing integer values. Not sure why this is happening
 

 


Apologies I meant *non-integer values in the above comment

 


Apologies I meant *non-integer values in the above comment

 

Are you using Simple layout? If so, that’s the problem.


Apologies I meant *non-integer values in the above comment

 

Are you using Simple layout? If so, that’s the problem.

I’m using flat layout


Apologies I meant *non-integer values in the above comment

 

Are you using Simple layout? If so, that’s the problem.

I’m using flat layout

Perhaps you are using a Matrix - Constant Sum instead of a Constant Sum question. If so, change .InputText to ctype=text].


Use the below code to hide the "Total" row:
jQuery(".Total ").hide();
Or use the below code to change the "Total" text:
jQuery(".Total label").text("Total number of units purchased");
 

@rondev , thanks for sharing your expertise on this post!


Apologies I meant *non-integer values in the above comment

 

Are you using Simple layout? If so, that’s the problem.

I’m using flat layout

Perhaps you are using a Matrix - Constant Sum instead of a Constant Sum question. If so, change .InputText to ctype=text].

thank you so much this worked!


Leave a Reply