jquery.maxlength.js

A plugin for dynamic decimal max length auto-configuration
View and get the source code on Github

提供数字长度限制功能的小插件
在Github上获取源码

[data-max-length]

Add the attribute [data-max-length="[$minus][$integral].[$fractional]"] to your input element to enable automatic configuration.

为input标签添加属性[data-max-length="[$minus][$integral].[$fractional]"]以自动配置数字长度限制.

<input type="text" data-max-length="-5.2">

Omit [$minus] if negative numbers are not allowed in your input element.

如果不允许输入负数,则省略配置参数中的负号[$minus]即可.

<input type="text" data-max-length="5.2">

[$fractional] can be omitted as well if only integral numbers are allowed in your input element.

如果不允许输入小数,则省略配置参数中的小数长度[$fractional]即可.

<input type="text" data-max-length="5">

Values of 0 for the integral limit, as well as any other unreadable parameters, will be reset to the default value of {integral: 9}.

如果配置参数中的整数长度小于0,或指定了任何无效参数,则会被重置为限制9位正整数.

<input type="text" data-max-length="0.2">
<input type="text" data-max-length="">

[data-disable-autofill]

Add the attribute [data-disable-autofill] to disable fractional autofill.

添加属性[data-disable-autofill]以禁用自动填充小数.

<input type="text" data-max-length="5.2" data-disable-autofill>

[data-disable-auto-comma]

Add the attribute [data-disable-auto-comma] to disable comma autofill.

添加属性[data-disable-auto-comma]以禁用自动填充千分位符.

<input type="text" data-max-length="5" data-disable-auto-comma>

[data-disable-smart-minus]

Press the minus key to conveniently change the plus or minus sign of the number when [data-max-length] is enabled.
Add the attribute [data-disable-smart-minus] to disable this mechanism.

启用[data-max-length]时,通过单次的按键即可快速改变数字的正负号.
添加属性[data-disable-smart-minus]以禁用这种快速取反机制.

<input type="text" data-max-length="-5">
<input type="text" data-max-length="-5" data-disable-smart-minus>

[data-disable-init-refresh]

All input elements with max length configuration will trigger all its enabled functions when document is ready.
Add the attribute [data-disable-init-refresh] to disable this initial fresh process.

启用了[data-max-length]的输入框会在页面加载完成时刷新以加载其已经被配置的功能.
添加属性[data-disable-init-refresh]以在页面加载完成时防止这种刷新.

<input type="text" data-max-length="5.2" value="12345">
<input type="text" data-max-length="5.2" value="12345" data-disable-init-refresh>

[data-highlight-minus]

Add the attribute [data-highlight-minus="[$hex]"] to enable highlighting of negative values.
Uses default red(#FF0000) color if the hexadecimal color is not assigned.

添加属性[data-highlight-minus="[$hex]"]以在数值为负时高亮显示.
如果未指定十六进制颜色,则使用红色(#FF0000)为默认高亮颜色.

<input type="text" data-max-length="-5" value="-12345" data-highlight-minus>
<input type="text" data-max-length="-5" value="-12345" data-highlight-minus="#0099FF">

[data-horizontal-align]

All input elements with max length configuration will be aligned right side when not focused.
Add the attribute [data-horizontal-align="[$align]"] to customize text align position.

启用了[data-max-length]的输入框中的内容会在未聚焦时靠右显示.
添加属性[data-horizontal-align="[$align]"]以改变未聚焦时的显示位置.

<input type="text" data-max-length="5" data-horizontal-align="left">
<input type="text" data-max-length="5" data-horizontal-align="center">

[data-sum] / [data-product]

Add the attribute [data-sum="[$selector]"] / [data-product="[$selector]"] to enable quick sum or product calculation on DOM elements matched by the jQuery selector.

添加属性[data-sum="[$selector]"] / [data-product="[$selector]"]以通过jQuery选择器进行快速求和或求积操作.

<input type="text" class="sumTarget" data-max-length="-5.2">
<input type="text" class="sumTarget" data-max-length="-5.2">
<input type="text" class="sumTarget" data-max-length="-5.2">
<input type="text" data-sum="input.sumTarget" data-max-length="-6.2">
<input type="text" class="productTarget" data-max-length="-3.1">
<input type="text" class="productTarget" data-max-length="-3.1">
<input type="text" class="productTarget" data-max-length="-3.1">
<input type="text" data-product="input.productTarget" data-max-length="-9.3">

[data-difference] / [data-quotient]

Add the attribute [data-difference="[$minuendSelector],[$subtrahendSelector]"] / [data-quotient="[$dividendSelector],[$divisorSelector]"] to enable quick difference or quotient calculation on DOM elements matched by the jQuery selector.

添加属性[data-difference="[$minuendSelector],[$subtrahendSelector]"] / [data-quotient="[$dividendSelector],[$divisorSelector]"]以通过jQuery选择器进行快速求差或求商操作.

<input type="text" id="minuend" data-max-length="-5.2">
<input type="text" class="subtrahend" data-max-length="-5.2">
<input type="text" data-difference="#minuend,.subtrahend" data-max-length="-6.2">	

There can be multiple subtrahends/divisors, but only 1 minuend/dividend.

减数/除数可以有多个,但被减数/被除数只能有一个.

<input type="text" id="dividend" data-max-length="-5">
<input type="text" class="divisor" data-max-length="-2">
<input type="text" class="divisor" data-max-length="-2">
<input type="text" data-quotient="#dividend,.divisor" data-max-length="-5.2">

[data-ceil] / [data-floor]

Add the attribute [data-ceil] / [data-floor] to change rounding method in auto calculation functions.

添加属性[data-ceil] / [data-floor]以改变在自动计算功能中的取整方式.

<input type="text" class="ceilSumTarget" data-max-length="-5.2">
<input type="text" data-sum="input.ceilSumTarget" data-ceil data-max-length="-5">
<input type="text" class="floorSumTarget" data-max-length="-5.2">
<input type="text" data-sum="input.floorSumTarget" data-floor data-max-length="-5">

$.NumberUtil

$.NumberUtil is an extended jQuery decimal calculating utility for use.

$.NumberUtil中包含了若干用于计算的方法以供使用.

$.NumberUtil.
->

Number Format Globalization Support

数字格式全球化支持

Automatically read your attribute [lang] of your [html] tag to select the proper number format.
If fail, then read the language of user's browser. For code consistency, please remain using ['.'] as decimal point in attribute [data-max-length].

该插件会自动根据[html][lang]标签选择合格的数字格式.
如果获取不到,则会尝试获取用户浏览器的语言来进行选择.
但为了代码一致性,无论您所在的地区使用哪种数字格式,都请在[data-max-length]的配置参数中使用['.']作为小数点.

<html lang="en">
	~
	<input type="text" data-max-length="-5.2" value="-12,345.00">
	~
</html>
<html lang="es">
	~
	<input type="text" data-max-length="-5.2" value="-12.345,00">
	~
</html>
<html lang="fi">
	~
	<input type="text" data-max-length="-5.2" value="-12 345.00">
	~
</html>

If that doesn't match the correct format, you can invoke $.NumberUtil.setNumberFormatStandard() in your ready event to select format from ["ISO", "EN", "ES"].

如果自动选择的结果与预期不符,可以在页面加载时通过调用$.NumberUtil.setNumberFormatStandard()方法以从["ISO", "EN", "ES"]中选择一项作为当前页面的数字格式.

$(document).ready(() => {
	$.NumberUtil.setNumberFormatStandard('EN');
});