1/* 2 @author: remy sharp / http://remysharp.com 3 @params: 4 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. 5 hardLimit - whether to stop the user being able to keep adding characters. Defaults to true. 6 useInput - whether to look for a hidden input named 'maxlength' instead of the maxlength attribute. Defaults to false. 7 words - limit by characters or words, set this to true to limit by words. Defaults to false. 8 @license: Creative Commons License - ShareAlike http://creativecommons.org/licenses/by-sa/3.0/ 9 @version: 1.2 10 @changes: code tidy via Ariel Flesler and fix when pasting over limit and including \t or \n 11*/ 12 13(function ($) { 14 15$.fn.maxlength = function (settings) { 16 17 if (typeof settings == 'string') { 18 settings = { feedback : settings }; 19 } 20 21 settings = $.extend({}, $.fn.maxlength.defaults, settings); 22 23 function length(el) { 24 var parts = el.value; 25 if ( settings.words ) 26 parts = el.value.length ? parts.split(/\s+/) : { length : 0 }; 27 return parts.length; 28 } 29 30 return this.each(function () { 31 var field = this, 32 $field = $(field), 33 $form = $(field.form), 34 limit = settings.useInput ? $form.find('input[name=maxlength]').val() : $field.attr('maxlength'), 35 $charsLeft = $form.find(settings.feedback); 36 37 function limitCheck(event) { 38 var len = length(this), 39 exceeded = len >= limit, 40 code = event.keyCode; 41 42 if ( !exceeded ) 43 return; 44 45 switch (code) { 46 case 8: // allow delete 47 case 9: 48 case 17: 49 case 36: // and cursor keys 50 case 35: 51 case 37: 52 case 38: 53 case 39: 54 case 40: 55 case 46: 56 case 65: 57 return; 58 59 default: 60 return settings.words && code != 32 && code != 13 && len == limit; 61 } 62 } 63 64 65 var updateCount = function () { 66 var len = length(field), 67 diff = limit - len; 68 69 $charsLeft.html( diff || "0" ); 70 71 // truncation code 72 if (settings.hardLimit && diff < 0) { 73 field.value = settings.words ? 74 // split by white space, capturing it in the result, then glue them back 75 field.value.split(/(\s+)/, (limit*2)-1).join('') : 76 field.value.substr(0, limit); 77 78 updateCount(); 79 } 80 }; 81 82 $field.keyup(updateCount).change(updateCount); 83 if (settings.hardLimit) { 84 $field.keydown(limitCheck); 85 } 86 87 updateCount(); 88 }); 89}; 90 91$.fn.maxlength.defaults = { 92 useInput : false, 93 hardLimit : true, 94 feedback : '.charsLeft', 95 words : false 96}; 97 98})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.