JavaScript to Convert String to Numerical Value After Custom Validation When Entering Monetary Value | XM Community
Skip to main content

We have been using the JS below directly within a form-type question to 1) add the "$" before each of the input fields and 2) validate so that only numerical values are entered and they are converted to display commas to the respondent when it is 1,00+ (our respondents have values in the millions +).
We have this all working properly; however, I would like to run calculations with these values after a page break within a text/graphic question type. In order to do so for any values for 1,000+ , I'd have to convert from a string to a numerical value to allow the Qualtrics calculation to work.
Is the conversion something that I could add to the Unload portion of the JS on the same question just below this script? Forgive me, I'm still quite the beginner with JS and I also couldn't find a similar existing question in my search.

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place Your JavaScript Here*/
// This code adds # prefix before textbox
 var questionId = this.questionId;
 var prefix = "$ ";
 crAddInputPrefix(questionId, prefix);
// This code filters text other than numbers entered from keyboard and 
// formats numbers with commas between each group of 3 digits for readability.
 var InputId1 = $("QR~"+this.questionId+"~1"); 
 var InputId2 = $("QR~"+this.questionId+"~2");
 var promptText = "";
 crAddInputTextNumberValidator(InputId1,promptText);
 crAddInputTextNumberValidator(InputId2,promptText);
});

I have the same question!


I have the same question!

Use the addOnPageSubmit() function. 


@TomG can you point me to documentation on this function? I’m new to custom code/javascript


@Anahivette - To my knowledge, it isn’t documented.  It is just like the other Qualtrics addOn… functions (e.g., addOnload) where ‘this’ is the question object. The differences is that it is called when the Next button is clicked.


Leave a Reply