limit the number of words in the text box | XM Community
Skip to main content
I am trying to limit the number of words in a text box to 75 but they can be less than that.
Yes you can add validation for character limit.
Read below for text entry validation



https://www.qualtrics.com/support/survey-platform/survey-module/editing-questions/question-types-guide/standard-content/text-entry/
Hello @Linda_charlton ,



Use custom validation on the text entry question, select "Matches Regex" and paste following regex in the next box - `^(?:\\b\\w+\\b[\\s\\r\\n]*){1,75}$`



The above will make user to input at least one word and not more than 75
Hello @Linda_charlton ,



Following solution will allow respondent to leave the text field blank(which can be avoided by 'Force response') but will not allow them to type after 75 words.



Paste the following code in the js(onReady) of the Text entry question



var that=this.questionId;

var numOfWords = 0;

jQuery("[id='QR~"+that+"']").on("cut copy paste",function(e) {

e.preventDefault();

});

jQuery("[id='QR~"+that+"']").keypress(function(){

numOfWords =jQuery("[id='QR~"+that+"']").val().replace(/^[\\s,.;]+/, "").replace(/[\\s,.;]+$/, "").split(/[\\s,.;]+/).length;

if( numOfWords > 74 ){

event.preventDefault();

}

});
Simple validation will also work for purely text entry question. Put maximum characters to 75.

!
> @bansalpeeyush29 said:

> Simple validation will also work for purely text entry question. Put maximum characters to 75.

> !

>

> But this will work for character count not for word count.
Right, I thought it is for characters 🙂
I was looking for limit to words not Characters.
is there a way to limit the words and give them the count of words so they would know how many they have left.
> @Shashi said:

> Hello @Linda_charlton ,

>

> Following solution will allow respondent to leave the text field blank(which can be avoided by 'Force response') but will not allow them to type after 75 words.

>

> Paste the following code in the js(onReady) of the Text entry question

>

> var that=this.questionId;

> var numOfWords = 0;

> jQuery("[id='QR~"+that+"']").on("cut copy paste",function(e) {

> e.preventDefault();

> });

> jQuery("[id='QR~"+that+"']").keypress(function(){

> numOfWords =jQuery("[id='QR~"+that+"']").val().replace(/^[\\s,.;]+/, "").replace(/[\\s,.;]+$/, "").split(/[\\s,.;]+/).length;

> if( numOfWords > 74 ){

> event.preventDefault();

> }

> });

>

>



Is there a way so they can see the number of words as they are keying?
Hello @Linda_charlton ,



Paste the following code in the js(onReady) of the text entry question. The below code will show words remaining



jQuery("<p id='count'>Words remaining: 0</p>").insertAfter(".QuestionText");

var that=this.questionId;

var numOfWords = 0;

jQuery("[id='QR~"+that+"']").on("cut copy paste",function(e) {

e.preventDefault();

});

jQuery("[id='QR~"+that+"']").keypress(function(){

numOfWords =jQuery("[id='QR~"+that+"']").val().replace(/^[\\s,.;]+/, "").replace(/[\\s,.;]+$/, "").split(/[\\s,.;]+/).length;

jQuery("#count").text("Words remaining: "+(75-parseInt(numOfWords)));

if( numOfWords > 74 ){

event.preventDefault();

}

});
I get words remaining for all text boxes that I have on the survey and the one that I want to have working the 0 will not change not matter how much I type
> @Shashi said:

> Hello @Linda_charlton ,

>

> Paste the following code in the js(onReady) of the text entry question. The below code will show words remaining

>

> jQuery("<p id='count'>Words remaining: 0</p>").insertAfter(".QuestionText");

> var that=this.questionId;

> var numOfWords = 0;

> jQuery("[id='QR~"+that+"']").on("cut copy paste",function(e) {

> e.preventDefault();

> });

> jQuery("[id='QR~"+that+"']").keypress(function(){

> numOfWords =jQuery("[id='QR~"+that+"']").val().replace(/^[\\s,.;]+/, "").replace(/[\\s,.;]+$/, "").split(/[\\s,.;]+/).length;

> jQuery("#count").text("Words remaining: "+(75-parseInt(numOfWords)));

> if( numOfWords > 74 ){

> event.preventDefault();

> }

> });



I get words remainings: 0 for all text boxes and will not change no matter how many words I type.
> @Linda_charlton said:

> I get words remaining for all text boxes that I have on the survey and the one that I want to have working the 0 will not change not matter how much I type

>

Are you using text type as "Form"

Can you upload the screenshot of the question here
> @Shashi said:

> > @Linda_charlton said:

> > I get words remaining for all text boxes that I have on the survey and the one that I want to have working the 0 will not change not matter how much I type

> >

> Are you using text type as "Form"

> Can you upload the screenshot of the question here

>



Thanks Shashi.

Here is screen shot of a preview after I put the code in. I was just doing a essay question type but the Words remaining shows up on all the questions.!
Hello @Linda_charlton ,



Use the below code



var that=this.questionId;

jQuery("<p id='count'>Words remaining: 75</p>").insertAfter("#"+that +" .QuestionText");

var numOfWords = 0;

jQuery("#"+that +" .InputText").on("cut copy paste",function(e) {

e.preventDefault();

});

jQuery("#"+that +" .InputText").keypress(function(){

numOfWords =jQuery("#"+that +" .InputText").val().replace(/^[\\s,.;]+/, "").replace(/[\\s,.;]+$/, "").split(/[\\s,.;]+/).length;

jQuery("#count").text("Words remaining: "+(75-parseInt(numOfWords)));

if( numOfWords > 74 ){

event.preventDefault();

}

});

Hello @Linda_charlton ,

Use the below code

var that=this.questionId;
jQuery("<p id='count'>Words remaining: 75</p>").insertAfter("#"+that +" .QuestionText");
var numOfWords = 0;
jQuery("#"+that +" .InputText").on("cut copy paste",function(e) {
e.preventDefault();
});
jQuery("#"+that +" .InputText").keypress(function(){
numOfWords =jQuery("#"+that +" .InputText").val().replace(/^[\\s,.;]+/, "").replace(/[\\s,.;]+$/, "").split(/[\\s,.;]+/).length;
jQuery("#count").text("Words remaining: "+(75-parseInt(numOfWords)));
if( numOfWords > 74 ){
event.preventDefault();
}
});

Hi @Anonymous 

I have tried this code for 300 words and it works fine, sometimes the number does not change as I type, but it's ok, thank you.

I just have a question, while in the survey, the field where the code is entered does not allow me to paste text from another source. I'm not a JS expert, not even a rookie, is this possible? if so, how?

Thanks!


Leave a Reply