Numeric text box commas and points as decimals | XM Community
Skip to main content

I have a survey with a couple of willingness-to-pay (WTP) questions. Those questions can be answered through a textbox to prevent any bias by the anchor points of a slider. The problem with the text box is that people who fill out my survey perhaps indicate their WTP with a comma or with a dot as a decimal place.

In the final data, the observations that used a comma to indicate decimals are just shown without any decimal point (i.e., 3,5 → 35). How do I solve this issue? Is there any way other than telling participants to please use decimal points?

Final note: most answers for the WTP will be between 2 and 10, so participants do not use commas (or points) to indicate thousands.

I am very thankful for any help. Enjoy the holidays :)

@cane van cane

You can limit it via regex and JavaScript combination. Eliminate the ‘,’ input and limit number from 2-10 along with decimal places (below code limits to 2 digits after decimal). If the input doesn’t match regex it would clear the text box. Add below code in Question JavaScript section.

Qualtrics.SurveyEngine.addOnload(function() {
var inputElement = jQuery("#" + this.questionId + " .InputText");

inputElement.on("blur", function() {
var inputValue = jQuery(this).val();

// Regex for valid number format (2-9.99 or 10)
var numberPattern = /^((10)|((2-9](\.\d{0,2})?))$/;
if (!numberPattern.test(inputValue)) {
// Invalid input
jQuery(this).val(""); // Clear the input field or handle as needed
}
});
});

Hope it helps!

Happy Holidays


@Deepak 

 

Thank you very much for your reply. However, I was more looking for something that would convert the input with a comma decimal into an input with a dot decimal.

My way how I handeled this now is that I just allowed a normal text entry and  I will clean the data from that afterwards in R, where it is quite easy to replace the comma by a dot. Furthermore, that allowes me to take care of other edge cases, like currency signs, without the participant bein annoyed by error messages.

 

Nevertheless, I am still very thankeful for your answer and it motivates me to get deeper into JavaScript, since it looks like a very helpful tool.


@Deepak

 

Thank you very much for your reply. However, I was more looking for something that would convert the input with a comma decimal into an input with a dot decimal.

My way how I handeled this now is that I just allowed a normal text entry and  I will clean the data from that afterwards in R, where it is quite easy to replace the comma by a dot. Furthermore, that allowes me to take care of other edge cases, like currency signs, without the participant bein annoyed by error messages.

 

Nevertheless, I am still very thankeful for your answer and it motivates me to get deeper into JavaScript, since it looks like a very helpful tool.

Your approach does make sense if the participant doesn’t want to be annoyed but if you want to replace comma by decimal the below code does the job. 

If you want to also allow currency signs you need to change regex as well.

Qualtrics.SurveyEngine.addOnload(function() {
var inputElement = jQuery("#" + this.questionId + " .InputText");

inputElement.on("blur", function() {
var inputValue = jQuery(this).val();

// Replace commas with decimal points
var formattedInputValue = inputValue.replace(/,/g, '.');

// Regex for valid number format (2-9.99 or 10)
var numberPattern = /^((10)|((2-9](\.\d{0,2})?))$/;

if (!numberPattern.test(formattedInputValue)) {
// Invalid input
jQuery(this).val(""); // Clear the input field or handle as needed
} else {
// Update the input field with the formatted value
jQuery(this).val(formattedInputValue);
}
});
});

 


@cane van cane,

I recommend using the cleave JS library. It will format numbers as they are typed and allow you to store them in ‘raw’ format so you won’t need to do any post-processing. I believe there are some other posts with specific of implementing cleave in Qualtrics. 


Leave a Reply