jquery.textareaCounter.plugin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * jQuery Textarea Characters Counter Plugin v 2.0
  3. * Examples and documentation at: http://roy-jin.appspot.com/jsp/textareaCounter.jsp
  4. * Copyright (c) 2010 Roy Jin
  5. * Version: 2.0 (11-JUN-2010)
  6. * Dual licensed under the MIT and GPL licenses:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.gnu.org/licenses/gpl.html
  9. * Requires: jQuery v1.4.2 or later
  10. */
  11. (function($){
  12. $.fn.textareaCount = function(options, fn) {
  13. var defaults = {
  14. maxCharacterSize: -1,
  15. originalStyle: 'originalTextareaInfo',
  16. warningStyle: 'warningTextareaInfo',
  17. warningNumber: 20,
  18. displayFormat: '#input characters | #words words'
  19. };
  20. var options = $.extend(defaults, options);
  21. var container = $(this);
  22. $("<div class='charleft'>&nbsp;</div>").insertAfter(container);
  23. //create charleft css
  24. var charLeftCss = {
  25. 'width' : container.width()
  26. };
  27. var charLeftInfo = getNextCharLeftInformation(container);
  28. charLeftInfo.addClass(options.originalStyle);
  29. charLeftInfo.css(charLeftCss);
  30. var numInput = 0;
  31. var maxCharacters = options.maxCharacterSize;
  32. var numLeft = 0;
  33. var numWords = 0;
  34. container.bind('keyup', function(event){limitTextAreaByCharacterCount();})
  35. .bind('mouseover', function(event){setTimeout(function(){limitTextAreaByCharacterCount();}, 10);})
  36. .bind('paste', function(event){setTimeout(function(){limitTextAreaByCharacterCount();}, 10);});
  37. function limitTextAreaByCharacterCount(){
  38. charLeftInfo.html(countByCharacters());
  39. //function call back
  40. if(typeof fn != 'undefined'){
  41. fn.call(this, getInfo());
  42. }
  43. return true;
  44. }
  45. function countByCharacters(){
  46. var content = container.val();
  47. var contentLength = content.length;
  48. //Start Cut
  49. if(options.maxCharacterSize > 0){
  50. //If copied content is already more than maxCharacterSize, chop it to maxCharacterSize.
  51. if(contentLength >= options.maxCharacterSize) {
  52. content = content.substring(0, options.maxCharacterSize);
  53. }
  54. var newlineCount = getNewlineCount(content);
  55. // newlineCount new line character. For windows, it occupies 2 characters
  56. var systemmaxCharacterSize = options.maxCharacterSize - newlineCount;
  57. if (!isWin()){
  58. systemmaxCharacterSize = options.maxCharacterSize
  59. }
  60. if(contentLength > systemmaxCharacterSize){
  61. //avoid scroll bar moving
  62. var originalScrollTopPosition = this.scrollTop;
  63. container.val(content.substring(0, systemmaxCharacterSize));
  64. this.scrollTop = originalScrollTopPosition;
  65. }
  66. charLeftInfo.removeClass(options.warningStyle);
  67. if(systemmaxCharacterSize - contentLength <= options.warningNumber){
  68. charLeftInfo.addClass(options.warningStyle);
  69. }
  70. numInput = container.val().length + newlineCount;
  71. if(!isWin()){
  72. numInput = container.val().length;
  73. }
  74. numWords = countWord(getCleanedWordString(container.val()));
  75. numLeft = maxCharacters - numInput;
  76. } else {
  77. //normal count, no cut
  78. var newlineCount = getNewlineCount(content);
  79. numInput = container.val().length + newlineCount;
  80. if(!isWin()){
  81. numInput = container.val().length;
  82. }
  83. numWords = countWord(getCleanedWordString(container.val()));
  84. }
  85. return formatDisplayInfo();
  86. }
  87. function formatDisplayInfo(){
  88. var format = options.displayFormat;
  89. format = format.replace('#input', numInput);
  90. format = format.replace('#words', numWords);
  91. //When maxCharacters <= 0, #max, #left cannot be substituted.
  92. if(maxCharacters > 0){
  93. format = format.replace('#max', maxCharacters);
  94. format = format.replace('#left', numLeft);
  95. }
  96. return format;
  97. }
  98. function getInfo(){
  99. var info = {
  100. input: numInput,
  101. max: maxCharacters,
  102. left: numLeft,
  103. words: numWords
  104. };
  105. return info;
  106. }
  107. function getNextCharLeftInformation(container){
  108. return container.next('.charleft');
  109. }
  110. function isWin(){
  111. var strOS = navigator.appVersion;
  112. if (strOS.toLowerCase().indexOf('win') != -1){
  113. return true;
  114. }
  115. return false;
  116. }
  117. function getNewlineCount(content){
  118. var newlineCount = 0;
  119. for(var i=0; i<content.length;i++){
  120. if(content.charAt(i) == '\n'){
  121. newlineCount++;
  122. }
  123. }
  124. return newlineCount;
  125. }
  126. function getCleanedWordString(content){
  127. var fullStr = content + " ";
  128. var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
  129. var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
  130. var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
  131. var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
  132. var splitString = cleanedStr.split(" ");
  133. return splitString;
  134. }
  135. function countWord(cleanedWordString){
  136. var word_count = cleanedWordString.length-1;
  137. return word_count;
  138. }
  139. };
  140. })(jQuery);