maxlength plugin in ckeditor does not work

Asked

Viewed 180 times

1

I need to limit the number of characters in the Ckeditor.

I followed the step by step of this topic: Limiting text in CKEDITOR

But it didn’t work. Below the changes made to my code:

../ckeditor/config.js

plugin add no diretorio

   <textarea id="desc" name="desc" data-maxlen="10" rows="7" cols="45"</textarea>

The editor works, but the plugin does not.

ps. tried also add the folder "maxlenght" inside this plugin folder. Also it did not work.

Something wrong in its implementation?

Another question: how does this plugin work? it does not let pass the characters of the chosen amount? or it shows error after sending the form?

Ever since I thank.

  • Instead of data-maxlen="10", trial maxlength="10"

  • Neither of them work.

1 answer

1


You can use this code below. It will pick up the value in maxlength and limit the number of characters in the Ckeditor.

Textarea:

<textarea name="editor1" maxlength="10" id="desc" rows="10" cols="80"></textarea>

Code:

<script>
window.onload = function(){            
    CKEDITOR.instances.desc.on('key',function(event){
       // cada código na array representa uma tecla
       // Ex.: backspace, delete, uparrow etc..
       var keys = [46,8,38,40,37,39,35,36,16,18];
        var keyCode = event.data.keyCode;
        if(~keys.indexOf(keyCode)){
            return true;
        }else{
            var textLimit = document.querySelector("#desc").getAttribute("maxlength");
            var str = CKEDITOR.instances.desc.getData().replace(/<[^>]*>/g, "");
            if (str.length >= textLimit) return false;
        }
    });    
};

CKEDITOR.replace('desc');
</script>
  • I used exactly your code. I copied and pasted. It limits the characters, but the page stops recognizing the letter.

  • See that in line response CKEDITOR.replace('desc'); is just an example to start Ckeditor. If vc has already started somewhere, you don’t need to put this line.

  • I pulled the line, I got the same result.

  • I think the way will be to expand the field in the database to text and not limit... At least with the Ckeditor I already protect the input against invasions. (right?)

  • Look at the test I did working: http://dvdteste.hospedagemdesites.ws/ckeditor/teste.html

  • I’m using ckeditor 4

  • In the line ''CKEDITOR.instances.desc.on('key',Function(Event){" a parenthesis is being opened, and closed only at the end of the code. This is correct?

  • : no. parentese. That was the mistake. The code is like this: "CKEDITOR.instances.desc.on('key',Function(Event){". It should look like this: "CKEDITOR.instances.desc.on('key'),Function(Event){".

  • But it solved my problem. I really appreciate it

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.