├── README.md ├── twitlimit-0.2.0.compressed.js ├── twitlimit-0.2.0.js └── examples └── index.html /README.md: -------------------------------------------------------------------------------- 1 | #jQuery-TwitLimit 2 | *A jQuery Plug-in* 3 | 4 | ##Description 5 | **Author**: Mike Helmick 6 | **Last Edit**: July 14th, 2010 7 | **Version**: 0.2.0 8 | 9 | 10 | A nice, easy jQuery plug-in limiting the amount of characters on a give DOM element. 11 | 12 | ##Usage 13 | * See example folder for usage https://github.com/michaelhelmick/jquery-twitlimit/tree/master/examples 14 | 15 | ##License 16 | This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 17 | 18 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 19 | 20 | You should have received a copy of the GNU General Public Licensealong with this program. If not, see . -------------------------------------------------------------------------------- /twitlimit-0.2.0.compressed.js: -------------------------------------------------------------------------------- 1 | (function($){$.fn.twitLimit=function(_1){$.fn.twitLimit.defaults={limit:140,message:"%1 Characters Remaining.",counterElem:"#limit",allowNegative:false,dangerMode:true,dangerBold:true,dangerColors:{dark:"550505",medium:"980808",bright:"e90909"},onNegative:function(){},onPositive:function(){}};var o=$.extend({},$.fn.twitLimit.defaults,_1);o.limit=Math.abs(o.limit);function _2(_3){var _4=$("

");if(_3<=20&&_3>=10){var _5=$("").attr("class","twitLimit_count").text(_3).css({"color":"#"+o.dangerColors.dark});}else{if(_3<10&&_3>0){var _5=$("").attr("class","twitLimit_count").text(_3).css({"color":"#"+o.dangerColors.medium});}else{if(_3<=0){var _5=$("").attr("class","twitLimit_count").text(_3).css({"color":"#"+o.dangerColors.bright});}else{return _3;}}}if(o.dangerBold==true){_5.css({"font-weight":"bold"});}if(_3<=20){_4.append(_5);}_5=_4.html();return _5;};function _6(_7,_8){_7=$(_7);_8=$(_8);if(o.allowNegative===false){if(_7.val().length>o.limit){_7.val(_7.val().substr(0,o.limit));}}if(o.dangerMode==true){_8.html(o.message.replace(/%1/,_2((o.limit-_7.val().length))));}else{_8.html(o.message.replace(/%1/,(o.limit-_7.val().length)));}if(o.limit-_7.val().length<0){o.onNegative(_7,_8);}if(o.limit-_7.val().length>=0){o.onPositive(_7,_8);}};return this.each(function(e){var _9=$(this);_9.bind("keydown keypress keyup change focus mouseout mouseover paste",function(e){_6(_9,o.counterElem);});if(_9.addEventListener){_9.addEventListener("input",function(){_6(_9,o.counterElem);},false);}_6(_9,o.counterElem);});};})(jQuery); -------------------------------------------------------------------------------- /twitlimit-0.2.0.js: -------------------------------------------------------------------------------- 1 | /*********************************************************************************************************** 2 | / TwitLimit v0.2.0 3 | / by Mike Helmick - http://michaelhelmick.com 4 | / Last Modification: 7.14.2010 5 | / Documentation: http://blog.michaelhelmick.com/2010/07/15/twitlimit-jquery-character-limiter/ 6 | / Examples: http://code.michalehelmick.com/twitlimit/ 7 | 8 | 9 | ,--. ,--. ,--. ,--. ,--. 10 | ,--. ,--.,-| | ,---. | |,-. ,---. ,--.--. ,---. ,-| |,--.,--. ,---.,-' '-.`--' ,---. ,--,--, ,---. 11 | \ ' /' .-. || .-. :| / | .-. || .--'| .-. |' .-. || || || .--''-. .-',--.| .-. || \( .-' 12 | \ ' \ `-' |\ --.| \ \ | '-' '| | ' '-' '\ `-' |' '' '\ `--. | | | |' '-' '| || |.-' `) 13 | .-' / `---' `----'`--'`--'| |-' `--' `---' `---' `----' `---' `--' `--' `---' `--''--'`----' 14 | `---' `--' 15 | 16 | 17 | / Licensed under both MIT and GPL licenses: 18 | / http://www.opensource.org/licenses/mit-license.php 19 | / http://www.gnu.org/licenses/gpl.html 20 | 21 | / Thanks jQuery for making javascript simple! :) 22 | / Copyright 2010 Mike Helmick 23 | **********************************************************************************************************/ 24 | 25 | (function($) { 26 | 27 | $.fn.twitLimit = function(options) { 28 | 29 | $.fn.twitLimit.defaults = { 30 | limit: 140, // Default limit, if no limit set upon selector. (In honor of Twitter <3) 31 | message: '%1 Characters Remaining.', // Keep %1 intact when defining custom message. (%1 is replaced by number of characters left.) 32 | counterElem: '#limit', // ID or Class of element where you want the message to display. 33 | allowNegative: false, // Whether to allow the count to go negative or not. 34 | dangerMode: true, // Will change the number of characters left "%1" with the colors defined in dangerColors. 35 | dangerBold: true, // Will make the number of characters left "%1" bold. 36 | dangerColors: {dark:'550505', // Colors for dangerMode. 37 | medium:'980808', // Colors for dangerMode. 38 | bright:'e90909'}, // Colors for dangerMode. 39 | onNegative: function(){}, // Called when character limit is less than zero. 40 | onPositive: function(){} // Called for when character limit is greater than or equal to zero. 41 | }; 42 | 43 | var o = $.extend({}, $.fn.twitLimit.defaults, options); 44 | 45 | o.limit = Math.abs(o.limit); 46 | 47 | function danger(count) { 48 | var $temp = $('

'); 49 | 50 | if(count <= 20 && count >= 10) var $count = $('').attr('class', 'twitLimit_count').text(count).css({'color':'#'+o.dangerColors.dark}); 51 | else if(count < 10 && count > 0) var $count = $('').attr('class', 'twitLimit_count').text(count).css({'color':'#'+o.dangerColors.medium}); 52 | else if(count <= 0) var $count = $('').attr('class', 'twitLimit_count').text(count).css({'color':'#'+o.dangerColors.bright}); 53 | else return count 54 | 55 | if(o.dangerBold == true) $count.css({'font-weight':'bold'}); 56 | if(count <= 20) $temp.append($count); $count = $temp.html(); 57 | 58 | return $count; 59 | } 60 | 61 | function count(elem, counterElem) { 62 | elem = $(elem); 63 | counterElem = $(counterElem); 64 | 65 | if(o.allowNegative === false) if(elem.val().length > o.limit) elem.val(elem.val().substr(0, o.limit)); 66 | 67 | if(o.dangerMode == true) counterElem.html(o.message.replace(/%1/, danger((o.limit - elem.val().length)))); 68 | else counterElem.html(o.message.replace(/%1/, (o.limit - elem.val().length))); 69 | 70 | if(o.limit-elem.val().length < 0) o.onNegative(elem, counterElem); 71 | if(o.limit-elem.val().length >= 0) o.onPositive(elem, counterElem); 72 | } 73 | 74 | return this.each(function(e) { 75 | var toCheck = $(this); 76 | 77 | // Check limit upon each of the following events. 78 | toCheck 79 | .bind("keydown keypress keyup change focus mouseout mouseover paste", function(e) { 80 | count(toCheck, o.counterElem); 81 | }); 82 | 83 | if (toCheck.addEventListener) toCheck.addEventListener('input', function () { count(toCheck, o.counterElem); }, false); 84 | count(toCheck, o.counterElem); 85 | }); 86 | 87 | } 88 | 89 | })(jQuery); -------------------------------------------------------------------------------- /examples/index.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | Character Limit 8 | 9 | 10 | 11 | 12 | 41 | 42 | 43 | 44 |

Demo running TwitLimit 0.2.0

45 | 46 |

Documentation
47 | Download

48 | 49 |

Demo 1:

50 | This example is using all default options of TwitLimit. 51 |
52 |

53 | $(document).ready(function(){
54 | $('#limitThis').twitLimit({}); 55 | }); 56 |

57 |
58 |
59 | 60 | 61 |
62 | 63 |

Demo 2:

64 | This example modifies the "message" option (%1 is used because it is replaced by the number characters left) and the "counterElem" which is where the message is displayed. 65 |
66 |

67 | $(document).ready(function(){
68 | $('#limitThis2').twitLimit({
69 | message: 'You have %1 Characters left!', 70 | counterElem: '#limit2', 71 | allowNegative: true 72 | });
73 | }); 74 |

75 |
76 |
77 | 78 | 79 |
80 | 81 |

Demo 3:

82 | This example disables "dangerMode", so the number of characters left won't go from dark to medium to bright red. It also adds listeners for when the count goes negative (it disables the submit button) and when it returns positive (it enables the submit button). 83 |
84 |

85 | $(document).ready(function(){
86 | $('#limitThis3').twitLimit({
87 | counterElem: '#limit3', 88 | allowNegative: true, 89 | dangerMode: false, 90 | onNegative: function(){ $('#submit-button').attr('disabled', 'disabled'); }, 91 | onPositive: function(){ $('#submit-button').removeAttr('disabled'); } 92 | 93 | });
94 | }); 95 |

96 |
97 |
98 | 99 | 100 | 101 |
102 | 103 |

Demo 4:

104 | This demo changes the colors of the count changes and does not change the count bold (when 20 characters or less left) when dangerMode is enabled. 105 |
106 |

107 | $(document).ready(function(){
108 | $('#limitThis4').twitLimit({
109 | counterElem: '#limit4', 110 | dangerBold: false, 111 | dangerColors: { 112 | dark: '5b04dd' 113 | medium: '3d0fe9' 114 | bright: '374aff' 115 | 116 | } 117 | });
118 | }); 119 |

120 |
121 |
122 | 123 | 124 | 125 | 130 | 131 | --------------------------------------------------------------------------------