3

this is my tinymce script:

<script>
      tinymce.init({
          menubar: false,
          selector: "textarea",
          plugins: [
              "advlist autolink lists link image charmap print preview anchor",
              "searchreplace visualblocks code fullscreen",
              "insertdatetime media table contextmenu paste"
          ],
          toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
      });
    </script>

how to limit characters to 500?

user3818592
  • 165
  • 1
  • 3
  • 8

2 Answers2

1

I added the "stopPropagation" line, in order to prevent the user from continuing to enter characters: 1 - in the html code of the textarea it is necessary to include the value of maxlength and Id.
2 - in script part, code below. If you want, uncomment the alert() line, and put your message.

<script type="text/javascript">
  tinymce.init ({
    ...
    ...
      setup: function(ed) {
        var maxlength = parseInt($("#" + (ed.id)).attr("maxlength"));
        var count = 0;
        ed.on("keydown", function(e) {
          count++;
          if (count > maxlength) {
            // alert("You have reached the character limit");
            e.stopPropagation();
            return false;
          }
        });
     },
<textarea type="text" id="test" name="test" maxlength="10"></textarea>
Brasil
  • 31
  • 1
0

It can work, but I don't know how to let it can press delete key...

  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;
            }
        });
    },
carry0987
  • 117
  • 1
  • 2
  • 14