How do I show the number of words in text entry boxes? I have several on the page. | XM Community
Solved

How do I show the number of words in text entry boxes? I have several on the page.

  • 15 November 2017
  • 11 replies
  • 1677 views

Userlevel 4
Badge +2
  • Qualtrics Employee
  • 23 replies
How do I show the number of words in text entry boxes? I have several on the page.
icon

Best answer by AnthonyR 20 November 2017, 22:14

View original

11 replies

Userlevel 5
Badge +10
1. Select your Text Entry question.
2. Click on Custom Validation in the right panel.
3. In the 2nd box, select your question text as the text to analyze.
4. Pull down the 3rd box and change it from "Equal to" to "Matches Regex"
5. Paste ^\\s*(\\S+\\s+){0,249}\\S*$ in the last text box. Change the upper number to the limit you choose.
Userlevel 7
Badge +7
> @JulieT said:
> 1. Select your Text Entry question.
> 2. Click on Custom Validation in the right panel.
> 3. In the 2nd box, select your question text as the text to analyze.
> 4. Pull down the 3rd box and change it from "Equal to" to "Matches Regex"
> 5. Paste ^\\s*(\\S+\\s+){0,249}\\S*$ in the last text box. Change the upper number to the limit you choose.

This will let you limit the number of words, but it doesn't display the number of words while typing, which is what I think the question is. I'll try and work out the JS for this later today.
Userlevel 7
Badge +7
Just got this worked out.
Create a text entry type question.
Create a span with an ID of wordCountDisplay in your question text, give it a default value of 0. Add this to your question's html.

For instance:

`Your word count is: <span id='wordCountDisplay'>0</span>`

Then add the following to the JavaScript editor for this queston:

Qualtrics.SurveyEngine.addOnReady(function()
{
var display = $('wordCountDisplay');
var questionID = this.questionId;
var textbox =$('QR~' + questionID);
var that = this;
function countWords(s){
s = s.replace(/\\n/g,' '); // newlines to space
s = s.replace(/(^\\s*)|(\\s*$)/gi,''); // remove spaces from start + end
s = s.replace(/[ ]{2,}/gi,' '); // 2 or more spaces to 1
if(s == ''){
return 0;
}else{
return s.split(' ').length;
}
}

textbox.onkeyup = function(e){
display.update(countWords(textbox.value));
console.log(textbox.value);
}

});

This will update dynamically as the respondent types in to the text box.

I have attached a qsf demo
Userlevel 7
Badge +6
> @JulieT said:
> 1. Select your Text Entry question.
> 2. Click on Custom Validation in the right panel.
> 3. In the 2nd box, select your question text as the text to analyze.
> 4. Pull down the 3rd box and change it from "Equal to" to "Matches Regex"
> 5. Paste ^\\s*(\\S+\\s+){0,249}\\S*$ in the last text box. Change the upper number to the limit you choose.

Has anyone gotten this to work? I can't get the custom validation to work with this logic.
I'm struggling with this is well. When I posed this question to Qualtrics Customer Support, they directed me to use embedded data.

Here's what I was told:

"Unfortunately Regex expressions and java script are outside of scope of support, and I cannot help you with them, but you can do something different, rather than using a Regex expression, you can create an embedded data and assign the word count in each keyboard event (in the same way that currently you are updating the count), then in the custom validation you can create the logic to pass the validation if embedded data text_length is greater than or equal to 500, it is important to create the embedded data before the block where you are using the java script, in your case you only have one element so the embedded data must be at the very top of the survey flow."

None of this made any sense to me and I sure would appreciate the help!
Userlevel 7
Badge +27
> @ctribucher said:
> I'm struggling with this is well. When I posed this question to Qualtrics Customer Support, they directed me to use embedded data.
>
> Here's what I was told:
>
> "Unfortunately Regex expressions and java script are outside of scope of support, and I cannot help you with them, but you can do something different, rather than using a Regex expression, you can create an embedded data and assign the word count in each keyboard event (in the same way that currently you are updating the count), then in the custom validation you can create the logic to pass the validation if embedded data text_length is greater than or equal to 500, it is important to create the embedded data before the block where you are using the java script, in your case you only have one element so the embedded data must be at the very top of the survey flow."
>
> None of this made any sense to me and I sure would appreciate the help!

It's not you. It doesn't make any sense because embedded data fields don't get updated until you submit the page. So, checking an embedded data field that won't be updated with the applicable data until after you leave the page will never work.

You should start a new thread with your specific question.
Thanks, @TomG! I feel like this should be easier than it is turning out to be. Will start a new thread.
Badge

AnthonyR or anyone else, could you please advise on how to apply this to:

1/ a text entry box that is added to a row statement
2/ a side-by-side text entry column

Any advise would be greatly appreciated!

Badge +1

This is helpful. I am wondering what is the code to reference a different question text. I would like it to show the character limit under the text entry box as opposed to above it. So, how would the Javascript read to count the words in the text questionID from the previous question. Or maybe there is another alternative way to do this.
image.png

https://www.qualtrics.com/community/discussion/comment/31919#Comment_31919I was looking for alternate codes to the one mentioned in this thread, as I need multiple word counts for multiple text entries within the same block. Saw another piece of code that adds a word count under the box of text entry. You also don't need to add anything to the HTML section of the description.
Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery("#"+this.questionId);
var input = q.find(".InputText");
input.after("

Word count: 0
");
var display = q.find(".wordCount");
countWords(input.get(0));
input.on("input", function() { countWords(this) });
input.blur(function() { this.value = this.value.trim(); });

function countWords(el) {
if(el.value.length > 0) display.text(el.value.match(/\\S+/g).length);
else display.text("0");
}
});
It's a little late, but perhaps you or some others may still have some use for it.
Qualtrics: Count and display the number of words in a text entry box. #qualtrics #js #jq #text #count

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/35395#Comment_35395I recognize that...I wrote it 😉

Leave a Reply