最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TinyMCE text editor max char limit - Stack Overflow

programmeradmin5浏览0评论

I am using TinyMCE for <textarea>. My requirement is to limit the character size to 2000 and also to show the remaining characters somewhere below the tool bar. I somehow managed to get the characters number; now I am stuck with displaying the remaining characters and prevent from exceeding limit.

Here is my TinyMCE code

tinyMCE.init({
    // General options
    mode : "textareas",
    theme : "simple",
    plugins : "autolink,lists,pagebreak,style,table,save,advhr,advimage,
               advlink,emotions,media,noneditable,visualchars,nonbreaking,
               xhtmlxtras,template",

    // Theme options
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,
                               justifyleft,justifycenter,justifyright,
                               justifyfull,|,styleselect,formatselect,
                               fontselect,fontsizeselect", 
    theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,|,undo,redo,|,
                               link,unlink,anchor,image,code,|,forecolor,
                               backcolor",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,
    charLimit : 10, // this is a default value which can get modified later
    setup : function(ed) {
        //peform this action every time a key is pressed
        ed.onKeyUp.add(function(ed, e) {
        //define local variables
        var tinymax, tinylen, htmlcount;
        //manually setting our max character limit
        tinymax = ed.settings.charLimit;
        //grabbing the length of the curent editors content
        tinylen = ed.getContent().replace(/(<([^>]+)>)/ig,"").length;
        //setting up the text string that will display in the path area
        htmlcount = "HTML Character Count: " + tinylen + "/" + tinymax;
        //if the user has exceeded the max turn the path bar red.
        if (tinylen>tinymax){


        } 
        });
    }
});

For testing purpose I am trying to limit up to 10 char.
Any suggestions are welcome.

I am using TinyMCE for <textarea>. My requirement is to limit the character size to 2000 and also to show the remaining characters somewhere below the tool bar. I somehow managed to get the characters number; now I am stuck with displaying the remaining characters and prevent from exceeding limit.

Here is my TinyMCE code

tinyMCE.init({
    // General options
    mode : "textareas",
    theme : "simple",
    plugins : "autolink,lists,pagebreak,style,table,save,advhr,advimage,
               advlink,emotions,media,noneditable,visualchars,nonbreaking,
               xhtmlxtras,template",

    // Theme options
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,
                               justifyleft,justifycenter,justifyright,
                               justifyfull,|,styleselect,formatselect,
                               fontselect,fontsizeselect", 
    theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,|,undo,redo,|,
                               link,unlink,anchor,image,code,|,forecolor,
                               backcolor",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,
    charLimit : 10, // this is a default value which can get modified later
    setup : function(ed) {
        //peform this action every time a key is pressed
        ed.onKeyUp.add(function(ed, e) {
        //define local variables
        var tinymax, tinylen, htmlcount;
        //manually setting our max character limit
        tinymax = ed.settings.charLimit;
        //grabbing the length of the curent editors content
        tinylen = ed.getContent().replace(/(<([^>]+)>)/ig,"").length;
        //setting up the text string that will display in the path area
        htmlcount = "HTML Character Count: " + tinylen + "/" + tinymax;
        //if the user has exceeded the max turn the path bar red.
        if (tinylen>tinymax){


        } 
        });
    }
});

For testing purpose I am trying to limit up to 10 char.
Any suggestions are welcome.

Share Improve this question edited Jan 14, 2016 at 14:49 naXa stands with Ukraine 37.9k25 gold badges204 silver badges272 bronze badges asked Aug 3, 2012 at 6:33 Prathamesh mhatrePrathamesh mhatre 1,0855 gold badges17 silver badges32 bronze badges 3
  • might be helpful: stackoverflow.com/questions/5533053/textarea-character-limit I haven't use tinymce before but redactor uses a textarea so I assume that tinymce is also using a textarea. – Wern Ancheta Commented Aug 3, 2012 at 6:37
  • Thanks for your reply but normal limiting of textarea wont work with tincymce. – Prathamesh mhatre Commented Aug 3, 2012 at 6:42
  • Possible duplicate of Limit the number of character in tinyMCE – naXa stands with Ukraine Commented Dec 8, 2015 at 13:40
Add a comment  | 

3 Answers 3

Reset to default 9

I suggest you execute your code onKeyDown, because on KeyUp the letter is already in the editor.

    //peform this action every time a key is pressed
    ed.onKeyDown.add(function(ed, e) {

      //define local variables
      var tinymax, tinylen, htmlcount;

      //manually setting our max character limit
      tinymax = ed.settings.charLimit;

      //grabbing the length of the curent editors content
      tinylen = ed.getContent().replace(/(<([^>]+)>)/ig,"").length;

      //setting up the text string that will display in the path area
      htmlcount = "HTML Character Count: " + tinylen + "/" + tinymax;

      //if the user has exceeded the max turn the path bar red.
      if (tinylen > tinymax){

        // place text string in path bar
        if ( $('#max_char_string').size() ){
          $('#max_char_string').html( '&nbsp;' + htmlcount);
        }
        else {
          $("div#"+ed.id+"_path_row").append('<span id="max_char_string">&nbsp;'+htmlcount+'</span>')
        }

        // prevent insertion of typed character
        e.preventDefault();
        e.stopPropagation();
        return false;
      } 

I had the same issue and found this link very useful: http://www.ryann.ca/?p=186

Although I changed this slightly so that it reads the attribute of maxlength direct from the textarea.

tinyMCE.init({
    // Options
    setup : function(ed) {
        ed.onKeyUp.add(function(ed, e) {
            var tinylen, htmlcount, maxlength = $("#" + tinyMCE.activeEditor.id).attr("maxlength");
            if (maxlength) {
                // grabbing the length of the curent editors content
                tinylen = ed.getContent().length;

                htmlcount = "HTML Character Count: " + tinylen + "/" + maxlength;
                if (tinylen > maxlength) {
                    htmlcount = "<span style='font-weight:bold; color: #f00;'>" + htmlcount + "</span>";
                }

                // write the html count into the path row of the active editor
                tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id+ '_path_row'), htmlcount);
            }
        });//ed.onKeyUp.add
    }//setup
});

Hope it will work :)

setup: function(ed) {            
            var maxlength = parseInt($('#'+(ed.id)).attr("maxlength"));
            var count = 0;
            ed.on('keydown', function(e) {
                count++;
                if (count >= maxlength)
                {
                    alert("false");
                    return false;
                }
            });
        },
发布评论

评论列表(0)

  1. 暂无评论