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

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


Dax
Qualtrics Employee
Forum|alt.badge.img+2
  • Qualtrics Employee
  • 23 replies
How do I show the number of words in text entry boxes? I have several on the page.

Best answer by AnthonyR

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
View original

11 replies

JulieT
Level 3 ●●●
Forum|alt.badge.img+10
  • Level 3 ●●●
  • 61 replies
  • November 16, 2017
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.

AnthonyR
Level 4 ●●●●
Forum|alt.badge.img+7
  • Level 4 ●●●●
  • 306 replies
  • November 16, 2017
> @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.

AnthonyR
Level 4 ●●●●
Forum|alt.badge.img+7
  • Level 4 ●●●●
  • 306 replies
  • Answer
  • November 20, 2017
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

Akdashboard
Level 4 ●●●●
Forum|alt.badge.img+6
  • Level 4 ●●●●
  • 488 replies
  • July 10, 2019
> @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.

ctribucher
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!

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • September 9, 2019
> @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.

ctribucher
  • 6 replies
  • September 10, 2019
Thanks, @TomG! I feel like this should be easier than it is turning out to be. Will start a new thread.

Forum|alt.badge.img
  • 3 replies
  • August 26, 2020

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!


Forum|alt.badge.img+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


  • 1 reply
  • March 8, 2021

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


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • March 8, 2021

Leave a Reply