2011-04-22 10:58:58 +00:00
|
|
|
/**
|
|
|
|
|
* jQuery byteLimit
|
2011-06-30 01:06:47 +00:00
|
|
|
*
|
|
|
|
|
* @author Jan Paul Posma
|
2011-04-22 10:58:58 +00:00
|
|
|
*/
|
|
|
|
|
( function( $ ) {
|
|
|
|
|
|
|
|
|
|
/**
|
2011-08-08 17:02:29 +00:00
|
|
|
* Enforces a byte limit to a textbox, so that UTF-8 entries are counted as well, when, for example,
|
|
|
|
|
* a databae field has a byte limit rather than a character limit.
|
|
|
|
|
* Plugin rationale: Browser has native maxlength for number of characters, this plugin exists to
|
|
|
|
|
* limit number of bytes instead.
|
|
|
|
|
*
|
|
|
|
|
* Can be called with a custom limit (to use that limit instead of the maxlength attribute value),
|
|
|
|
|
* a filter function (in case the limit should apply to something other than the exact input value),
|
|
|
|
|
* or both. Order of arguments is important!
|
|
|
|
|
*
|
|
|
|
|
* @context {jQuery} Instance of jQuery for one or more input elements
|
|
|
|
|
* @param limit {Number} (optional) Limit to enforce, fallsback to maxLength-attribute,
|
|
|
|
|
* called with fetched value as argument.
|
|
|
|
|
* @param fn {Function} (optional) Function to call on the input string before assessing the length
|
|
|
|
|
* @return {jQuery} The context
|
2011-04-22 10:58:58 +00:00
|
|
|
*/
|
2011-08-08 17:02:29 +00:00
|
|
|
$.fn.byteLimit = function( limit, fn ) {
|
|
|
|
|
// If the first argument is the function,
|
|
|
|
|
// set fn to the first argument's value and ignore the second argument.
|
|
|
|
|
if ( $.isFunction( limit ) ) {
|
|
|
|
|
fn = limit;
|
|
|
|
|
limit = undefined;
|
|
|
|
|
}
|
2011-04-26 20:00:59 +00:00
|
|
|
|
2011-08-08 17:02:29 +00:00
|
|
|
// Default limit to current attribute value
|
|
|
|
|
if ( limit === undefined ) {
|
2011-04-26 20:00:59 +00:00
|
|
|
limit = this.attr( 'maxLength' );
|
|
|
|
|
|
2011-08-08 17:02:29 +00:00
|
|
|
// If limit passed, update/set attribute value instead
|
2011-04-26 20:00:59 +00:00
|
|
|
} else {
|
|
|
|
|
this.attr( 'maxLength', limit );
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-30 01:06:47 +00:00
|
|
|
// Nothing passed and/or empty attribute, return without binding an event.
|
2011-08-08 17:02:29 +00:00
|
|
|
if ( limit === undefined ) {
|
2011-04-26 20:00:59 +00:00
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-08 17:02:29 +00:00
|
|
|
// Save function for reference
|
|
|
|
|
this.data( 'byteLimit-callback', fn );
|
|
|
|
|
|
2011-04-26 20:00:59 +00:00
|
|
|
// We've got something, go for it:
|
|
|
|
|
return this.keypress( function( e ) {
|
|
|
|
|
// First check to see if this is actually a character key
|
2011-04-22 10:58:58 +00:00
|
|
|
// being pressed.
|
|
|
|
|
// Based on key-event info from http://unixpapa.com/js/key.html
|
|
|
|
|
// jQuery should also normalize e.which to be consistent cross-browser,
|
|
|
|
|
// however the same check is still needed regardless of jQuery.
|
|
|
|
|
|
|
|
|
|
// Note: At the moment, for some older opera versions (~< 10.5)
|
|
|
|
|
// some special keys won't be recognized (aka left arrow key).
|
|
|
|
|
// Backspace will be, so not big issue.
|
|
|
|
|
|
|
|
|
|
if ( e.which === 0 || e.charCode === 0 || e.which === 8 ||
|
|
|
|
|
e.ctrlKey || e.altKey || e.metaKey )
|
|
|
|
|
{
|
|
|
|
|
return true; //a special key (backspace, etc) so don't interfere.
|
|
|
|
|
}
|
2011-08-08 17:02:29 +00:00
|
|
|
|
|
|
|
|
var val = fn !== undefined ? fn( $( this ).val() ): $( this ).val(),
|
|
|
|
|
len = $.byteLength( val ),
|
|
|
|
|
// Note that keypress returns a character code point, not a keycode.
|
|
|
|
|
// However, this may not be super reliable depending on how keys come in...
|
|
|
|
|
charLen = $.byteLength( String.fromCharCode( e.which ) );
|
2011-04-26 20:00:59 +00:00
|
|
|
|
2011-07-11 17:53:52 +00:00
|
|
|
if ( ( len + charLen ) > limit ) {
|
2011-04-22 10:58:58 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} )( jQuery );
|