Qualtrics: Word Count/Limit JavaScript
Step 1: Define Custom Validation for Word Limit
To add a word limit to a question, you will add custom validation to the question in order for Qualtrics to check that the respondent's answer is within the word limit. This method also includes creating a custom error message if a respondent exceeds the set word limit.
Select the text entry question you wish to add the word count limit. On the left side bar, toggle to Add validation. Next using the dropdown, select Custom Validation.
You'll notice that the current question is selected on the Select Question dropdown (1 in image below). Next, you will need to select the question again for the Select Choices dropdown in the next box (2 in image below).
In the next dropdown, change the selection, is equal to, to Matches Regex (3 in image below).
In the last box of the custom validation conditions, copy the following text: ^\s*(\S+\s+){0,149}\S*$
and paste it in (4 in image below).
The last step in the custom validation (5 in image below) requires you to choose an error message to display if a respondent types more than 150 words.
You can set that up by clicking the dropdown menu (1 in image below), moving your mouse over My Library (2 in image below) and then selecting New Message (3 in image below).
In the New Message box, add a description such as "Response exceeds word count", then click Create New Validation. Enter a name for the error that you will remember, such as "Over word count", then begin typing your custom message you want the respondent to see if they exceed 150 words. For example, "The response exceeds the max word count." Select Create Message once all the required boxes are filled.
Make sure to select Save when you're happy with the custom validation.
If you want to ensure that the respondent puts something in the text box, you will need to toggle on Add requirements and then select Force Response in the left side bar. Without having this selected, the respondent will be able to skip this question without answering it.
Step 2: Display Word Count to Respondent
Now that you've successfully created the limit on the word count, now we move on to add JavaScript which will embed the active word count into the survey.
Click on the text entry question you'd like to include the word count on. Navigate to Question Behavior on the left side bar and select JavaScript.
Paste the following code into the JavaScript window:
Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery("#"+this.questionId);
var input = q.find(".InputText");
input.after("<div style='font-size:0.8em;float:right'>Word count: <span class='wordCount'>0</span></div>");
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");
}
});
Click Save to return to the survey builder.
Once the custom validation and JavaScript are set up, you can now Preview your survey to ensure that 1) the word count is default set to zero; 2) the word count is updated as they type; and 3) respondents get an error message if they type more than 150 words.
Text entry question before a respondent begins typing and the word count is zero:
Newly created error message if the respondent exceeds the word limit:
Notes
- If you have more than one question with a word limit, then you might need to add a page break between them for the word count display to work properly on the subsequent one(s).
- Qualtrics Support does NOT support any javascript coding used to personalize surveys. However, there are many forums and resources available when you search for help on these types of topics on Google – from other knowledgeable experts.
Did this solve your issue??