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

Finding the length of an HTML string

1 Answer 179 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Steve909
Top achievements
Rank 2
Steve909 asked on 18 Dec 2009, 09:33 PM

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>

 

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 21 Dec 2009, 10:15 AM
Hi Stephen,

RadEditor does not offer a method named get_HTML(True). You should use editor.get_html(true).length to get the length of the HTML content.

In addition you should also strip the \r\n symbols that are added for readibility:

RadEditor1.Content = RadEditor1.Content.Replace("\r", "").Replace("\n", "");

You can find more information in this KB article: "\r\n" being added to content when submitted.

In the Q3 2009 build of RadEditor for ASP.NET AJAX we introduced two new properties MaxTextLenght and MaxHTMLLength to limit content size. You can use the MaxTextLenght property in your scenario.

The MaxTextLength and MaxHtmlLength properties check the character length only when submitting the content. We decided to implement them in this way because if we check the content length on paste and / or on onkeydown then this will decrease performance and will slow down the typing / editing of large content. This is how these properties are implemented in our competitors' web editors as well.

Best wishes,
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.
Tags
Editor
Asked by
Steve909
Top achievements
Rank 2
Answers by
Rumen
Telerik team
Share this question
or