Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Maxlength textarea

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #24793
    Oskar
    Participant

    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

    #57160
    akeenlabs
    Participant

    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.

    #57197
    Oskar
    Participant

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

    #57403
    Oskar
    Participant

    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] + ‘]’);
    #57407
    Stack
    Member

    give this a shot.

    Code:
    new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]||[‘ + tags[index] + ‘=(.*?)](.*?)\[/’ + tags[index] + ‘]’);
    #57409
    Chris Coyier
    Keymaster

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

    #57507
    akeenlabs
    Participant
    "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)
  • The forum ‘JavaScript’ is closed to new topics and replies.