I am using the RadEditor component. I am storing the content in a database text field with a max length of 4000 characters. I used a telerik javascript solution to limit the number of characters in the content to 4000 characters. This does not work because the database field needs to store not only the characters in the design or preview tab of the component but the HTML characters as well.
Anyway, I get the HTML from the editor.get_HTML(true), if I use the LENGTH (editor.get_HTML(True).Length). I get the number of characters that is previewed or in the design tab. I need the TOTAL number of characters in the HTML string including the HTML code such as <br />, <span>, etc.
editor.get_HTML(True).Length does not work is can not use a BLOB or CLOB.
Here is the javascript code.
<
script type="text/javascript">
var editorList= new Object();
var editorLengthArray = [4000,4000];
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_html().length;
if (textLength >= maxTextLength)
{
alert(
'You are not able to type more than ' + maxTextLength + ' symbols!');
e.returnValue =
false;
}
}
});
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()];
//this code below does not work for me. it returns the number of characters previewed
//not the total number of characters in the HTML string/
textLength = editor.get_html(true).length;<<<<<<<<<<<<<
var clipboardLength = window.clipboardData.getData("Text").length;
textLength += clipboardLength;
if (textLength >= maxTextLength)
{
alert(
'You reached the 4000 symbol limit for your content.');
e.returnValue =
false;
return false;
}
});
}
function CalculateLength(editor, value)
{
var textLength = editor.get_html(true).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);
alert(
'TL='+textLength);
if (textLength >= maxTextLength)
{
alert(
'You reached the 4000 symbol limit for your content.');
args.set_cancel(
true);
}
}
}
function OnClientSubmit(editor)
{
var textLength = editor.get_html().length;
if (textLength > 4000)
{
alert(
'You reached the 4000 symbol limit. Your content will be truncated!');
}
}
</
script>