jQuery: Word Count & Words Limit

jQuery: Word Count & Words Limit

    function wordCount(selector,label,limit) {
        var words = $(selector).val().match(/S+/g);
        var length = words.length;

        if (length > limit) {
            var trimmed = $(selector).val().split(/s+/, limit).join(" ");
            $(selector).val(trimmed + " ");
        }
        else {
            $(label).text(length + '/' + limit + ' words');
        }
    }

// Usage
wordCount('#text-input', '#word-count-indicator', 1000);

Leave a Reply

Your email address will not be published. Required fields are marked *

twelve + 4 =