tags:

views:

118

answers:

3

here is the script does what i want but not exactly,

my question is, how can i stop user entering text once it reached the lmit of 255 characters?

var limit = 255;
            var txt = $('textarea[id$=txtPurpose]');
            $(txt).keyup(function() {
                var len = $(this).val().length;
                if (len > limit) {
                    //this.value = this.value.substring(0, 50);
                    $(this).addClass('goRed');
                    $('#spn').text(len - limit + " characters exceeded");
                    return false;
                }
                else {
                    $(this).removeClass('goRed');
                    $('#spn').text(limit - len + " characters left");
                }
            });

if there is a better way please let me know.

+2  A: 

Here's what I use to limit something to 1200 chars. When someone types too many characters, I just truncate the contents of that textarea.

$(function() {
    //set up text length counter
    $('#id_limited_textarea').keyup(function() {
        update_chars_left(1200, $('#id_limited_textarea')[0], $('#text_chars_left'));
    });
    //and fire it on doc ready, too
    update_chars_left(1200, $('#id_limited_textarea')[0], $('#text_chars_left'));

});

function update_chars_left(max_len, target_input, display_element) {
   var text_len = target_input.value.length;
   if (text_len >= max_len) {
       target_input.value = target_input.value.substring(0, max_len); // truncate
       display_element.html("0");
   } else {
       display_element.html(max_len - text_len);
   }
}
stevejalim
+3  A: 
$(this).val( $(this).val().substring(0, limit) );
BlueRaja - Danny Pflughoeft
its not truncating the characters.
Abu Hamzah
@Abu: Whoops, try this
BlueRaja - Danny Pflughoeft
Is this code best placed inside the keyup event?
Bryan Downing
Nice solution. @Bryan: yes, that way you can see that you added another character with keydown, but that it gets deleted afterwards. Adding a message for the user is still preferred of course: `if ($(this).val().length > limit) { // msg for user }`.
Alec
@BlueRaja: i have tested but still not 100% working, i can still be able to press one character and before its truncating so which means, i can still have one characters exceeded, have you tested?
Abu Hamzah
A: 

My plugin:

(function($) {

   $.fn.textCounter = function(options) {

        var defaults = {
            maxlimit: 100,      // max limit character
            description: null,  // element for descript count character
            enter: true         // if accept enter
        };

        var options = $.extend({}, defaults, options);

        if (options.description != null) {
            if (typeof options.description == 'string')
                options.description = $('#' + options.description);
        }

        var fevent = function(ev) {

            var value = $(this).val(),
                k = ev.charCode || ev.keyCode || ev.which,
                incremente = 1;

            if (k == 8)
                incremente = -1;

            if (options.enter == false && k == 13)
                return false;

            if (ev.ctrlKey || ev.altKey || ev.metaKey)  //Ignore
                return true;

            if ((value.length + incremente) > options.maxlimit)
                return false;

            return true;
        };

        var fcounter = function(ev) {
            var value = $(this).val();
            $(options.description).text(options.maxlimit - value.length);
        };

        $(this).each(function(i, el) {
            if ($(this).is(':input')) {
                $(this).unbind('keypress.textCounter').bind('keypress.textCounter', fevent);
                $(this).unbind('keyup.textCounter').bind('keyup.textCounter', fcounter);
            }
        });

    };

})(jQuery);
andres descalzo