Grow your CSS skills. Land your dream job.

Maxlength textarea

  • # May 5, 2009 at 9:58 am

    Hi,

    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

    Code:
    [url]test[/url]

    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?

    Thanks,
    Oskar

    # 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:

    Code:
    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

    Code:
    [tag]four[/tag]

    just fine (as 4 chars), but

    Code:
    [url=someurlhere]test[url]

    fails.

    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:

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

    give this a shot.

    Code:
    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.

    Code:
    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:

    Code:
    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.

*May or may not contain any actual "CSS" or "Tricks".