输入框字符限制插件·

(function($) {



    $.fn.maxlength = function(settings) {



        if (typeof settings == 'string') {

            settings = { feedback: settings };

        }



        settings = $.extend({}, $.fn.maxlength.defaults, settings);



        function length(el) {

            var parts = $(el).attr("value");

            if (settings.words)

                parts = $(el).attr("value").length ? parts.split(/\s+/) : { length: 0 };

            return parts.length;

        }



        return this.each(function() {

            var field = this,

                        $field = $(field),

                        limit = $field.attr('maxlength') || settings.limit,

                          $charsLeft = $("" + settings.feedback + "");



            function limitCheck(event) {

                var len = length(this),

                            exceeded = len >= limit,

                            code = event.keyCode;

                if (!exceeded)

                    return;

                switch (code) {

                    case 8:  // allow delete

                    case 9:

                    case 17:

                    case 36: // and cursor keys

                    case 35:

                    case 37:

                    case 38:

                    case 39:

                    case 40:

                    case 46:

                    case 65:

                    case 67:

                        return;

                    default:

                        return settings.words && code != 32 && code != 13 && len == limit;

                }

            }



            var updateCount = function() {

                var len = length(field),

                        msg = "";

                diff = limit - len;

                if (len >= settings.middle) {

                    msg = settings.Msg + settings.DefaultMsg;

                }

                else {

                    msg = settings.DefaultMsg;

                }

                $charsLeft.html(msg.replace(/@/g, (diff || "0")));

                // truncation code

                if (settings.hardLimit && diff < 0) {

                    field.value = settings.words ?

                    // split by white space, capturing it in the result, then glue them back

                            field.value.split(/(\s+)/, (limit * 2) - 1).join('') :

                            field.value.substr(0, limit);

                    updateCount();

                }

            };



            $field.bind('focus keyup input paste', function() {

                setTimeout(function() {

                    updateCount();

                }, 100);

            });

            if (settings.hardLimit) {

                $field.keydown(limitCheck);

            }

            updateCount();

        });

    };

    $.fn.maxlength.defaults = {

        useInput: false,

        hardLimit: true,

        feedback: '#msg',

        words: false,

        middle: 70,

        limit: 140,

        Msg: "已经超过<span style='color:red'>70</span>个字,将分两条短信发送。",

        DefaultMsg: "还可输入<span style='color:green'>@</span>个字"

    };



})(jQuery);

对鼠标的粘贴事件也做了监视

你可能感兴趣的:(插件)