The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

Maxlength textarea

  • # May 5, 2009 at 9:58 am


    I need a script to manage maxlength in a textarea. I’m currently using one from anon-design, which is working just fine.

    There’s one big problem tho. It parses


    as 15 chars instead of just 4, as I need it to. Do you know of any other maxlength script or just a way to fix this?


    # May 5, 2009 at 11:06 am

    It sounds like you’ll have to modify the script to first strip out any accepted tags and then count what remains. Assuming you want to support a subset of the BBCode tags, you could do something like this:

    var stripBBCode = function(value){

    var tags = new Array(‘b’, ‘i’, ‘u’, ‘url’, ‘quote’);
    var result = value;

    for( var index in tags){
    var oRegExp = new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]’);
    result = result.replace(oRegExp, “$1”);
    oRegExp = null;

    return result;

    I don’t guarantee this will work perfectly in all circumstances, but I think you can see where I’m going and expand it to suite your needs.

    # May 6, 2009 at 9:07 am

    Justin, that worked wonders! Thank you very very much :)

    # May 11, 2009 at 8:35 am

    I’ve found a little glitch, the expression matches


    just fine (as 4 chars), but



    I’ve tried some things but none are working for me. Is there a simple fix? As most others I’m not too fond of the syntax =) This is the current expression:

    new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]’);
    # May 11, 2009 at 12:50 pm

    give this a shot.

    new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]||[‘ + tags[index] + ‘=(.*?)](.*?)\[/’ + tags[index] + ‘]’);
    # May 11, 2009 at 8:48 pm

    You guys are friggin geniuses. I really need to learn me some regular expressions.

    # May 14, 2009 at 11:23 am
    "Stack" wrote:
    give this a shot.

    new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]||[‘ + tags[index] + ‘=(.*?)](.*?)\[/’ + tags[index] + ‘]’);

    This doesn’t work for about 10 different reasons. The most important part is that you’ve split the expression up into two different sections that repeat basically the same expression which renders the replace part of the code unusable; you’ve also introduced new capture groups that are unused so they provide a big performance penalty.

    I’ve modified the original expression to make an "=something" optional:

    new RegExp(‘\[‘ + tags[index] + ‘(?:=.+?)?](.*?)\[/’ + tags[index] + ‘]’);

    Notice that I made the "=something" group non-capturing. This way, we still only have 1 capturing group to the replace code works as is. I’ve also added the requirement that something must come after the equals sign, otherwise this would match: "something"; if you don’t want this requirement, change the + to a *.

    Let me know if there are any other glitches and I’ll be glad to give it another look.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed