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

Max Char limit Javascript problem

3 Answers 121 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 2
Richard asked on 04 Sep 2009, 04:16 PM
I found Javascript from the search site function which enables me to check the number of characters when I paste.
Unfotunately, it doesn't work!

The javascript is below, and the ClientExecutedCommand function is fired on "OnClientCommandExecuted":
<script type="text/javascript">  
 function CalculateLength(editor, value) {  
            var textLength = editor.get_text().length;  
            var clipboardLength = value.length;  
            textLength += clipboardLength;  
            return textLength;  
        }  
 
 
        function ClientExecuteCommand(editor, args) {  
            var commandName = args.get_commandName();  
            var value = args.get_value();  
            if (commandName == "PasteFromWord"  
            || commandName == "PasteFromWordNoFontsNoSizes"  
            || commandName == "PastePlainText"  
            || commandName == "PasteAsHtml"  
            || commandName == "Paste") {  
                var textLength = CalculateLength(editor, value);  
                if (textLength >= 10) {  
                    alert("Max char. reached");  
                    args.set_cancel(true);  
 
                }  
            }  
        }     
</script> 

The HTML:
                    <telerik:RadEditor ID="RadEditor1" runat="server" OnClientCommandExecuted="ClientExecuteCommand">  
                        <Content> 
                        </Content> 
                    </telerik:RadEditor> 

The problem is that line 12 args.get_value() of the Javascript returns null although I have contents in my clipboard and I can see that when I paste in to Notepad there is something there.

Does anyone have any ideas?

Thanks
R

3 Answers, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 08 Sep 2009, 02:55 PM
Hello Richard,

You should attach the ClientExecuteCommand to the OnClientPasteHtml event but not to the OnClientCommandExecuted event and this will fix the problem:

<script type="text/javascript">   
function CalculateLength(editor, value) {   
    var textLength = editor.get_text().length;   
    var clipboardLength = value.length;   
    textLength += clipboardLength;   
    return textLength;   
}   
 
 
function ClientExecuteCommand(editor, args) {   
    var commandName = args.get_commandName();   
    var value = args.get_value();   
    if (commandName == "PasteFromWord"   
    || commandName == "PasteFromWordNoFontsNoSizes"   
    || commandName == "PastePlainText"   
    || commandName == "PasteAsHtml"   
    || commandName == "Paste") {   
        var textLength = CalculateLength(editor, value);   
        if (textLength >= 10) {   
            alert("Max char. reached");   
            args.set_cancel(true);   
        }   
    }   
}      
</script>  
<telerik:RadEditor ID="RadEditor1" runat="server" OnClientPasteHtml="ClientExecuteCommand">   
                <Content>  
                </Content>  
            </telerik:RadEditor>  

You can find more information in this forum thread: RadEditor - limit characters entered (issue with right click - paste command).

Best regards,
Rumen
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Mathias
Top achievements
Rank 1
answered on 02 Oct 2010, 04:55 AM
Hi! this code is awesome! BUT... it doesn't subtracts character when is erased.... how i do that?

Thanks!
0
Rumen
Telerik team
answered on 04 Oct 2010, 04:28 PM
Hi Mathias,

You can attach to the onkeydown event and check the content length as shown in this forum thread: RadEditor - limit characters entered (issue with right click - paste command).


Kind regards,
Rumen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Editor
Asked by
Richard
Top achievements
Rank 2
Answers by
Rumen
Telerik team
Mathias
Top achievements
Rank 1
Share this question
or