Skip to Main Content

Qualtrics Tutorials: Adding a Word Limit and Word Count to a Text Entry Question

Qualtrics surveys can easily apply a character limit to a text entry question, but word count limits require extra effort. In this tutorial, we show how to apply a word count limit and display a counter of the number of words to the respondent.

Problem Statement

Suppose you want to add a text entry question in Qualtrics and want to apply a word limit of 350 words. You may also want to display the number of words the user has entered.

The built-in content validation options for text entry questions can apply a character limit for these question types -- but that is not the same as a word limit. So to actually achieve this, you'll need to be comfortable applying some custom coding.

The solution for this requires two steps: 1) define a custom validation condition on the text entry question to count the number of "word breaks" in the text and prevent the respondent from continuing if their answer is more than 350 words; and 2) add JavaScript to compute and display the active word count as the respondent types their response.

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. 

  1. 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

    Adding custom validation in the left hand toolbar.

  2. You'll notice that the current question is selected on the Select Question dropdown. Next, you will need to select the question again for the Select Choices dropdown in the next box.
  3. In the next dropdown, change the selection, is equal to, to Matches Regex.

    The custom validation window with Matches Regex selected.

    "Regex", or regular expressions, are a special syntax used to search for patterns and sequences in text strings.

  4. In the last box of the custom validation conditions, put the following text: ^(?:\b\w+\b[\s\r\n]*){0,350}$
  5. The last step in the custom validation requires you to choose an error message to display if a respondent types more than 350 words. You can set that up by clicking the dropdown menu, moving your mouse over My Library and then selecting New Message.

    Create a new message to display when the custom validation fails by clicking the arrow next to Word Count, hover over My Library and select New Message.

  6. In the New Message box, add a description such as "error message for word count", then click Create New Validation. Enter a name for the error that you will remember, such as "word count", then begin typing your custom message you want the respondent to see if they exceed 350 words. For example, "You've exceeded the word count of 350." Select Create Message once all the required boxes are filled.

    When creating a new message, add a description and specific message to be displayed when the error occurs.

  7. Make sure to select Save when you're happy with the custom validation.
  8. 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. 

  1. 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.

     

    Select JavaScript under Question Behavior to access the Java Script box.

     

  2. Paste the following code into the JavaScript window:

     

    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 JavaScript includes the creation of a display variable titled wordCountDisplay. The rest of the JS is code referring to how the word count is calculated. Once you've typed in the JavaScript, your code JavaScript window should look similar to this:

     

    Spacing and color schemes of Java Script once pasted into the Java Script box.

    Click Save to return to the survey builder.

  3. In the Question text, you will need to add a line that will link to the newly created, display variable. Beneath your question text, type: "Your word count is: ". Next, click on HTML view and paste the following code: <span id="wordCountDisplay">0</span>

 

The HTML view of the question text.

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 350 words.

 

Text entry question before a respondent begins typing and the word count is zero:

Showing word count is zero when there are no words in the text box.

 

Testing the word count JavaScript to update when a respondent is typing:

Showing the word count is 4 when the respondent typed "Using data collected from" in the text box.

 

Newly created error message if the respondent exceeds the word limit:

In Preview mode, showing the newly created error message when the word count limit is exceeded.

Sources Cited

The solutions for this tutorial were created based on Qualtrics Community Threads related to limiting the number of words in a text box and showing the number of words in text entry questions .

Tutorial Feedback