Expand all

jquery.lengthLimit

Limit string length.

This module provides jQuery plugins that place different types of limits on strings. To use the plugins, load the module with mw.loader.

For other methods for managing strings, see module:mediawiki.String.

Example

mw.loader.using( 'jquery.lengthLimit' ).then( () => {
  // Create an input that only accepts values <= 4 bytes. For example: 💪💪 is not a permitted value.
  $( '<input type="text" value="💪">' ).byteLimit( 4 ).appendTo(document.body);
} );
Source:

Methods

$.fn.byteLimit([limit], [filterFunction]) → {jQuery}staticchainable #

Enforces a byte limit on an input field, assuming UTF-8 encoding, for situations when, for example, a database field has a byte limit rather than a character limit. Plugin rationale: Browser has native maxlength for number of characters (technically, UTF-16 code units), 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 parameters is important!

Parameters:

Name Type Attributes Description
limit number optional

Limit to enforce, fallsback to maxLength-attribute, called with fetched value as argument.

filterFunction function optional

Function to call on the string before assessing the length.

Source:

Returns:

Type
jQuery

Enforces a byte limit on an input field, assuming UTF-8 encoding, for situations when, for example, a database field has a byte limit rather than a character limit.

$.fn.codePointLimit([limit], [filterFunction]) → {jQuery}staticchainable #

Enforces a codepoint (character) limit on an input field.

For unfortunate historical reasons, browsers' native maxlength counts the number of UTF-16 code units rather than Unicode codepoints, which means that codepoints outside the Basic Multilingual Plane (such as many emojis) count as 2 characters each. This plugin exists to correct this.

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 parameters is important!

Parameters:

Name Type Attributes Description
limit number optional

Limit to enforce, fallsback to maxLength-attribute, called with fetched value as argument.

filterFunction function optional

Function to call on the string before assessing the length.

Source:

Returns:

Type
jQuery
Enforces a codepoint (character) limit on an input field.

'$.fn.trimByteLength'(safeVal, newVal, byteLimit, [filterFunction]) → {module:mediawiki.String~StringTrimmed}static #

Utility function to trim down a string, based on byteLimit and given a safe start position.

It supports insertion anywhere in the string, so "foo" to "fobaro" if limit is 4 will result in "fobo", not "foba". Basically emulating the native maxlength by reconstructing where the insertion occurred.

Parameters:

Name Type Attributes Description
safeVal string

Known value that was previously returned by this function, if none, pass empty string.

newVal string

New value that may have to be trimmed down.

byteLimit number

Number of bytes the value may be in size.

filterFunction function optional

See jQuery#byteLimit.

Deprecated:
Source:

Returns:

Type
module:mediawiki.String~StringTrimmed

Utility function to trim down a string, based on byteLimit and given a safe start position.