The forum of the forums

Would you like to react to this message? Create an account in a few clicks or log in to continue.
The forum of the forums
2 posters

    Editor Word Counter

    avatar
    inallsorts
    Forumember


    Posts : 246
    Reputation : 8
    Language : English

    Solved Editor Word Counter

    Post by inallsorts July 4th 2014, 11:25 am

    Greetings everyone,

    I've applied the Word Counter as per this tutorial here on Forumotion: https://help.forumotion.com/t131191-add-a-word-counter-to-the-editor

    It works great, however when you use the "Quote" button to post reply (on a given member's post), It also takes Into account the "Quoted text" (from the given member), hence the "minimum words to send" has already been exceeded (from the quoted text) without even posting anything.

    Is there a way so the Word Counter does not take the quoted text Into account when quoting a post? 

    Thanks In advance for all help given.


    Last edited by inallsorts on July 5th 2014, 10:11 am; edited 1 time in total (Reason for editing : Topic marked as solved.)
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Editor Word Counter

    Post by Ange Tuteur July 4th 2014, 4:55 pm

    Hello inallsorts,

    We can try to filter out the quotes with a regular expression. Give this a try :
    Code:
      $(function(){$(function(){

     Â      var MinNumberOfWords = 5;
     Â      var LetterCountPerWord = 3;
     Â      var ico_compl = "http://i73.servimg.com/u/f73/18/10/89/49/ok-gre10.png";
     Â      var ico_incompl = "http://i73.servimg.com/u/f73/18/10/89/49/exclam10.png";  
     Â 
     Â    if($("#text_editor_textarea").length != 0){
     Â         $("input[type='submit'][name='post']").attr("disabled", true).css("opacity", "0.5");      
     Â         $(".sceditor-container").after("<br/><div id='div_minchars' align='center'><div id='div_minchars_header'> Minimum words to send a message: " + MinNumberOfWords + " (at least " + LetterCountPerWord + " letters)</div><div id='div_minchars_info'></div></div>");
     Â         
     Â         var sceditor = $("#text_editor_textarea").sceditor("instance");                  
     Â         var str = sceditor.val();
     Â         var regex = new RegExp('\\w{' + LetterCountPerWord + ',}\\b', 'g');
     Â         var str_arr = str.match(regex);
     Â         var str_len = str_arr == null ? 0 : str_arr.length;
     Â         
     Â         if(str_len >= MinNumberOfWords) $("input[type='submit'][name='post']").attr("disabled", false).css("opacity", "1");
     Â         
     Â         sceditor.keyUp(function(e) {
     Â            str = sceditor.val().replace(/\n/g,' ').replace(/\[quote=".*?"\].*?\[\/quote\]/gi,'').replace(/\[quote\].*?\[\/quote\]/gi,'').replace(/\[\/quote\]/gi,'').replace(/\[quote\]/gi,'').replace(/\[quote=".*?"\]/gi,'');
     Â            str_arr = str.match(regex);
     Â            str_len = str_arr == null ? 0 : str_arr.length;
     Â            if(str_len >= MinNumberOfWords) {
     Â               $("#div_minchars_info").html("You've written <span style='color:green'>" + str_len + "</span> word(s). You can now send the message <img src='" + ico_compl  + "' />");  
     Â           $("input[type='submit'][name='post']").attr("disabled", false).css("opacity", "1");
     Â            }else{
     Â               $("#div_minchars_info").html("You've written <span style='color:red'>" + str_len + "</span> word(s). You need " + (MinNumberOfWords - str_len) + " word(s) to send this message <img src='" + ico_incompl  + "' />");
     Â               $("input[type='submit'][name='post']").attr("disabled", true).css("opacity", "0.5");
     Â            }
     Â         });
     Â      }
     Â   })});
    avatar
    inallsorts
    Forumember


    Posts : 246
    Reputation : 8
    Language : English

    Solved Re: Editor Word Counter

    Post by inallsorts July 5th 2014, 10:08 am

    Ange Tuteur wrote:Hello inallsorts,

    We can try to filter out the quotes with a regular expression. Give this a try :
    Code:
      $(function(){$(function(){

     Â      var MinNumberOfWords = 5;
     Â      var LetterCountPerWord = 3;
     Â      var ico_compl = "http://i73.servimg.com/u/f73/18/10/89/49/ok-gre10.png";
     Â      var ico_incompl = "http://i73.servimg.com/u/f73/18/10/89/49/exclam10.png";  
     Â 
     Â    if($("#text_editor_textarea").length != 0){
     Â         $("input[type='submit'][name='post']").attr("disabled", true).css("opacity", "0.5");      
     Â         $(".sceditor-container").after("<br/><div id='div_minchars' align='center'><div id='div_minchars_header'> Minimum words to send a message: " + MinNumberOfWords + " (at least " + LetterCountPerWord + " letters)</div><div id='div_minchars_info'></div></div>");
     Â         
     Â         var sceditor = $("#text_editor_textarea").sceditor("instance");                  
     Â         var str = sceditor.val();
     Â         var regex = new RegExp('\\w{' + LetterCountPerWord + ',}\\b', 'g');
     Â         var str_arr = str.match(regex);
     Â         var str_len = str_arr == null ? 0 : str_arr.length;
     Â         
     Â         if(str_len >= MinNumberOfWords) $("input[type='submit'][name='post']").attr("disabled", false).css("opacity", "1");
     Â         
     Â         sceditor.keyUp(function(e) {
     Â            str = sceditor.val().replace(/\n/g,' ').replace(/\[quote=".*?"\].*?\[\/quote\]/gi,'').replace(/\[quote\].*?\[\/quote\]/gi,'').replace(/\[\/quote\]/gi,'').replace(/\[quote\]/gi,'').replace(/\[quote=".*?"\]/gi,'');
     Â            str_arr = str.match(regex);
     Â            str_len = str_arr == null ? 0 : str_arr.length;
     Â            if(str_len >= MinNumberOfWords) {
     Â               $("#div_minchars_info").html("You've written <span style='color:green'>" + str_len + "</span> word(s). You can now send the message <img src='" + ico_compl  + "' />");  
     Â           $("input[type='submit'][name='post']").attr("disabled", false).css("opacity", "1");
     Â            }else{
     Â               $("#div_minchars_info").html("You've written <span style='color:red'>" + str_len + "</span> word(s). You need " + (MinNumberOfWords - str_len) + " word(s) to send this message <img src='" + ico_incompl  + "' />");
     Â               $("input[type='submit'][name='post']").attr("disabled", true).css("opacity", "0.5");
     Â            }
     Â         });
     Â      }
     Â   })});
    \".*?"\\ wrote:

    You're absolutely brilliant! What would this board do without you.

    The code has worked nicely, thank you kindly once again.
    /Topic.
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Editor Word Counter

    Post by Ange Tuteur July 5th 2014, 5:50 pm

    No problem Very Happy

    Topic archived

    Have a good day. ^^

      Current date/time is September 22nd 2024, 9:38 am