Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ckeditor character limitation with charcount plugin

How can i prevent users to enter new characters after max character limit is reached ?

Ckeditor charcount plugin just shows me the remaining characters, i want it to stop at 0. But it goes minus integers.

Here's my html code.

<textarea id="myTextEditor1" name="myTextEditor"></textarea>
<script type="text/javascript">
        CKEDITOR.replace('myTextEditor1', { 
            height: 200, 
            extraPlugins: 'charcount', 
            maxLength: 10, 
            toolbar: 'TinyBare', 
            toolbar_TinyBare: [
                 ['Bold','Italic','Underline'],
                 ['Undo','Redo'],['Cut','Copy','Paste'],
                 ['NumberedList','BulletedList','Table'],['CharCount']
            ] 
        });
</script>

Do i have to use onChange plugin ? If i have to how can i limit users entering new characters ?

like image 457
saimcan Avatar asked Dec 25 '22 01:12

saimcan


1 Answers

I used the ckeditor jQuery adapter for this.

<textarea id="myTextEditor1" name="myTextEditor"></textarea>
<script type="text/javascript">
    $(function () {
        var myEditor = $('#myTextEditor1');
        myEditor.ckeditor({ 
        height: 200, 
        extraPlugins: 'charcount', 
        maxLength: 10, 
        toolbar: 'TinyBare', 
        toolbar_TinyBare: [
             ['Bold','Italic','Underline'],
             ['Undo','Redo'],['Cut','Copy','Paste'],
             ['NumberedList','BulletedList','Table'],['CharCount']
        ] 
        }).ckeditor().editor.on('key', function(obj) {
            if (obj.data.keyCode === 8 || obj.data.keyCode === 46) {
                return true;
            }
            if (myEditor.ckeditor().editor.document.getBody().getText().length >= 10) {
                alert('No more characters possible');
                return false;
            }else { return true; }

        });
    });
</script>

The keyCode check is to allow backspace and delete key presses. To use the jQuery adapter, don't forget to insert it:

<script src="/path-to/ckeditor/adapters/jquery.js"></script>
like image 191
jazZRo Avatar answered Dec 30 '22 09:12

jazZRo