Remove line breaks and quotation marks from essay text entry | XM Community
Solved

Remove line breaks and quotation marks from essay text entry


Userlevel 1
Badge +1

Hi all,
In a side-by-side question, I have participants inputting (a) some text in an essay box, and (b) a numerical rating from a drop-down.
I then use some JavaScript to (a) sort numerical responses into ascending order, and (b) set Qualtrics embedded data based on the text of largest 3 numerical ratings. Everything works fine unless a user inputs a line break or quotation mark in their response, in which case the code breaks & nothing displays.
Anyone know how I can remove line breaks and quotation marks from the user input?
I tried this:
function cleanText(input_string) {
input_string = input_string.replaceAll(/(\\r\\n|\\n|\\r)/gm,"");
 input_string = input_string.replaceAll("\\"", "\\'");
 return (input_string);
}
var text1 = "${q://QID34%231/ChoiceTextEntryValue/1/1}";
text1 = cleanText(text1)
But this didn't work. My suspicion is that it breaks at the point of initializing the variable

text1
if it contains a line break or quotation mark.
I'm rather novice at JS, the sorting function for the matrix was with the help of a friend.
Thank you in advance!

icon

Best answer by TomG 28 June 2022, 22:16

View original

11 replies

Userlevel 1
Badge +1

Note: Apparently things are breaking down very early in this process.
If start the JS block with

alert("${q://QID34%231/ChoiceTextEntryValue/1/1}")
, the alert is empty if it contains any quotation marks or line breaks.

Userlevel 1
Badge +1

TomG any of your usual brilliant ideas?

Userlevel 7
Badge +27

https://community.qualtrics.com/XMcommunity/discussion/comment/47276#Comment_47276Remove the line breaks and double quotes in the addOnPageSubmit() function attached to the Side-by-Side question. That way it will be saved without line breaks and quotes. Something like:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var input = jQuery("#"+this.questionId+" InputText").first();
input.val(input.val().replace(/\\s+/g," ").replace(/\\"/g,"'"));
});

Userlevel 1
Badge +1

Absolute magic. Thanks, TomG!

Userlevel 7
Badge +30

https://community.qualtrics.com/XMcommunity/discussion/comment/47276#Comment_47276Remove the line breaks and double quotes in the addOnPageSubmit() function attached to the Side-by-Side question. That way it will be saved without line breaks and quotes. Something like:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var input = jQuery("#"+this.questionId+" InputText").first();
input.val(input.val().replace(/\\s+/g," ").replace(/\\"/g,"'"));
});

This is very helpful. How would we change the code to apply it to a Text Entry question type instead of SBS?

Userlevel 7
Badge +27

@MatthewM - It should work as-is for a text entry question.

Userlevel 7
Badge +30

Thanks for letting me know, @TomG ! I’m not sure what I’m missing, but I can’t seem to get it to work; the line breaks and quotes are still present when I download the data to a csv. I even made sure that all the quotes in the Javascript that I copied and pasted are straight.

Not the end of the world; this is more of a nice-to-have than a project requirement. Always appreciate the help, even when I can’t figure out how to implement it!

 

 

Userlevel 7
Badge +27

@MatthewM - Somehow in the transition to the new Community platform, the regex got double escaped, which broke it.  It should be:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var input = jQuery("#"+this.questionId+" .InputText").first();
input.val(input.val().replace(/\s+/g," ").replace(/\"/g,"'"));
});

 

Userlevel 7
Badge +30

Ah, I see it now. That fixed it. Thank you so much!

Userlevel 7
Badge +33

@MatthewM - Somehow in the transition to the new Community platform, the regex got double escaped, which broke it.  It should be:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var input = jQuery("#"+this.questionId+" .InputText").first();
input.val(input.val().replace(/\s+/g," ").replace(/\"/g,"'"));
});

 

Dear @TomG this works and replace line breaks within text field on hitting next button. Can we keep it as is and how can i store in embedded field just without line breaks. As in my webservice i am using embedded field and it have line breaks.

Userlevel 7
Badge +27

@MatthewM - Somehow in the transition to the new Community platform, the regex got double escaped, which broke it.  It should be:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var input = jQuery("#"+this.questionId+" .InputText").first();
input.val(input.val().replace(/\s+/g," ").replace(/\"/g,"'"));
});

 

Dear @TomG this works and replace line breaks within text field on hitting next button. Can we keep it as is and how can i store in embedded field just without line breaks. As in my webservice i am using embedded field and it have line breaks.

Change the last line to something like:

Qualtrics.SurveyEngine.setEmbeddedData("nobreaks",input.val().replace(/\s+/g," ").replace(/\"/g,"'"));

 

Leave a Reply