Getting the word count at the server side

6 posts, 0 answers
  1. Yaniv
    Yaniv avatar
    9 posts
    Member since:
    Mar 2008

    Posted 05 Sep 2008 Link to this post

    How can I get the word count at the server side?

    If there is no way of getting it at the server side then how can I do it at the client side ?

    thanks,
    yaniv
  2. Rumen
    Admin
    Rumen avatar
    13849 posts

    Posted 08 Sep 2008 Link to this post

    Hi Yaniv,

    Please, review the following KB article on the subject: Implementing custom Word / Character counter.

    Kind regards,
    Rumen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Yaniv
    Yaniv avatar
    9 posts
    Member since:
    Mar 2008

    Posted 08 Sep 2008 Link to this post

    Hi Rumen,

    In this artical there are the following posts:

    " steve malkin, 12/20/2007
    This is useful for displaying the word count on the page, but how would I write the wordcount to a hidden field value rather than a span so that I could save the wordcount value to a database when the page is saved ?

    Telerik Admin, 1/17/2008
    Hi Steve, Could you please open a support ticket and I will send you the requested solution? In order to open a support ticket you should be a customer or to have downloaded some product.

    Best regards,
    Rumen
    the Telerik team
    "

    I am asking the same question.
    Don't you have a way to retrieve the editor's built in word count ?

    Thanks,
    yaniv

  4. Rumen
    Admin
    Rumen avatar
    13849 posts

    Posted 09 Sep 2008 Link to this post

    Hi Yaniv,

    Here is the requested example:

    Default.aspx
    <script type="text/javascript">
    function OnClientLoad(editor)
    {
        editor.add_submit(function ()
        {
            var content = editor.get_text(); //returns the editor's content as plain text
            var words = 0;
            if (content)
            {       
                punctRegX = /[!\.?;,:&_\-\-\{\}\[\]\(\)~#'"]/g;
                contentcontent = content.replace(punctRegX, "");
                trimRegX = /(^\s+)|(\s+$)/g;
                contentcontent = content.replace(trimRegX, "");
                if (content)
                {
                    splitRegX = /\s+/;
                    var array = content.split(splitRegX);
                    words = array.length;
                }
            }
          
            var hiddenField = document.getElementById("<%=HiddenField1.ClientID %>"); //get a reference to the HiddenField control
            hiddenField.value = "Words: " + words + " Characters: " + content.length;     
        });
    }
    </script>

    <asp:HiddenField ID="HiddenField1" runat="server" />
    <telerik:radeditor runat="server" ID="RadEditor1" OnClientLoad="OnClientLoad"></telerik:radeditor>
    <asp:Button ID="Button1" runat="server" Text="Button" />
      
    Default.aspx.cs:
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(HiddenField1.Value);
        }

    Another approach is to obtain the content via the RadEditor1.Content server property and using regular expression to count the words directly on the server.

    Best regards,
    Rumen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Kevin
    Kevin avatar
    2 posts
    Member since:
    Mar 2011

    Posted 08 Mar 2011 Link to this post

    The regular expression /\s+/ can't seem to recognize line breaks properly. For example, the below text

    the quick brown fox

    gives a word count of 4. When I insert multiple line breaks anywhere in the middle, like so

    the quick


    brown fox

    I now get a word count of 11! That's 2 words for each line break.

    The online demo, using default behavior, can correctly recognize the line breaks and returns the appropriate word count. Is there a way to emulate this result while using custom statistics? Thanks!

  6. Rumen
    Admin
    Rumen avatar
    13849 posts

    Posted 10 Mar 2011 Link to this post

    Hi Kevin,

    Unfortunately, I was unable to reproduce this problem. Could you please see my test in the following video http://screencast.com/t/8IlDR1LlHb6Y and let me know what I am missing?

    Best regards,
    Rumen
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top