Files
phpbb_mchat_tlw/styles/prosilver/template/jquery.maxlength.min.js
2015-04-22 21:34:31 +02:00

13 lines
1.8 KiB
JavaScript

/*
@author: remy sharp / http://remysharp.com
@params:
feedback - the selector for the element that gives the user feedback. Note that this will be relative to the form the plugin is run against.
hardLimit - whether to stop the user being able to keep adding characters. Defaults to true.
useInput - whether to look for a hidden input named 'maxlength' instead of the maxlength attribute. Defaults to false.
words - limit by characters or words, set this to true to limit by words. Defaults to false.
@license: Creative Commons License - ShareAlike http://creativecommons.org/licenses/by-sa/3.0/
@version: 1.2
@changes: code tidy via Ariel Flesler and fix when pasting over limit and including \t or \n
*/
(function(a){a.fn.maxlength=function(b){function c(a){var c=a.value;if(b.words)c=a.value.length?c.split(/\s+/):{length:0};return c.length}if(typeof b=="string"){b={feedback:b}}b=a.extend({},a.fn.maxlength.defaults,b);return this.each(function(){function i(a){var d=c(this),e=d>=g,f=a.keyCode;if(!e)return;switch(f){case 8:case 9:case 17:case 36:case 35:case 37:case 38:case 39:case 40:case 46:case 65:return;default:return b.words&&f!=32&&f!=13&&d==g}}var d=this,e=a(d),f=a(d.form),g=b.useInput?f.find("input[name=maxlength]").val():e.attr("maxlength"),h=f.find(b.feedback);var j=function(){var e=c(d),f=g-e;h.html(f||"0");if(f<497){a("#charsreman").show()}else{a("#charsreman").hide()};if(f<499){a("#charsreman2").show()}else{a("#charsreman2").hide()};if(f<500){a("#charsreman3").show()}else{a("#charsreman3").hide()}if(b.hardLimit&&f<0){d.value=b.words?d.value.split(/(\s+)/,g*2-1).join(""):d.value.substr(0,g);j()}};e.keyup(j).change(j).focus(j);if(b.hardLimit){e.keydown(i)}j()})};a.fn.maxlength.defaults={useInput:false,hardLimit:true,feedback:".charsLeft",words:false}})(jQuery)