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

    Problem with javascript which counts the words

    Van-Helsing
    Van-Helsing
    Hyperactive


    Male Posts : 2431
    Reputation : 116
    Language : English, Greek

    Solved Problem with javascript which counts the words

    Post by Van-Helsing November 7th 2014, 12:00 pm

    Hello all,
    I am using the following javascript to count the words in quick reply but it isn't recognizes the greek characters can you help me to fix it? It recognizes only the english characters.
    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'> Ελάχιστος αριθμός λέξεων για να σταλεί το μήνυμα: " + MinNumberOfWords + " (τουλάχιστον " + LetterCountPerWord + " γράμματα)</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();
                    str_arr = str.match(regex);
                    str_len = str_arr == null ? 0 : str_arr.length;
                    if(str_len >= MinNumberOfWords) {
                       $("#div_minchars_info").html("Έχετε γράψει <span style='color:green'>" + str_len + "</span> λέξη/εις. Τώρα μπορείτε να αποστείλετε το θέμα/απάντηση σας <img src='" + ico_compl  + "' />"); 
                   $("input[type='submit'][name='post']").attr("disabled", false).css("opacity", "1");
                    }else{
                       $("#div_minchars_info").html("Έχετε γράψει <span style='color:red'>" + str_len + "</span> λέξη/εις. Χρειάζεστε ακόμα " + (MinNumberOfWords - str_len) + " λέξη/εις για να αποστείλετε το θέμα/απάντηση σας <img src='" + ico_incompl  + "' />");
                       $("input[type='submit'][name='post']").attr("disabled", true).css("opacity", "0.5");
                    }
                 });
              }
           })});


    Last edited by Black-Shadow on November 7th 2014, 7:15 pm; edited 1 time in total
    Ange Tuteur
    Ange Tuteur
    Forumaster


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

    Solved Re: Problem with javascript which counts the words

    Post by Ange Tuteur November 7th 2014, 4:47 pm

    Hello Black-Shadow,

    Try replacing the script by :
    Code:
    $(function(){$(function(){

     Â          var MinNumberOfChars = 50;
     Â          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 characters to send a message: " + MinNumberOfChars + " </div><div id='div_minchars_info'></div></div>");
     Â             
     Â             var sceditor = $("#text_editor_textarea").sceditor("instance");                  
     Â             var str = sceditor.val();
     Â             var str_len = str.length;
     Â             
     Â             if(str_len >= MinNumberOfChars) $("input[type='submit'][name='post']").attr("disabled", false).css("opacity", "1");
     Â             
     Â             sceditor.keyUp(function(e) {
     Â                str = sceditor.val();
     Â                str_len = str.length;
     Â                if(str_len >= MinNumberOfChars) {
     Â                   $("#div_minchars_info").html("You've written <span style='color:green'>" + str_len + "</span> character(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> character(s). You need " + (MinNumberOfChars - str_len) + " character(s) to send this message <img src='" + ico_incompl  + "' />");
     Â                   $("input[type='submit'][name='post']").attr("disabled", true).css("opacity", "0.5");
     Â                }
     Â             });
     Â          }
     Â       })});
    Van-Helsing
    Van-Helsing
    Hyperactive


    Male Posts : 2431
    Reputation : 116
    Language : English, Greek

    Solved Re: Problem with javascript which counts the words

    Post by Van-Helsing November 7th 2014, 5:12 pm

    Hello Ange,
    This script is counting the characters but working with greek characters and recognize them, but I need to count the words. Is there a way to fix it?
    Ange Tuteur
    Ange Tuteur
    Forumaster


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

    Solved Re: Problem with javascript which counts the words

    Post by Ange Tuteur November 7th 2014, 5:47 pm

    You can try this, but I'm not sure if it'll work :
    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'> Ελάχιστος αριθμός λέξεων για να σταλεί το μήνυμα: " + MinNumberOfWords + " (τουλάχιστον " + LetterCountPerWord + " γράμματα)</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();
                    str_arr = str.match(regex);
                    str_len = str_arr == null ? 0 : str_arr.length;
                    if(str_len >= MinNumberOfWords) {
                      $("#div_minchars_info").html("Έχετε γράψει <span style='color:green'>" + str_len + "</span> λέξη/εις. Τώρα μπορείτε να αποστείλετε το θέμα/απάντηση σας <img src='" + ico_compl  + "' />");
                  $("input[type='submit'][name='post']").attr("disabled", false).css("opacity", "1");
                    }else{
                      $("#div_minchars_info").html("Έχετε γράψει <span style='color:red'>" + str_len + "</span> λέξη/εις. Χρειάζεστε ακόμα " + (MinNumberOfWords - str_len) + " λέξη/εις για να αποστείλετε το θέμα/απάντηση σας <img src='" + ico_incompl  + "' />");
                      $("input[type='submit'][name='post']").attr("disabled", true).css("opacity", "0.5");
                    }
                });
              }
          })});
    Van-Helsing
    Van-Helsing
    Hyperactive


    Male Posts : 2431
    Reputation : 116
    Language : English, Greek

    Solved Re: Problem with javascript which counts the words

    Post by Van-Helsing November 7th 2014, 5:53 pm

    No the quantifier is not working http://prntscr.com/5433ox is there another quantifier except /w and /W?
    Ange Tuteur
    Ange Tuteur
    Forumaster


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

    Solved Re: Problem with javascript which counts the words

    Post by Ange Tuteur November 7th 2014, 7:02 pm

    Okay, see if this works :
    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'> Ελάχιστος αριθμός λέξεων για να σταλεί το μήνυμα: " + MinNumberOfWords + " (τουλάχιστον " + LetterCountPerWord + " γράμματα)</div><div id='div_minchars_info'></div></div>");
     Â         
     Â           var sceditor = $("#text_editor_textarea").sceditor("instance");              
     Â           var str = sceditor.val();
     Â           var regex = new RegExp('[a-z\u0370-\u03FF]{' + LetterCountPerWord + ',}[a-z\u0370-\u03FF]', '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();
     Â               str_arr = str.match(regex);
     Â               str_len = str_arr == null ? 0 : str_arr.length;
     Â               if(str_len >= MinNumberOfWords) {
     Â                 $("#div_minchars_info").html("Έχετε γράψει <span style='color:green'>" + str_len + "</span> λέξη/εις. Τώρα μπορείτε να αποστείλετε το θέμα/απάντηση σας <img src='" + ico_compl  + "' />");
     Â             $("input[type='submit'][name='post']").attr("disabled", false).css("opacity", "1");
     Â               }else{
     Â                 $("#div_minchars_info").html("Έχετε γράψει <span style='color:red'>" + str_len + "</span> λέξη/εις. Χρειάζεστε ακόμα " + (MinNumberOfWords - str_len) + " λέξη/εις για να αποστείλετε το θέμα/απάντηση σας <img src='" + ico_incompl  + "' />");
     Â                 $("input[type='submit'][name='post']").attr("disabled", true).css("opacity", "0.5");
     Â               }
     Â           });
     Â         }
    })});

    I added the unicode character codes to the regex.
    Van-Helsing
    Van-Helsing
    Hyperactive


    Male Posts : 2431
    Reputation : 116
    Language : English, Greek

    Solved Re: Problem with javascript which counts the words

    Post by Van-Helsing November 7th 2014, 7:14 pm

    Yes now its working both greek and english characters sets.
    Thank you very much Ange. The problem is solved http://prntscr.com/543z72 .
    ~1~
    Ange Tuteur
    Ange Tuteur
    Forumaster


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

    Solved Re: Problem with javascript which counts the words

    Post by Ange Tuteur November 7th 2014, 7:33 pm

    You're welcome ^^

    Topic archived

      Current date/time is November 11th 2024, 1:59 pm