forked from craigmccoy/jquery-charcount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.charcount.min.js
1 lines (1 loc) · 2.35 KB
/
jquery.charcount.min.js
1
(function(a){var b=1;var c="before";var d="after";var e="none";var f=function(f){if(!a.isPlainObject(f)){return{}}a.each(f,function(g,h){if(g=="maxLength"){f.maxLength=parseInt(h);if(isNaN(f.maxLength)){delete f.maxLength}else if(f.maxLength<b){f.maxLength=b}else if(f.maxLength>a.charcount.config.max){f.maxLength=a.charcount.config.max}}else if(g=="position"){f.position=a.trim(h.toString().toLowerCase());if(a.inArray(f.position,[c,d,e])<0){delete f.position}}else if(g=="preventOverage"){f.preventOverage=Boolean(h)}else if(g=="classPrefix"){f.classPrefix=a.trim(h.toString().toLowerCase())}else{delete f[g]}});return f};a.expr[":"].charcountable=function(b){return a(b).is('textarea,input[type="text"],input[type="password"],input[type="email"],input[type="url"],input[type="search"]')};a.fn.maxlength=function(){var c=a(this);if(arguments.length>0){var d=parseInt(arguments[0]);if(!isNaN(d)){c.attr("maxlength",d)}return c}else{var e=parseInt(c.attr("maxlength"));return e<b||e>a.charcount.config.max||isNaN(e)?undefined:e}};a.charcount={util:{pluralize:function(a,b){var c=arguments.length>2?arguments[2]:b+"s";return a==1?b:c}},config:{max:4e3},defaults:{maxLength:250,position:c,preventOverage:true,classPrefix:"charcount"}};a.fn.charcount=function(){var b=arguments.length>0?f(arguments[0]):{};var g=a.extend({},a.charcount.defaults,b);return this.each(function(b){var h=a(this);if(h.is(":charcountable")){var i={maxLength:h.maxlength()};if(i.maxLength==undefined){delete i.maxLength}var j=a.extend({},g,f(i));h.removeAttr("maxlength");if(j.preventOverage){h.maxlength(j.maxLength)}if(j.position!=e){h.wrap("<span />").addClass(j.classPrefix);var k=a("<span />").addClass(j.classPrefix+"-display");if(j.position==c){k.insertBefore(h).addClass(j.classPrefix+"-position-"+c)}else if(j.position==d){k.insertAfter(h).addClass(j.classPrefix+"-position-"+d)}}h.bind("keyup keypress charcount",function(b){var c=a(b.target).val().length;var d=j.maxLength-c;if(j.preventOverage&&d<1){b.preventDefault();h.val(h.val().substr(0,j.maxLength))}if(b.type=="keyup"||b.type=="charcount"){h.trigger("update",[c,d]);if(j.position!=e){var f=h.parent().find("."+j.classPrefix+"-display").removeClass(j.classPrefix+"-overage");if(d<0){f.addClass(j.classPrefix+"-overage")}f.text(d>=0?d+" "+a.charcount.util.pluralize(d,"character")+" remaining":Math.abs(d)+" too many characters")}}}).trigger("charcount")}})}})(jQuery)