Created
November 27, 2012 18:57
-
-
Save abitdodgy/4156252 to your computer and use it in GitHub Desktop.
A CoffeeScript version of my lightweight character counter for jQuery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(($) -> | |
# Author: Mohamad El-Husseini, www.mohamad.im | |
# Light weight character counter for jQuery. | |
# Example: http://jsfiddle.net/yHPg7/6/ | |
"use strict" | |
$.fn.counter = (options) -> | |
defaultText = -> | |
options.defaultText.replace /minSize/g, options.minSize | |
count = (elem) -> | |
size = elem.val().length | |
target = elem.siblings(options.targetClass) | |
if size is 0 | |
target.html defaultText() | |
else if size < options.minSize | |
target.html (options.minSize - size) + options.minWarning | |
else if size >= options.minSize and size < options.warningSize | |
target.html " " | |
else if size >= options.warningSize and size < options.maxSize | |
target.html (options.maxSize - size) + options.maxWarning | |
else if size >= options.maxSize | |
elem.val elem.val().substring(0, options.maxSize) | |
target.html "0" + options.maxWarning | |
options = $.extend($.fn.counter.defaults, options) | |
@each -> | |
elem = $(this) | |
count elem | |
elem.keyup -> | |
count elem | |
elem.closest("form").submit -> | |
if elem.val().length < options.minSize | |
$(this).find(options.targetClass).fadeOut("fast").fadeIn("fast").fadeOut("fast").fadeIn "fast" | |
false | |
elem | |
$.fn.counter.defaults = | |
minSize: 15 | |
minWarning: " more to go..." | |
maxSize: 25 | |
maxWarning: " characters remaining..." | |
warningSize: 20 | |
targetClass: ".help-block" | |
defaultText: "Enter at least minSize characters." | |
) jQuery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment