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

Maximum Text Length

1 Answer 51 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 12 Nov 2008, 04:39 PM
Is it possible to change the maximum text length of the editor in javascript?

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 12 Nov 2008, 04:58 PM
Hi Jason,

You can use the following symbol counter and limiter:

<span id="counter" style="border: 1px solid red;text-align: right;">Characters left:</span>
<telerik:RadEditor ID="RadEditor" runat="server" OnClientLoad="OnClientLoad">
</telerik:RadEditor>

<script type="text/javascript">
var limitNum = 250;
var message = 'You are not able to type more than ' + limitNum + ' characters!';
var counter = $get('counter');

function LimitLength(mode)
{                  
    var rtfEditor = $find("<%=RadEditor.ClientID %>");
    if (mode == 2){
     var oValue = rtfEditor.get_textArea().innerHTML.trim().length;
     }
    else{var oValue = rtfEditor.get_html(true).trim();}
 
    if (oValue.length >= limitNum)
    {
        rtfEditor.set_html(oValue.substring(0, limitNum - 1));
        alert(message);
    }  
    counter.innerHTML = "Characters left: " + ( limitNum - oValue.length );
}

function AttachHandlers(mode)
{
                  
    var rtfEditor = $find("<%=RadEditor.ClientID %>");
    if(mode==1)
    {        
        rtfEditor.attachEventHandler("onkeyup", LimitLength);
        rtfEditor.attachEventHandler("onpaste", LimitLength);
        rtfEditor.attachEventHandler("onblur", LimitLength);
    }
    else
    {                              
        var textarea = rtfEditor.get_textArea();                                      

        if(window.attachEvent)
        {                              
            textarea.attachEvent("onkeydown", LimitLength);
            textarea.attachEvent("onpaste", LimitLength);
            textarea.attachEvent("onblur", LimitLength);
        }                            
        else
        {                      
           textarea.addEventListener("keyup",LimitLength, true);
           textarea.addEventListener("paste", LimitLength, true);
           textarea.addEventListener("blur", LimitLength, true);
        }
    }
}

function OnClientLoad(editor, args)
{                  
    rtfEditor = editor;                      
    AttachHandlers(1);
    LimitLength(1);
                                                  
 
    editor.add_modeChange(function(sender, args)
    {      
       var mode = sender.get_mode();                   
       if (mode == 1 || mode == 2)
       {                                     
          AttachHandlers(mode);
          LimitLength(mode);
       }
  });
}
</script>

Feel free to enhance the code to better fit your scenario.

Best regards,
Rumen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Editor
Asked by
Jason
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Share this question
or