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

RadEditor setText using javascript

1 Answer 172 Views
Editor
This is a migrated thread and some comments may be shown as answers.
reynald lawrence
Top achievements
Rank 1
reynald lawrence asked on 26 Oct 2009, 07:23 AM
Hi there!

I have a RadEditor which have cientside validation of 4000 characters, my problem is the alert box message prompts when the characters exceeds 4000, but I can't set or change the text editor to maintain only its 4000 characters...

It seems that the code line > rtfEditor.set_text(oValue.substring(0, 30)); doesn't work.

Many Thanks!

Reynald


Here's the markup:

<

 

telerik:RadEditor ID="EdtDesc" runat="server" SkinID="radEditor1Skin" OnClientLoad="LimitCharacters"

 

 

OnClientPasteHtml="OnClientPasteHtml">

 

 

</telerik:RadEditor>

 


 

<script type="text/javascript">

 

 

var editorList = new Object();

 

 

var editorLengthArray = [30, 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().trim().length;

 

if (textLength >= maxTextLength) {

 

alert(

'You are not able to type more than ' + maxTextLength + ' symbols!');

 

 

var rtfeditor = $find("<%=EdtDesc.ClientID %>");

 

 

var oValue = rtfeditor.get_text().trim();

 

rtfEditor.set_text(oValue.substring(0, 30));

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()];

 

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>

 


1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 28 Oct 2009, 03:32 PM
Hi Reynald,

RadEditor does not offer a method named set_text. To set the content in the editor you should use the editor's set_html() method.

Best regards,
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
reynald lawrence
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Share this question
or