Radeditor onkeyup char count custom module's count != code behinds length count

4 posts, 1 answers
  1. TippCityTom
    TippCityTom avatar
    14 posts
    Member since:
    Sep 2008

    Posted 08 Sep 2009 Link to this post

    The following is text I entered in RadEditor box:

    This is a test message, but the count is not coming out quite right.  What shall I do, this text is not pasted, but manually entered.  Does that make a difference?

    Characters: 157

    but in code behind the length of text shows 163

    See code below:


    <

     

    script type="text/javascript">

     

    MyModule =

    function(element)

     

    {

    MyModule.initializeBase(

    this, [element]);

     

    }

    MyModule.prototype =

    {

    initialize :

    function()

     

    {

    MyModule.callBaseMethod(

    this, 'initialize');

     

     

    var selfPointer = this;

     

     

    this.get_editor().add_selectionChange(function (){ selfPointer.CountCharAction(); });

     

     

    this.get_editor().attachEventHandler("onkeyup", function(){ selfPointer.CountCharAction(); });

     

     

    this.CountCharAction();

     

    },

     

    //A method that does the actual work - it is usually attached to the "selection changed" editor event

     

    CountCharAction :

    function()

     

    {

     

     

    var content = this.get_editor().get_html(true);

     

     

    var chars = 0;

     

     

    if (content)

     

    {

        punctRegX = /[!\.?;,:&_\-\-\{\}\[\]\(\)~#

    '"]/g;

     

        content = content.replace(punctRegX,

    "");

     

        trimRegX = /(^\s+)|(\s+$)/g;

        content = content.replace(trimRegX,

    "");

     

     

        if (content)

     

        {

            splitRegX = /\s+/;

     

            var array = content.split(splitRegX);

     

            chars = content.length;

     

        }

    }

     

    var element = this.get_element();

     

    element.innerHTML = "<span style='line-height:22px; float:left;'>" + " Characters: " + chars + "&nbsp;</span>"
    element.style.border = "1px solid red";

     

    element.style.backgroundColor =

    "transparent";

     

    element.style.color =

    "red";

     

    }

    };

    MyModule.registerClass(

    'MyModule', Telerik.Web.UI.Editor.Modules.ModuleBase);

     

     

    </script>


     

    <

     

    telerik:RadEditor ID="tbBody" runat="server" Skin="Web20" Width="500px" Height="400px"

     

     

     

     

     

    EditModes="Design">

     

     

     

     

     

    <Modules>

     

     

     

     

     

    <telerik:EditorModule Name="RadEditorStatistics" Enabled="true" Visible="false" />

     

     

     

     

     

    <telerik:EditorModule Name="MyModule" Enabled="true" Visible="true" />

     

     

     

     

     

    </Modules>

     

     

     

     

     

    <Tools>

     

     

     

     

     

    <telerik:EditorToolGroup>

     

     

     

     

     

    <telerik:EditorTool Name="Cut" />

     

     

     

     

     

    <telerik:EditorTool Name="Copy" />

     

     

     

     

     

    <telerik:EditorTool Name="Paste" />

     

     

     

     

     

    <telerik:EditorSeparator />

     

     

     

     

     

    <telerik:EditorTool Name="JustifyLeft" />

     

     

     

     

     

    <telerik:EditorTool Name="JustifyCenter" />

     

     

     

     

     

    <telerik:EditorTool Name="JustifyRight" />

     

     

     

     

     

    </telerik:EditorToolGroup>

     

     

     

     

     

    </Tools>

     

     

     

     

     

    </telerik:RadEditor>

     

     

     



    Code behind (shows count of 163):

     

    string

     

    body = tbBody.Content.Trim();

     

     

    body = utils.stripHTML(body).Trim();

     

     

     

     

    if

     

    (body.Length > 1250)

     

    {

    errorMessage(

    string.Format("Error: Comments can not exceed 1250 characters!"));

     

     

    return;

     

    }

  2. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 08 Sep 2009 Link to this post

    Hi Thomas,

    I guess the difference results from the fact that RadEditor adds a linefeed when you store the content.
    If the difference is always that 6 chars I guess it is this behaviour.

    Another thing - on top of the page (here to post something) is an Icon "Format Codeblock" (right seconde next to spell check).
    Using this guy makes the things much more readable.

    Regards
    Manfred
    PS: if this post was helpful please use "Mark as answer"
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. TippCityTom
    TippCityTom avatar
    14 posts
    Member since:
    Sep 2008

    Posted 09 Sep 2009 Link to this post

    Thanks, is there any way to sink the numbers up?  I want to limit characters to 2500 but the box may display 2404 and in code behind might actually be 2506 and be over limit and cause error and user may be confused.  Any way to count the linefeeds? 
  5. Answer
    ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 09 Sep 2009 Link to this post

    Hi,

    one idea (saves space in the DB by the way) - simply change your string before entering it to the database.
    What I mean is replace \n with nothing.
    RadEditor adds these tags to give better readability in HTML view - but for HTML representation you don't need them.
    I do something like this in a "instead of insert" trigger on my DB for some values.

    Regards

    Manfred
    PS: if this post was helpful please use "Mark as answer"
Back to Top