limit the number of words in the text box

Linda_charltonLinda_charlton Fargo NDCommunity Member Qubie ✭

limit the number of words in the text box

I am trying to limit the number of words in a text box to 75 but they can be less than that.

Best Answer

  • ShashiShashi India Wizard ✭✭✭✭✭
    Accepted Answer

    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();
        }
        }); 
    

Answers

  • bansalpeeyush29bansalpeeyush29 IndiaCommunity Member Wizard ✭✭✭✭✭

    Yes you can add validation for character limit.

  • ShashiShashi IndiaCommunity Member Wizard ✭✭✭✭✭

    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

  • ShashiShashi IndiaCommunity Member Wizard ✭✭✭✭✭

    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();
        }
        }); 
    
  • bansalpeeyush29bansalpeeyush29 IndiaCommunity Member Wizard ✭✭✭✭✭

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

  • PremPrem MumbaiCommunity Member Qubie ✭

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

  • bansalpeeyush29bansalpeeyush29 IndiaCommunity Member Wizard ✭✭✭✭✭

    Right, I thought it is for characters :)

  • Linda_charltonLinda_charlton Fargo NDCommunity Member Qubie ✭

    I was looking for limit to words not Characters.

  • Linda_charltonLinda_charlton Fargo NDCommunity Member Qubie ✭

    is there a way to limit the words and give them the count of words so they would know how many they have left.

  • Linda_charltonLinda_charlton Fargo NDCommunity Member Qubie ✭

    @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?

  • ShashiShashi IndiaCommunity Member Wizard ✭✭✭✭✭

    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();
        }
        }); 
    
  • Linda_charltonLinda_charlton Fargo NDCommunity Member Qubie ✭

    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

  • Linda_charltonLinda_charlton Fargo NDCommunity Member Qubie ✭

    @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("
    

    Words remaining: 0

    ").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.

  • ShashiShashi IndiaCommunity Member Wizard ✭✭✭✭✭

    @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

  • Linda_charltonLinda_charlton Fargo NDCommunity Member Qubie ✭

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

Sign In to Comment