1/** 2 * jQuery Maxlength plugin 3 * @version $Id: jquery.maxlength.js 18 2009-05-16 15:37:08Z [email protected] $ 4 * @package jQuery maxlength 1.0.5 5 * @copyright Copyright (C) 2009 Emil Stjerneman / http://www.anon-design.se 6 * @license GNU/GPL, see LICENSE.txt 7 */ 8 9(function($) 10{ 11 12 $.fn.maxlength = function(options) 13 { 14 var settings = jQuery.extend( 15 { 16 events: [], // Array of events to be triggerd 17 maxCharacters: 10, // Characters limit 18 status: true, // True to show status indicator bewlow the element 19 statusClass: "status", // The class on the status div 20 statusText: "character left", // The status text 21 notificationClass: "notification", // Will be added to the emement when maxlength is reached 22 showAlert: false, // True to show a regular alert message 23 alertText: "You have typed too many characters.", // Text in the alert message 24 slider: false // Use counter slider 25 }, options ); 26 27 // Add the default event 28 $.merge(settings.events, ['keyup']); 29 30 return this.each(function() 31 { 32 var item = $(this); 33 var charactersLength = $(this).val().length; 34 35 // Update the status text 36 function updateStatus() 37 { 38 var charactersLeft = settings.maxCharacters - charactersLength; 39 40 if(charactersLeft < 0) 41 { 42 charactersLeft = 0; 43 } 44 45 item.next("div").html(charactersLeft + " " + settings.statusText); 46 } 47 48 function checkChars() 49 { 50 var valid = true; 51 52 // Too many chars? 53 if(charactersLength >= settings.maxCharacters) 54 { 55 // Too may chars, set the valid boolean to false 56 valid = false; 57 // Add the notifycation class when we have too many chars 58 item.addClass(settings.notificationClass); 59 // Cut down the string 60 item.val(item.val().substr(0,settings.maxCharacters)); 61 // Show the alert dialog box, if its set to true 62 showAlert(); 63 } 64 else 65 { 66 // Remove the notification class 67 if(item.hasClass(settings.notificationClass)) 68 { 69 item.removeClass(settings.notificationClass); 70 } 71 } 72 73 if(settings.status) 74 { 75 updateStatus(); 76 } 77 } 78 79 // Shows an alert msg 80 function showAlert() 81 { 82 if(settings.showAlert) 83 { 84 alert(settings.alertText); 85 } 86 } 87 88 // Check if the element is valid. 89 function validateElement() 90 { 91 var ret = false;
92 93 if(item.is('textarea')) { 94 ret = true; 95 } else if(item.filter("input[type=text]")) { 96 ret = true; 97 } else if(item.filter("input[type=password]")) { 98 ret = true; 99 } 100 101 return ret; 102 } 103 104 // Validate 105 if(!validateElement()) 106 { 107 return false; 108 } 109 110 // Loop through the events and bind them to the element 111 $.each(settings.events, function (i, n) { 112 item.bind(n, function(e) { 113 charactersLength = item.val().length; 114 checkChars(); 115 }); 116 }); 117 118 // Insert the status div 119 if(settings.status) 120 { 121 item.after($("<div/>").addClass(settings.statusClass).html('-')); 122 updateStatus(); 123 } 124 125 // Remove the status div 126 if(!settings.status) 127 { 128 var removeThisDiv = item.next("div."+settings.statusClass); 129 130 if(removeThisDiv) { 131 removeThisDiv.remove(); 132 } 133 134 } 135 136 // Slide counter 137 if(settings.slider) { 138 item.next().hide(); 139 140 item.focus(function(){ 141 item.next().slideDown('fast'); 142 }); 143 144 item.blur(function(){ 145 item.next().slideUp('fast'); 146 }); 147 } 148 149 }); 150 }; 151})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.