jquery.inputlimiter.1.3.1.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * jQuery Input Limiter plugin 1.3.1
  3. * http://rustyjeans.com/jquery-plugins/input-limiter/
  4. *
  5. * Copyright (c) 2009 Russel Fones <russel@rustyjeans.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. (function ($) {
  29. $.fn.inputlimiter = function (options) {
  30. var opts = $.extend({}, $.fn.inputlimiter.defaults, options),
  31. $elements = $(this);
  32. if (opts.boxAttach && !$('#' + opts.boxId).length) {
  33. $('<div/>').appendTo("body").attr({id: opts.boxId, 'class': opts.boxClass}).css({'position': 'absolute'}).hide();
  34. // apply bgiframe if available
  35. if ($.fn.bgiframe) {
  36. $('#' + opts.boxId).bgiframe();
  37. }
  38. }
  39. var inputlimiterKeyup = function (e) {
  40. var $this = $(this),
  41. count = counter($this.val());
  42. if (!opts.allowExceed && count > opts.limit) {
  43. $this.val(truncater($this.val()));
  44. }
  45. if (opts.boxAttach) {
  46. $('#' + opts.boxId).css({
  47. 'width': $this.outerWidth() - ($('#' + opts.boxId).outerWidth() - $('#' + opts.boxId).width()) + 'px',
  48. 'left': $this.offset().left + 'px',
  49. 'top': ($this.offset().top + $this.outerHeight()) - 1 + 'px',
  50. 'z-index': 2000
  51. });
  52. }
  53. var charsRemaining = (opts.limit - count > 0 ? opts.limit - count : 0),
  54. remText = opts.remTextFilter(opts, charsRemaining),
  55. limitText = opts.limitTextFilter(opts);
  56. if (opts.limitTextShow) {
  57. $('#' + opts.boxId).html(remText + ' ' + limitText);
  58. // Check to see if the text is wrapping in the box
  59. // If it is lets break it between the remaining test and the limit test
  60. var textWidth = $("<span/>").appendTo("body").attr({id: '19cc9195583bfae1fad88e19d443be7a', 'class': opts.boxClass}).html(remText + ' ' + limitText).innerWidth();
  61. $("#19cc9195583bfae1fad88e19d443be7a").remove();
  62. if (textWidth > $('#' + opts.boxId).innerWidth()) {
  63. $('#' + opts.boxId).html(remText + '<br />' + limitText);
  64. }
  65. // Show the limiter box
  66. $('#' + opts.boxId).show();
  67. } else {
  68. $('#' + opts.boxId).html(remText).show();
  69. }
  70. },
  71. inputlimiterKeypress = function (e) {
  72. var count = counter($(this).val());
  73. if (!opts.allowExceed && count > opts.limit) {
  74. var modifierKeyPressed = e.ctrlKey || e.altKey || e.metaKey;
  75. if (!modifierKeyPressed && (e.which >= 32 && e.which <= 122) && this.selectionStart === this.selectionEnd) {
  76. return false;
  77. }
  78. }
  79. },
  80. inputlimiterBlur = function () {
  81. var $this = $(this);
  82. count = counter($this.val());
  83. if (!opts.allowExceed && count > opts.limit) {
  84. $this.val(truncater($this.val()));
  85. }
  86. if (opts.boxAttach) {
  87. $('#' + opts.boxId).fadeOut('fast');
  88. } else if (opts.remTextHideOnBlur) {
  89. var limitText = opts.limitText;
  90. limitText = limitText.replace(/\%n/g, opts.limit);
  91. limitText = limitText.replace(/\%s/g, (opts.limit === 1 ? '' : 's'));
  92. $('#' + opts.boxId).html(limitText);
  93. }
  94. },
  95. counter = function (value) {
  96. if (opts.limitBy.toLowerCase() === "words") {
  97. return (value.length > 0 ? $.trim(value).replace(/\ +(?= )/g, '').split(' ').length : 0);
  98. }
  99. var count = value.length,
  100. newlines = value.match(/\n/g);
  101. if (newlines && opts.lineReturnCount > 1) {
  102. count += newlines.length * (opts.lineReturnCount - 1);
  103. }
  104. return count;
  105. },
  106. truncater = function (value) {
  107. if (opts.limitBy.toLowerCase() === "words") {
  108. return $.trim(value).replace(/\ +(?= )/g, '').split(' ').splice(0, opts.limit).join(' ') + ' ';
  109. }
  110. return value.substring(0, opts.limit);
  111. };
  112. $(this).each(function (i) {
  113. var $this = $(this);
  114. if ((!options || !options.limit) && opts.useMaxlength && parseInt($this.attr('maxlength')) > 0 && parseInt($this.attr('maxlength')) != opts.limit) {
  115. $this.inputlimiter($.extend({}, opts, { limit: parseInt($this.attr('maxlength')) }));
  116. } else {
  117. if (!opts.allowExceed && opts.useMaxlength && opts.limitBy.toLowerCase() === "characters") {
  118. $this.attr('maxlength', opts.limit);
  119. }
  120. $this.unbind('.inputlimiter');
  121. $this.bind('keyup.inputlimiter', inputlimiterKeyup);
  122. $this.bind('keypress.inputlimiter', inputlimiterKeypress);
  123. $this.bind('blur.inputlimiter', inputlimiterBlur);
  124. }
  125. });
  126. };
  127. $.fn.inputlimiter.remtextfilter = function (opts, charsRemaining) {
  128. var remText = opts.remText;
  129. if (charsRemaining === 0 && opts.remFullText !== null) {
  130. remText = opts.remFullText;
  131. }
  132. remText = remText.replace(/\%n/g, charsRemaining);
  133. remText = remText.replace(/\%s/g, (opts.zeroPlural ? (charsRemaining === 1 ? '' : 's') : (charsRemaining <= 1 ? '' : 's')));
  134. return remText;
  135. };
  136. $.fn.inputlimiter.limittextfilter = function (opts) {
  137. var limitText = opts.limitText;
  138. limitText = limitText.replace(/\%n/g, opts.limit);
  139. limitText = limitText.replace(/\%s/g, (opts.limit <= 1 ? '' : 's'));
  140. return limitText;
  141. };
  142. $.fn.inputlimiter.defaults = {
  143. limit: 255,
  144. boxAttach: true,
  145. boxId: 'limiterBox',
  146. boxClass: 'limiterBox',
  147. remText: '%n character%s remaining.',
  148. remTextFilter: $.fn.inputlimiter.remtextfilter,
  149. remTextHideOnBlur: true,
  150. remFullText: null,
  151. limitTextShow: true,
  152. limitText: 'Field limited to %n character%s.',
  153. limitTextFilter: $.fn.inputlimiter.limittextfilter,
  154. zeroPlural: true,
  155. allowExceed: false,
  156. useMaxlength: true,
  157. limitBy: 'characters',
  158. lineReturnCount: 1
  159. };
  160. })(jQuery);