Limit the number of character in tinyMCE

后端 未结 16 1129
心在旅途
心在旅途 2020-11-29 07:44

Im using tinyMCe for my project.Everything is working fine but now i want to restrict the number of character that will be insert into tinyMce text

16条回答
  •  北海茫月
    2020-11-29 07:58

    If you are here maybe you are looking for simple solution. Here is my solution. It is not perfect, but it is very simple

    var max_length = 3;
        tinymce.init({
            selector: '#description',
            // some my settings for tiny mce
            toolbar: ' undo redo | bold italic | formatselect',
            setup : function(ed) {
               // important part
               ed.on("keypress", function(event){
                    // get content of the tinymce and remove tags
                    // tinymce will be adding tags while you type in it.
                    // when tags are removed, you will heave real input length (the one that customer sees)
                    var content =  tinymce.activeEditor.getContent().replace(/(<([^>]+)>)/ig,"");
                    // now just compare that length to your prefered length.
                    // if it is larger or same, return false, and that will disregard last input.
                    if(content.length >= max_length){
                        return false;
                    }
                });
            }
    
        });
    

提交回复
热议问题