Enforce whole number in constant sum | XM Community
Solved

Enforce whole number in constant sum

  • 22 November 2020
  • 12 replies
  • 221 views

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

icon

Best answer by rondev 23 November 2020, 14:09

View original

12 replies

Userlevel 7
Badge +22

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(/[^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!

Userlevel 7
Badge +22

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

Userlevel 7
Badge +22

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

Userlevel 7
Badge +22

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(/[^0-9]/g,""); 
but the same code doesn't work in a matrix table constant sum - does it need to be different ?

Userlevel 7
Badge +27

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.

Leave a Reply