This is a migrated thread and some comments may be shown as answers.

Getting the word count at the server side

5 Answers 170 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Yaniv
Top achievements
Rank 1
Yaniv asked on 05 Sep 2008, 10:13 PM
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

5 Answers, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 08 Sep 2008, 09:39 AM
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.
0
Yaniv
Top achievements
Rank 1
answered on 08 Sep 2008, 05:12 PM
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

0
Rumen
Telerik team
answered on 09 Sep 2008, 09:00 AM
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.
0
Kevin
Top achievements
Rank 1
answered on 08 Mar 2011, 01:02 PM
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!

0
Rumen
Telerik team
answered on 10 Mar 2011, 03:28 PM
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!
Tags
Editor
Asked by
Yaniv
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Yaniv
Top achievements
Rank 1
Kevin
Top achievements
Rank 1
Share this question
or