Created
June 17, 2014 08:52
-
-
Save RobCombs/60ec6dbca72bb7b68ed9 to your computer and use it in GitHub Desktop.
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
hi, i've updated the plug in to allow for delegation so that current and future text fields will be processed. | |
/** | |
* jQuery Maxlength plugin | |
* @author Emil Stjerneman, updated by Rob Combs | |
* @version $Id: jquery.maxlength.js 18 2014-06-17 | |
* @package jQuery maxlength 1.0 | |
*/ | |
(function($) { | |
$.fn.maxlength = function(options) { | |
/* jQuery plugin that enforces a max length on text elements. This plugin also supports event delegation to bind | |
events to existing and future objects created in the DOM. | |
Example usage: | |
$('textarea.vLargeTextField').maxlength({ | |
events: [], // Array of events to be triggered | |
maxCharacters: 110, // Characters limit | |
status: true, // True to show status indicator below the element | |
statusClass: "status", // The class on the status div | |
statusText: "characters left", // The status text | |
notificationClass: "max-length-notification", // Will be added when maxlength is reached | |
showAlert: false, // True to show a regular alert message | |
alertText: "You have typed too many characters.", // Text in alert message | |
slider: false // True Use counter slider | |
}); | |
*/ | |
var settings = jQuery.extend( | |
{ | |
events: [], // Array of events to be triggerd | |
maxCharacters: 10, // Characters limit | |
status: true, // True to show status indicator bewlow the element | |
statusClass: "status", // The class on the status div | |
statusText: "characters left", // The status text | |
notificationClass: "notification", // Will be added to the emement when maxlength is reached | |
showAlert: false, // True to show a regular alert message | |
alertText: "You have typed too many characters.", // Text in the alert message | |
slider: false // Use counter slider | |
}, options ); | |
// Add the default event | |
$.merge(settings.events, ['keyup']); | |
// Update status div | |
function updateStatus(obj) { | |
var charactersLeft = settings.maxCharacters - $(obj).val().length; | |
if(charactersLeft < 0) { | |
charactersLeft = 0; | |
} | |
obj.next("div").html(charactersLeft + " " + settings.statusText); | |
} | |
// check the number of characters compared to settings.maxCharacters | |
function checkChars(obj) { | |
var valid = true; | |
// Too many chars? | |
if($(obj).val().length >= settings.maxCharacters) { | |
// Too may chars, set the valid boolean to false | |
valid = false; | |
// Add the notifycation class when we have too many chars | |
obj.addClass(settings.notificationClass); | |
// Cut down the string | |
obj.val(item.val().substr(0,settings.maxCharacters)); | |
// Show the alert dialog box, if its set to true | |
showAlert(); | |
} else { | |
// Remove the notification class | |
if(obj.hasClass(settings.notificationClass)) { | |
obj.removeClass(settings.notificationClass); | |
} | |
} | |
if(settings.status) { | |
updateStatus(obj); | |
} | |
} | |
// Shows an alert msg | |
function showAlert() { | |
if(settings.showAlert) { | |
alert(settings.alertText); | |
} | |
} | |
// Check if the element is valid | |
function validateElement(obj) { | |
var ret = false; | |
if(obj.is('textarea')) { | |
ret = true; | |
} else if(item.filter("input[type=text]")) { | |
ret = true; | |
} else if(item.filter("input[type=password]")) { | |
ret = true; | |
} | |
return ret; | |
} | |
// Bind events to the text elements to trigger max length updates | |
$.each(settings.events, function(i, n){ | |
$("body").delegate($(this).selector, n, function (e) { | |
item = $(e.target) | |
charactersLength = $(item).val().length; | |
checkChars($(item)); | |
}); | |
}); | |
return this.each(function() { | |
var item = $(this); | |
var charactersLength = $(this).val().length; | |
// Validate | |
if(!validateElement($(item))) { | |
return false; | |
} | |
// Insert the status div | |
if(settings.status) { | |
item.after($("<div/>").addClass(settings.statusClass).html('-')); | |
updateStatus($(item)); | |
} | |
// Remove the status div | |
if(!settings.status) { | |
var removeThisDiv = item.next("div."+settings.statusClass); | |
if(removeThisDiv) { | |
removeThisDiv.remove(); | |
} | |
} | |
// Slide counter | |
if(settings.slider) { | |
item.next().hide(); | |
item.focus(function() { | |
item.next().slideDown('fast'); | |
}); | |
item.blur(function() { | |
item.next().slideUp('fast'); | |
}); | |
} | |
}); // end this.each(function() | |
}; // end $.fn.maxlength | |
})(django.jQuery); // end max length jquery plug in |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good contribution. Thanks