I've taken the previous script examples and am trying to limit character content for multiple RadEditor controls on a single page (...see original thread, "Charector limit in Rad:Editor", posted 27 Oct 2008).
I've included my current version of the script below. Everything works, except when adding content to the editor using a simple "right click paste" command.
Typing beyond the specified character limits is handled correctly, as well as using the commands "Paste from Word", "Paste Plain Text", and "Paste As Html" when the [clipboard content + previous text entered] exceeds the specified character limits.
However, the paste command from a simple right click-paste or using the Edit/Paste function from the Menu... or ctl V does not result in the alert message that the limits have been exceeded.
Shown below is the code that I've been using.
Please let me know what I can do to resolve this.
Thanks in advance.
====================================================================================================
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="contentPage.aspx.vb" Inherits="contentPage" title="Untitled Page" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript" src="limitCharacters2.js"></script>
<telerik:RadEditor ID="RadEditorHeadline" runat="server" OnClientLoad="LimitCharacters" OnClientPasteHtml="OnClientPasteHtml">
<Content></Content></telerik:RadEditor>
<telerik:RadEditor ID="RadEditorPurpose" runat="server" OnClientLoad="LimitCharacters" NewLineBr="False" OnClientPasteHtml="OnClientPasteHtml">
<Content></Content></telerik:RadEditor>
</asp:Content>
//========================================================================================================
//limitCharacters2.js
//========================================================================================================
// JavaScript File to limit characters entered into Prometheus Rad Editor control
var editorList= new Object();
var editorLengthArray = [80,80,80];
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)
{
alert('You are not able to type more than ' + maxTextLength + ' symbols!');
e.returnValue = 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);
}
}
}
//======================================================================================================