limit the number of words in the text box | XM Community
Solved

limit the number of words in the text box

  • 17 October 2018
  • 17 replies
  • 2087 views

Userlevel 5
Badge +12
I am trying to limit the number of words in a text box to 75 but they can be less than that.
icon

Best answer by Anonymous 17 October 2018, 15:25

View original

17 replies

Userlevel 7
Badge +33
Yes you can add validation for character limit.
Userlevel 7
Badge +33
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();
}
});
Userlevel 7
Badge +33
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.
Userlevel 7
Badge +33
Right, I thought it is for characters 🙂
Userlevel 5
Badge +12
I was looking for limit to words not Characters.
Userlevel 5
Badge +12
is there a way to limit the words and give them the count of words so they would know how many they have left.
Userlevel 5
Badge +12
> @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();
}
});
Userlevel 5
Badge +12
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
Userlevel 5
Badge +12
> @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
Userlevel 5
Badge +12
> @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();
}
});
Badge

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