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

[Solved] RadEditor setText in javascript

3 Answers 327 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:26 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.
Hope I can find solution for this one.

Many Thanks!

Reynald


Here's my 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>


3 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 27 Oct 2009, 08:42 PM
I haven't had much success with set_text() and I always just use set_html and get_html instead. Perhaps they can provide an alternative for you?
0
Accepted
Lini
Telerik team
answered on 28 Oct 2009, 08:29 AM
Hello Reynald,

There is no setter for the text property - it is read only - editor.get_text(). Using set_html() is OK but you need to remember that if you do a get_text and then use set_html(), you will lose all your formatting. I don't think there is a reliable way to trim down the editor text content to a specific length - you will need to rely on the user doing that after you display the alert message.

Best wishes,
Lini
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.
0
reynald lawrence
Top achievements
Rank 1
answered on 28 Oct 2009, 02:42 PM
Hi Lini,

Thanks for the information when using set_html() as other option for text setter. I think I'll just use custom validator for validation.

Regards,

Reynald
Tags
Editor
Asked by
reynald lawrence
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Lini
Telerik team
reynald lawrence
Top achievements
Rank 1
Share this question
or