Limit text area value and cut the rest

Posted by streetparade on Stack Overflow See other posts from Stack Overflow or by streetparade
Published on 2010-06-07T08:02:03Z Indexed on 2010/06/07 8:12 UTC
Read the original article Hit count: 180

Filed under:
|
|

I have a textarea which users can enter some tags. I need to limit this tags to 20 tags.

Tags could be enterer this way

maytag1,mytag2,mytag3

What i wrote is this function

function limitTags()
{

    var tags        =   $("input[type=text][name=tags]").val()
    var tag         =   $.trim(tags);
    var selected            = new Array();

    /**
     * replace the last ','
     */
    if(tag.substring(tag.length - 1) == ",")
    {
         *///tag = tag.replace(tag.length - 1, '');
    }


    var enteredTags = tag.split(",");

    if( enteredTags.length > 20 )
    {
        //$("input[type=text][name=tags]").val(enteredTags.join(",", enteredTags));

        alert("Only 20 Tags allowed");
    }


}

The alert works just fine but, after the alert box is gone. i can continiue entering tags till the alert box appears.

What i need is cut the text after the messagebox which was entered also the last ","

I hope i could ask my question clear.

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery