Text entry word limits | XM Community
Question

Text entry word limits

  • 28 July 2021
  • 3 replies
  • 21 views

Hello, I found the following code that limits words and displays a word count for a text entry question and is nearly ideal for what I need, however it does not enable individuals to paste text into a box. Can someone assist with changing the code to allow for pasted text?

var that=this.questionId;
  jQuery("

Words remaining: 100

").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("#count01").text("Words remaining: "+(100-parseInt(numOfWords)));
  if( numOfWords > 100 ){
    event.preventDefault();
  }
  }); 


3 replies

Badge

Hi,
There is function applied in this jQuery which is preventing "copy/paste" functionality. Just remove this below code and you will be able to copy paste.
  jQuery("#"+that +" .InputText").on("cut copy paste",function(e) {
   e.preventDefault();
  });

Userlevel 2
Badge +8

Sorry to dig up such an old post, but could somebody help me with figuring out how to use this code? I've tried a variety of ways to paste this into the Javascript section of the question I wish to apply it to, but I continually receive the error "Invalid JavaScript! You cannot save until you fix all errors: Unexpected token )".
I don't work with Javascript much at all. Thanks for any advice.

Userlevel 7
Badge +27

Hi BrianGunther , for the version that does allow copy/paste, try replacing the entirety of the question's JavaScript with the below:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var that=this.questionId;

  jQuery("

Words remaining: 100

").insertAfter("#"+that +" .QuestionText");

   var numOfWords = 0;

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

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

    jQuery("#count01").text("Words remaining: "+(100-parseInt(numOfWords)));

  if( numOfWords > 100 ){

    event.preventDefault();

  }

  });

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

For the version that does not allow copy paste, use the below:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var that=this.questionId;

  jQuery("

Words remaining: 100

").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("#count01").text("Words remaining: "+(100-parseInt(numOfWords)));

  if( numOfWords > 100 ){

    event.preventDefault();

  }

  }); 

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

Leave a Reply