Limiting number of words in a response | XM Community
Question

Limiting number of words in a response

  • 21 July 2021
  • 2 replies
  • 88 views

I need to limit response to a maximum of 500 words. I have no idea what steps to take to do it. I have tried finding the answer in the community. Found one that said to use ^\\s(\\S+\\s+){0,249}\\S$ (which I changed the number to 500), and used the script provided:
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);
}

});
I must be missing a step? Can someone please help me?


2 replies

Badge +1

The one you found, is it from this link? Here they use a similar regular expression but in the custom validation section. That way you don't have to worry about scripting.
https://help.lafayette.edu/custom-validation-in-qualtrics/

No, it wasn't from that link, it was from this community. I will check it out.

Leave a Reply