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

[Solved] Check for the character limit during paste event

1 Answer 130 Views
Editor
This is a migrated thread and some comments may be shown as answers.
reethish
Top achievements
Rank 1
reethish asked on 18 Feb 2008, 02:13 PM
Hello All,
    I'm using Prometheus Rad Editor on my page. I set the rad editor to accept only the a limited number of html contents.
    I used the following code to accomplish this scenario.
var maxTextLength;
    var messageText;
    function CharactersLimit(editor)
    {
        maxTextLength = document.getElementById('<%= hdnColumn.ClientID %>').value == 'Notes'? 1000 : 2800;
        messageText = "You have reached the field limit. You cannot type further than " + maxTextLength + " characters.";
        editor.AttachEventHandler ("onkeydown", function (e)
        {
            e = (e == null)? window.event : e;
            textLength = editor.get_Html().length;
            if (textLength >= maxTextLength)
            {
                alert(messageText);
                e.returnValue = false;
                return false;
            }
        });
                    Telerik.Web.DomElement.addExternalHandler(editor.get_document().body, "beforepaste",
        function(e)
        {
            var textLength = CalculateLength(editor);
            if (textLength >= maxTextLength)
            {
                alert(messageText);
                e.cancelBubble = true;
                e.returnValue = false;
                return false;
            }
        });
    }
    function OnClientCommandExecuting(editor, commandName, oTool)
    {
        var CmdName = commandName.get_CommandName();
        if (CmdName == "PasteFromWord"
            || CmdName == "PasteFromWordNoFontsNoSizes"
            || CmdName == "PastePlainText"
            || CmdName == "PasteAsHtml"
            || CmdName == "Paste")
        {
            if (document.all)
            {
                var textLength = CalculateLength (editor);
                if (textLength >= maxTextLength)
                {
                    alert(messageText);
                    return false;
                }
            }
        }
    }
    //To calculate the length of the Editor text
    function CalculateLength(editor)
    {
        var textLength = editor.get_Html().length;
        var clipboardLength = window.clipboardData.getData("Text").length;
        textLength += clipboardLength;
        return textLength;
    }


    Its working fine during the keydown event but not during the paste event.
    I'm getting the alert message if the character limit has exceeded on both the events, but still my contents from the clipboard is getting pasted.

1 Answer, 1 is accepted

Sort by
0
George
Telerik team
answered on 20 Feb 2008, 11:13 AM
Hi Reethish,

You are experiencing this problem because you are not cancelling the paste command. You can easily cancel the command with the following property: set_cancel(true);
 
Here is your modified OnClientCommandExecuting function:
function OnClientCommandExecuting(editor, commandName, oTool)  
        {  
            var CmdName = commandName.get_CommandName();  
            if (CmdName == "PasteFromWord"   
                || CmdName == "PasteFromWordNoFontsNoSizes"   
                || CmdName == "PastePlainText"   
                || CmdName == "PasteAsHtml"   
                || CmdName == "Paste")  
            {  
                if (document.all)  
                {  
                    var textLength = CalculateLength (editor);  
                    if (textLength >= maxTextLength)  
                    {  
                        alert(messageText);  
                        commandName.set_cancel(true);    
                    }  
                }  
            }  
        } 


I hope this helps.

Best regards,
George
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Editor
Asked by
reethish
Top achievements
Rank 1
Answers by
George
Telerik team
Share this question
or