question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MaxLength attribute

See original GitHub issue

Is there a way to set a MaxLength number of characters allowed?

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
gskinnercommented, Mar 7, 2014

For anyone else encountering this, here’s a method I wrote to enable maxLength on CodeMirror editors. It hasn’t been tested extensively, but seems to work properly with multiline copy/paste & regular editing. Just define a maxLength option on your CM instance, and add this beforeChange handler:

CMUtils.enforceMaxLength = function(cm, change) {
    var maxLength = cm.getOption("maxLength");
    if (maxLength && change.update) {
        var str = change.text.join("\n");
        var delta = str.length-(cm.indexFromPos(change.to) - cm.indexFromPos(change.from));
        if (delta <= 0) { return true; }
        delta = cm.getValue().length+delta-maxLength;
        if (delta > 0) {
            str = str.substr(0, str.length-delta);
            change.update(change.from, change.to, str.split("\n"));
        }
    }
    return true;
}

myCM.setOption("maxLength", 25);
myCM.on("beforeChange", CMUtils.enforceMaxLength);
3reactions
marijnhcommented, Sep 12, 2012

No, there isn’t. You could, if you really need this, register an onChange handler that, when the content becomes too long, undoes the current change (or simply marks the content as invalid by styling the editor differently).

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML input maxlength Attribute - W3Schools
The maxlength attribute specifies the maximum number of characters allowed in the <input> element. Browser Support. The numbers in the table specify the...
Read more >
HTML maxlength Attribute - Dofactory
The maxlength attribute specifies the maximum number of characters that can be entered. By default, the maximum is 524,288 characters. Syntax. #. <tagname ......
Read more >
HTML maxlength Attribute - GeeksforGeeks
HTML maxlength Attribute ... It specifies the maximum number of characters that have been allowed in the Element. It can be used on...
Read more >
Data Annotations - MaxLength Attribute in EF 6 & EF Core
The MaxLength attribute specifies the maximum length of data value allowed for a property which in turn sets the size of a corresponding...
Read more >
HTML5 Forms: MaxLength Type Attribute - Wufoo
The maxlength attribute limits the number of characters that an <input> or <textarea> can accept. The maxlength attribute on textarea is new in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found