<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>


<telerik:RadEditor ID="RadEditor2" runat="server" OnClientLoad="LimitCharacters" OnClientPasteHtml="OnClientPasteHtml">
<Content></Content></telerik:RadEditor>
<telerik:RadEditor ID="RadEditor3" runat="server" OnClientLoad="LimitCharacters" OnClientPasteHtml="OnClientPasteHtml">
<Content></Content></telerik:RadEditor> 
 <div id="counter"></div>
<script type="text/javascript">
    var editorList = new Object();
    var editorLengthArray = [20, 30];
    var counter = 0;

    function isAlphaNumericKey(keyCode)
    {
        if ((keyCode > 47 && keyCode < 58) || (keyCode > 64 && keyCode < 91))
        {
            return true;
        }
        return false;
    }

    function LimitCharacters(editor)
    {
        editorList[editor.get_id()] = editorLengthArray[counter];
        counter++;
        editor.attachEventHandler("onkeydown", function (e)
        {
            e = (e == null) ? window.event : e;
            if (isAlphaNumericKey(e.keyCode))
            {
                var maxTextLength = editorList[editor.get_id()];
                textLength = editor.get_text().length;
                if (textLength >= maxTextLength)
                {
                    if ($telerik.isIE)
                    {
                        alert('You are not able to type more than ' + maxTextLength + ' symbols!');
                    }
                    $telerik.cancelRawEvent(e);
                }
            }
        });
        var element = document.all ? editor.get_document().body : editor.get_document();
        $telerik.addExternalHandler(element, "paste", function (e)
        {
            e = (e == null) ? window.event : e;

            var maxTextLength = editorList[editor.get_id()];
            textLength = editor.get_text().length;

            var clipboardLength = window.clipboardData.getData("Text").length;
            textLength += clipboardLength;
            if (textLength >= maxTextLength)
            {
                alert('You are not able to type more than ' + maxTextLength + ' symbols!');
                e.returnValue = false;
                return false;

            }
        });
    }

    function CalculateLength(editor, value)
    {
        var textLength = editor.get_text().length;
        var clipboardLength = value.length;
        textLength += clipboardLength;
        return textLength;
    }

    function OnClientPasteHtml(editor, args)
    {
        var maxTextLength = editorList[editor.get_id()];
        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 >= maxTextLength)
            {
                alert('You are not able to type more than ' + maxTextLength + ' symbols!');
                args.set_cancel(true);

            }
        }
    }    
 </script>


    </div>
    </form>
</body>
</html>
