简易数字和货币格式库 autoNumeric

  • 源码大小:410.04KB
  • 所需积分:1积分
  • 源码编号:19JP-3104
  • 浏览次数:794次
  • 最后更新:2023年05月06日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

autoNumeric是一个用于国际货币格式和数字格式的有用JavaScript库。

它几乎支持欧洲、北美和南美、亚洲和印度使用的所有主要国际数字格式和货币符号。

您可能还喜欢:

  • 带有jQuery的简易数字格式插件-数字
  • 输入字段数据格式插件-掩码
  • 价格格式jQuery插件-价格格式

如何使用它:

1.安装和导入。

  1. # Yarn
  2. $ yarn add autonumeric
  3.  
  4. # NPM
  5. $ npm install autonumeric --save
  1. import AutoNumeric from 'autonumeric';

2.或者在网页上包含最新的autoNumeric.js

  1. <script src="/path/to/autoNumeric.js"></script>
  2. <!-- from a cdn -->
  3. <script src="//unpkg.com/autonumeric"></script>

3.在页面上创建一个普通输入字段。

  1. <form>
  2. <input type="text" class="demo">
  3. </form>

4.初始化autoNumeric库并指定货币符号。完成。

  1. new AutoNumeric('.demo', { currencySymbol : '$' });

5.所有选项和默认值。您还可以通过HTML5数据属性传递以下选项。

  1. /* Defines if the decimal places should be padded with zeroes
  2. * `true` : always pad decimals with zeros (ie. '12.3400')
  3. * `false` : never pad with zeros (ie. '12.34')
  4. * `'floats'` : pad with zeroes only when there are decimals (ie. '12' and '12.3400')
  5. * Note: setting allowDecimalPadding to 'false' will override the 'decimalPlaces' setting.
  6. */
  7. allowDecimalPadding: {
  8. always: true,
  9. never : false,
  10. floats: 'floats',
  11. },
  12.  
  13. /* Defines if the decimal character or decimal character alternative should be accepted when there is already a decimal character shown in the element.
  14. * If set to `true`, any decimal character input will be accepted and will subsequently modify the decimal character position, as well as the `rawValue`.
  15. * If set to `false`, the decimal character and its alternative key will be dropped as before. This is the default setting.
  16. */
  17. alwaysAllowDecimalCharacter: {
  18. alwaysAllow: true,
  19. doNotAllow: false,
  20. },
  21.  
  22. /* Defines where should be positioned the caret on focus
  23. * null : Do not enforce any caret positioning on focus (this is needed when using `selectOnFocus`)
  24. * `'start'` : put the caret of the far left side of the value (excluding the positive/negative sign and currency symbol, if any)
  25. * `'end'` : put the caret of the far right side of the value (excluding the positive/negative sign and currency symbol, if any)
  26. * `'decimalLeft'` : put the caret of the left of the decimal character if any
  27. * `'decimalRight'` : put the caret of the right of the decimal character if any
  28. */
  29. caretPositionOnFocus: {
  30. start : 'start',
  31. end : 'end',
  32. decimalLeft : 'decimalLeft',
  33. decimalRight : 'decimalRight',
  34. doNoForceCaretPosition: null,
  35. },
  36.  
  37. /* Defines if a local list of AutoNumeric objects should be kept when initializing this object.
  38. * This list is used by the `global.*` functions.
  39. */
  40. createLocalList: {
  41. createList : true,
  42. doNotCreateList: false,
  43. },
  44.  
  45. /* Defines the currency symbol string.
  46. * It can be a string of more than one character (allowing for instance to use a space on either side of it, example: '$ ' or ' $')
  47. * cf. https://en.wikipedia.org/wiki/Currency_symbol
  48. */
  49. currencySymbol: {
  50. none : '',
  51. currencySign : '¤',
  52. austral : '₳', // ARA
  53. australCentavo: '¢',
  54. baht : '฿', // THB
  55. cedi : '₵', // GHS
  56. cent : '¢',
  57. colon : '₡', // CRC
  58. cruzeiro : '₢', // BRB - Not used anymore since 1993
  59. dollar : '$',
  60. dong : '₫', // VND
  61. drachma : '₯', // GRD (or 'Δρχ.' or 'Δρ.')
  62. dram : '​֏', // AMD
  63. european : '₠', // XEU (old currency before the Euro)
  64. euro : '€', // EUR
  65. florin : 'ƒ',
  66. franc : '₣', // FRF
  67. guarani : '₲', // PYG
  68. hryvnia : '₴', // грн
  69. kip : '₭', // LAK
  70. att : 'ອັດ', // cents of the Kip
  71. lepton : 'Λ.', // cents of the Drachma
  72. lira : '₺', // TRY
  73. liraOld : '₤',
  74. lari : '₾', // GEL
  75. mark : 'ℳ',
  76. mill : '₥',
  77. naira : '₦', // NGN
  78. peseta : '₧',
  79. peso : '₱', // PHP
  80. pfennig : '₰', // cents of the Mark
  81. pound : '£',
  82. real : 'R$ ', // Brazilian real
  83. riel : '៛', // KHR
  84. ruble : '₽', // RUB
  85. rupee : '₹', // INR
  86. rupeeOld : '₨',
  87. shekel : '₪',
  88. shekelAlt : 'ש״ח‎‎',
  89. taka : 'à§³', // BDT
  90. tenge : '₸', // KZT
  91. togrog : '₮', // MNT
  92. won : '₩',
  93. yen : 'Â¥',
  94. },
  95.  
  96. /* Defines where the currency symbol should be placed (before of after the numbers)
  97. * for prefix currencySymbolPlacement: "p" (default)
  98. * for suffix currencySymbolPlacement: "s"
  99. */
  100. currencySymbolPlacement: {
  101. prefix: 'p',
  102. suffix: 's',
  103. },
  104.  
  105. /* Defines what decimal separator character is used
  106. */
  107. decimalCharacter: {
  108. comma : ',',
  109. dot : '.',
  110. middleDot : '·',
  111. arabicDecimalSeparator : 'Ù«',
  112. decimalSeparatorKeySymbol: '⎖',
  113. },
  114.  
  115. /* Allow to declare an alternative decimal separator which is automatically replaced by `decimalCharacter` when typed.
  116. * This is used by countries that use a comma ',' as the decimal character and have keyboards with a numeric pads that have
  117. * a period 'full stop' as the decimal character (France or Spain for instance).
  118. */
  119. decimalCharacterAlternative: {
  120. none : null,
  121. comma: ',',
  122. dot : '.',
  123. },
  124.  
  125. /* Defines the default number of decimal places to show on the formatted value, and keep for the precision.
  126. * Incidentally, since we need to be able to show that many decimal places, this also defines the raw value precision by default.
  127. */
  128. decimalPlaces: {
  129. none : 0,
  130. one : 1,
  131. two : 2,
  132. three: 3,
  133. four : 4,
  134. five : 5,
  135. six : 6,
  136. },
  137.  
  138. /* Defines how many decimal places should be kept for the raw value (ie. This is the precision for float values).
  139. *
  140. * If this option is set to `null` (which is the default), then the value of `decimalPlaces` is used for `decimalPlacesRawValue` as well.
  141. * Note: Setting this to a lower number of decimal places than the one to be shown will lead to confusion for the users.
  142. */
  143. decimalPlacesRawValue: {
  144. useDefault: null,
  145. none : 0,
  146. one : 1,
  147. two : 2,
  148. three : 3,
  149. four : 4,
  150. five : 5,
  151. six : 6,
  152. },
  153.  
  154. /* Defines how many decimal places should be visible when the element is unfocused.
  155. * If this is set to `null`, then this option is ignored, and the `decimalPlaces` option value will be used instead.
  156. * This means this is optional ; if omitted the decimal places will be the same when the input has the focus.
  157. *
  158. * This option can be used in conjonction with the two other `scale*` options, which allows to display a different formatted value when the element is unfocused, while another formatted value is shown when focused.
  159. * For those `scale*` options to have any effect, `divisorWhenUnfocused` must not be `null`.
  160. */
  161. decimalPlacesShownOnBlur: {
  162. useDefault: null,
  163. none : 0,
  164. one : 1,
  165. two : 2,
  166. three : 3,
  167. four : 4,
  168. five : 5,
  169. six : 6,
  170. },
  171.  
  172. /* Defines how many decimal places should be visible when the element has the focus.
  173. * If this is set to `null`, then this option is ignored, and the `decimalPlaces` option value will be used instead.
  174. *
  175. * Example:
  176. * For instance if `decimalPlacesShownOnFocus` is set to `5` and the default number of decimal places is `2`, then on focus `1,000.12345` will be shown, while without focus `1,000.12` will be set back.
  177. * Note 1: the results depends on the rounding method used.
  178. * Note 2: the `getNumericString()` method returns the extended decimal places
  179. */
  180. decimalPlacesShownOnFocus: {
  181. useDefault: null,
  182. none : 0,
  183. one : 1,
  184. two : 2,
  185. three : 3,
  186. four : 4,
  187. five : 5,
  188. six : 6,
  189. },
  190.  
  191. /* Helper option for ASP.NET postback
  192. * This should be set as the value of the unformatted default value
  193. * examples:
  194. * no default value="" {defaultValueOverride: ""}
  195. * value=1234.56 {defaultValueOverride: '1234.56'}
  196. */
  197. defaultValueOverride: {
  198. doNotOverride: null,
  199. },
  200.  
  201. /* Defines how many numbers should be grouped together (usually for the thousand separator)
  202. * - "2", results in 99,99,99,999 India's lakhs
  203. * - "2s", results in 99,999,99,99,999 India's lakhs scaled
  204. * - "3", results in 999,999,999 (default)
  205. * - "4", results in 9999,9999,9999 used in some Asian countries
  206. * Note: This option does not accept other grouping choice.
  207. */
  208. digitalGroupSpacing: {
  209. two : '2',
  210. twoScaled: '2s',
  211. three : '3',
  212. four : '4',
  213. },
  214.  
  215. /* Defines the thousand grouping separator character
  216. * Example : If `'.'` is set, then you'll get `'1.234.567'`
  217. */
  218. digitGroupSeparator: {
  219. comma : ',',
  220. dot : '.',
  221. normalSpace : ' ',
  222. thinSpace : '\u2009',
  223. narrowNoBreakSpace : '\u202f',
  224. noBreakSpace : '\u00a0',
  225. noSeparator : '',
  226. apostrophe : `'`,
  227. arabicThousandsSeparator: 'Ù¬',
  228. dotAbove : '˙',
  229. privateUseTwo : '’', // \u0092
  230. },
  231.  
  232. /* The `divisorWhenUnfocused` divide the element value on focus.
  233. * On blur, the element value is multiplied back.
  234. *
  235. * Example : Display percentages using { divisorWhenUnfocused: 100 } (or directly in the Html with `<input data-divisor-when-unfocused="100">`)
  236. * The divisor value does not need to be an integer, but please understand that Javascript has limited accuracy in math ; use with caution.
  237. * Note: The `getNumericString` method returns the full value, including the 'hidden' decimals.
  238. */
  239. divisorWhenUnfocused: {
  240. none : null,
  241. percentage: 100,
  242. permille : 1000,
  243. basisPoint: 10000,
  244. },
  245.  
  246. /* Defines what should be displayed in the element if the raw value is an empty string ('').
  247. * - 'focus' : The currency sign is displayed when the input receives focus (default)
  248. * - 'press' : The currency sign is displayed whenever a key is being pressed
  249. * - 'always' : The currency sign is always displayed
  250. * - 'zero' : A zero is displayed ('rounded' with or without a currency sign) if the input has no value on focus out
  251. * - 'min' : The minimum value is displayed if the input has no value on focus out
  252. * - 'max' : The maximum value is displayed if the input has no value on focus out
  253. * - 'null' : When the element is empty, the `rawValue` and the element value/text is set to `null`. This also allows to set the value to `null` using `anElement.set(null)`.
  254. */
  255. emptyInputBehavior: {
  256. focus : 'focus',
  257. press : 'press',
  258. always: 'always',
  259. zero : 'zero',
  260. min : 'min',
  261. max : 'max',
  262. null : 'null',
  263. },
  264.  
  265. /* Defines if the custom and native events triggered by AutoNumeric should bubble up or not.
  266. */
  267. eventBubbles: {
  268. bubbles: true,
  269. doesNotBubble: false,
  270. },
  271.  
  272. /* Defines if the custom and native events triggered by AutoNumeric should be cancelable.
  273. */
  274. eventIsCancelable: {
  275. isCancelable: true,
  276. isNotCancelable: false,
  277. },
  278.  
  279. /* This option is the 'strict mode' (aka 'debug' mode), which allows autoNumeric to strictly analyse the options passed, and fails if an unknown options is used in the settings object.
  280. * You should set that to `true` if you want to make sure you are only using 'pure' autoNumeric settings objects in your code.
  281. * If you see uncaught errors in the console and your code starts to fail, this means somehow those options gets polluted by another program (which usually happens when using frameworks).
  282. */
  283. failOnUnknownOption: {
  284. fail : true,
  285. ignore: false,
  286. },
  287.  
  288. /* Determine if the default value will be formatted on initialization.
  289. */
  290. formatOnPageLoad: {
  291. format : true, // automatically formats the default value on initialization
  292. doNotFormat: false, // will not format the default value on initialization
  293. },
  294.  
  295. /* Defines if the 'formula mode' can be activated by the user.
  296. * If set to `true`, then the user can enter the formula mode by entering the '=' character.
  297. * He will then be allowed to enter any simple math formula using numeric characters as well as the following operators +, -, *, /, ( and ).
  298. * The formula mode is closed when the user either validate their math expression using the `Enter` key, or when the element is blurred.
  299. * If the formula is invalid, the previous valid `rawValue` is set back, and the `autoNumeric:invalidFormula` event is sent.
  300. * When a valid formula is accepted, then its result is `set()`, and the `autoNumeric:validFormula` event is sent.
  301. *
  302. * By default, this mode is disabled.
  303. */
  304. formulaMode: {
  305. enabled : true,
  306. disabled: false,
  307. },
  308.  
  309. /* Set the undo/redo history table size.
  310. * Each record keeps the raw value as well and the last known caret/selection positions.
  311. */
  312. historySize: {
  313. verySmall: 5,
  314. small : 10,
  315. medium : 20,
  316. large : 50,
  317. veryLarge: 100,
  318. insane : Number.MAX_SAFE_INTEGER,
  319. },
  320.  
  321. /* Defines the name of the CSS class to use on contenteditable-enabled elements when the value is invalid
  322. * This is not used when the HTML element used is an input.
  323. */
  324. invalidClass: 'an-invalid',
  325.  
  326. /* Allow the user to 'cancel' and undo the changes he made to the given autonumeric-managed element, by pressing the 'Escape' key.
  327. * Whenever the user 'validate' the input (either by hitting 'Enter', or blurring the element), the new value is saved for subsequent 'cancellation'.
  328. *
  329. * The process :
  330. * - save the input value on focus
  331. * - if the user change the input value, and hit `Escape`, then the initial value saved on focus is set back
  332. * - on the other hand if the user either have used `Enter` to validate (`Enter` throws a change event) his entries, or if the input value has been changed by another script in the mean time, then we save the new input value
  333. * - on a successful 'cancel', select the whole value (while respecting the `selectNumberOnly` option)
  334. * - bonus; if the value has not changed, hitting 'Esc' just select all the input value (while respecting the `selectNumberOnly` option)
  335. */
  336. isCancellable: {
  337. cancellable : true,
  338. notCancellable: false,
  339. },
  340.  
  341. /* Controls the leading zero behavior
  342. * - 'allow' : allows leading zeros to be entered. Zeros will be truncated when entering additional digits. On focusout zeros will be deleted
  343. * - 'deny' : allows only one leading zero on values that are between 1 and -1
  344. * - 'keep' : allows leading zeros to be entered. on focusout zeros will be retained
  345. */
  346. leadingZero: {
  347. allow: 'allow',
  348. deny : 'deny',
  349. keep : 'keep',
  350. },
  351.  
  352. /* Defines the maximum possible value a user can enter.
  353. * Notes:
  354. * - this value must be a string and use the period for the decimal point
  355. * - this value needs to be larger than `minimumValue`
  356. */
  357. maximumValue: {
  358. tenTrillions : '10000000000000', // 10000 billions
  359. oneBillion : '1000000000',
  360. zero : '0',
  361. },
  362.  
  363. /* Defines the minimum possible value a user can enter.
  364. * Notes:
  365. * - this value must be a string and use the period for the decimal point
  366. * - this value needs to be smaller than `maximumValue`
  367. * - if this is superior to 0, then you'll effectively prevent your user to entirely delete the content of your element
  368. */
  369. minimumValue: {
  370. tenTrillions : '-10000000000000', // 10000 billions
  371. oneBillion : '-1000000000',
  372. zero : '0',
  373. },
  374.  
  375. /* Allows the user to increment or decrement the element value with the up and down arrow keys.
  376. * The behavior is similar to the mouse wheel one.
  377. * The up and down arrow keys behavior can be modified by the `upDownStep` option.
  378. * This `upDownStep` option can be used in two ways, either by setting:
  379. * - a 'fixed' step value (`upDownStep : 1000`), or
  380. * - the 'progressive' string (`upDownStep : 'progressive'`), which will then activate a special mode where the step is automatically calculated based on the element value size.
  381. */
  382. modifyValueOnUpDownArrow: {
  383. modifyValue: true,
  384. doNothing : false,
  385. },
  386.  
  387. /* Allows the user to increment or decrement the element value with the mouse wheel.
  388. * The behavior is similar to the up/down arrow one.
  389. * The wheel behavior can be modified by the `wheelStep` option.
  390. * This `wheelStep` option can be used in two ways, either by setting:
  391. * - a 'fixed' step value (`wheelStep : 1000`), or
  392. * - the 'progressive' string (`wheelStep : 'progressive'`), which will then activate a special mode where the step is automatically calculated based on the element value size.
  393. *
  394. * Note :
  395. * You can activate/deactivate the wheel event for each `wheelOn` option value by using the 'Shift' modifier key while using the mouse wheel.
  396. */
  397. modifyValueOnWheel: {
  398. modifyValue: true,
  399. doNothing : false,
  400. },
  401.  
  402. /* Adds brackets on negative values (ie. transforms '-$ 999.99' to '($999.99)')
  403. * Those brackets are visible only when the field does NOT have the focus.
  404. * The left and right symbols should be enclosed in quotes and separated by a comma.
  405. */
  406. negativeBracketsTypeOnBlur: {
  407. parentheses : '(,)',
  408. brackets : '[,]',
  409. chevrons : '<,>',
  410. curlyBraces : '{,}',
  411. angleBrackets : '〈,〉',
  412. japaneseQuotationMarks: 'ï½¢,ï½£',
  413. halfBrackets : '⸤,⸥',
  414. whiteSquareBrackets : '⟦,⟧',
  415. quotationMarks : '‹,›',
  416. guillemets : '«,»',
  417. none : null, // This is the default value, which deactivate this feature
  418. },
  419.  
  420. /* Placement of the negative/positive sign relative to the `currencySymbol` option.
  421. *
  422. * Example:
  423. * // Default values
  424. * -1,234.56 => default no options required
  425. * $-1,234.56 => {currencySymbol: "$", negativePositiveSignPlacement: "r"} // Default if negativePositiveSignPlacement is 'null' and currencySymbol is not empty
  426. *
  427. * // Sign on the left hand side of the whole number
  428. * -$1,234.56 => {currencySymbol: "$"} or {currencySymbol: "$", negativePositiveSignPlacement: "l"}
  429. * -1,234.56$ => {currencySymbol: "$", currencySymbolPlacement: "s", negativePositiveSignPlacement: "p"} // Default if negativePositiveSignPlacement is 'null' and currencySymbol is not empty
  430. *
  431. * // Sign on the right hand side of the whole number
  432. * 1,234.56- => {negativePositiveSignPlacement: "s"}
  433. * $1,234.56- => {currencySymbol: "$", negativePositiveSignPlacement: "s"}
  434. * 1,234.56-$ => {currencySymbol: "$", currencySymbolPlacement: "s"}
  435. * 1,234.56$- => {currencySymbol: "$", currencySymbolPlacement: "s", negativePositiveSignPlacement: "r"}
  436. */
  437. negativePositiveSignPlacement: {
  438. prefix: 'p',
  439. suffix: 's',
  440. left : 'l',
  441. right : 'r',
  442. none : null,
  443. },
  444.  
  445. /* Defines the negative sign symbol.
  446. * It can be a string of only one character.
  447. */
  448. negativeSignCharacter: {
  449. hyphen : '-',
  450. minus : '−',
  451. heavyMinus : '➖',
  452. fullWidthHyphen: '-',
  453. circledMinus : '⊖',
  454. squaredMinus : '⊟',
  455. triangleMinus : '⨺',
  456. plusMinus : '±',
  457. minusPlus : '∓',
  458. dotMinus : '∸',
  459. minusTilde : '≂',
  460. not : '¬',
  461. },
  462.  
  463. /*
  464. * Defines if the negative sign should be toggled when hitting the negative or positive key multiple times.
  465. * When `toggle` is used, using the same '-' on '+' key will toggle between a positive and negative value.
  466. * When `doNotToggle` is used, using '-' will always set the value negative, and '+' will always set the value positive.
  467. */
  468. negativePositiveSignBehavior: {
  469. toggle : true,
  470. doNotToggle: false,
  471. },
  472.  
  473. /* Defines if the element should have event listeners activated on it.
  474. * By default, those event listeners are only added to <input> elements and html element with the `contenteditable` attribute set to `true`, but not on the other html tags.
  475. * This allows to initialize elements without any event listeners.
  476. * Warning: Since AutoNumeric will not check the input content after its initialization, using some autoNumeric methods afterwards *will* probably leads to formatting problems.
  477. */
  478. noEventListeners: {
  479. noEvents : true,
  480. addEvents: false,
  481. },
  482.  
  483. /* Manage how autoNumeric react when the user tries to paste an invalid number.
  484. * - 'error' : (This is the default behavior) The input value is not changed and an error is output in the console.
  485. * - 'ignore' : idem than 'error', but fail silently without outputting any error/warning in the console.
  486. * - 'clamp' : if the pasted value is either too small or too big regarding the minimumValue and maximumValue range, then the result is clamped to those limits.
  487. * - 'truncate' : autoNumeric will insert as many pasted numbers it can at the initial caret/selection, until everything is pasted, or the range limit is hit.
  488. * The non-pasted numbers are dropped and therefore not used at all.
  489. * - 'replace' : autoNumeric will first insert as many pasted numbers it can at the initial caret/selection, then if the range limit is hit, it will try
  490. * to replace one by one the remaining initial numbers (on the right side of the caret) with the rest of the pasted numbers.
  491. *
  492. * Note 1 : A paste content starting with a negative sign '-' will be accepted anywhere in the input, and will set the resulting value as a negative number
  493. * Note 2 : A paste content starting with a number will be accepted, even if the rest is gibberish (ie. '123foobar456').
  494. * Only the first number will be used (here '123').
  495. * Note 3 : The paste event works with the `decimalPlacesShownOnFocus` option too.
  496. */
  497. onInvalidPaste: {
  498. error : 'error',
  499. ignore : 'ignore',
  500. clamp : 'clamp',
  501. truncate: 'truncate',
  502. replace : 'replace',
  503. },
  504.  
  505. /* Defines how the value should be formatted when wanting a 'localized' version of it.
  506. * - null or 'string' => 'nnnn.nn' or '-nnnn.nn' as text type. This is the default behavior.
  507. * - 'number' => nnnn.nn or -nnnn.nn as a Number (Warning: this works only for integers inferior to Number.MAX_SAFE_INTEGER)
  508. * - ',' or '-,' => 'nnnn,nn' or '-nnnn,nn'
  509. * - '.-' => 'nnnn.nn' or 'nnnn.nn-'
  510. * - ',-' => 'nnnn,nn' or 'nnnn,nn-'
  511. *
  512. * Note: The hyphen '-' is translated to the custom negative sign defined in `negativeSignCharacter`
  513. */
  514. outputFormat: {
  515. string : 'string',
  516. number : 'number',
  517. dot : '.',
  518. negativeDot : '-.',
  519. comma : ',',
  520. negativeComma: '-,',
  521. dotNegative : '.-',
  522. commaNegative: ',-',
  523. none : null,
  524. },
  525.  
  526. /* Defines if AutoNumeric should let the user override the minimum and/or maximum limits when he types numbers in the element.
  527. * - 'ceiling' Strictly adheres to `maximumValue` and ignores the `minimumValue` settings
  528. * It allows the user to enter anything between -∞ `and maximumValue`
  529. * If `maximumValue` is less than 0, then it will prevent the user emptying the field or typing value above `maximumValue`, making sure the value entered is always valid
  530. * - 'floor' Strictly adheres to `minimumValue` and ignores the `maximumValue` settings
  531. * It allows the user to enter anything between `minimumValue` and +∞
  532. * If `minimumValue` is higher than 0, then it will prevent the user emptying the field or typing value below `minimumValue`, making sure the value entered is always valid
  533. * - 'ignore' Ignores both the `minimumValue` and `maximumValue` settings
  534. * When using this option, the field will always be valid range-wise
  535. * - 'invalid' The user can temporarily type out-of-bound values. In doing so, the invalid state is set on the field.
  536. * Whenever an invalid value is typed, an 'autoNumeric:invalidValue' event is sent
  537. * When the value is correctly set back within the limit boundaries, the invalid state is removed, and the 'autoNumeric:correctedValue' event is sent
  538. * - 'doNotOverride' Strictly adheres to the `maximumValue` and `minimumValue` settings
  539. * This is the default behavior
  540. * If `0` is out of the min/max range, this will prevent the user clearing the input field, making sure the value entered is always valid
  541. */
  542. overrideMinMaxLimits: {
  543. ceiling : 'ceiling',
  544. floor : 'floor',
  545. ignore : 'ignore',
  546. invalid : 'invalid',
  547. doNotOverride: null,
  548. },
  549.  
  550. /* Defines the positive sign symbol.
  551. * It can be a string of only one character.
  552. * This is shown only if `showPositiveSign` is set to `true`.
  553. */
  554. positiveSignCharacter: {
  555. plus : '+',
  556. fullWidthPlus : '+',
  557. heavyPlus : '➕',
  558. doublePlus : '⧺',
  559. triplePlus : 'â§»',
  560. circledPlus : '⊕',
  561. squaredPlus : '⊞',
  562. trianglePlus : '⨹',
  563. plusMinus : '±',
  564. minusPlus : '∓',
  565. dotPlus : '∔',
  566. altHebrewPlus : '﬩',
  567. normalSpace : ' ',
  568. thinSpace : '\u2009',
  569. narrowNoBreakSpace: '\u202f',
  570. noBreakSpace : '\u00a0',
  571. },
  572.  
  573. /* The `rawValueDivisor` divides the formatted value shown in the AutoNumeric element and store the result in `rawValue`.
  574. * @example { rawValueDivisor: '100' } or <input data-raw-value-divisor="100">
  575. * Given the `0.01234` raw value, the formatted value will be displayed as `'1.234'`.
  576. * This is useful when displaying percentage for instance, and avoid the need to divide/multiply by 100 between the number shown and the raw value.
  577. */
  578. rawValueDivisor: {
  579. none : null,
  580. percentage: 100,
  581. permille : 1000,
  582. basisPoint: 10000,
  583. },
  584.  
  585. /* Defines if the element (`<input>` or another allowed html tag) should be set as read-only on initialization.
  586. * When set to `true`, then:
  587. * - the `readonly` html property is added to the <input> element on initialization, or
  588. * - the `contenteditable` attribute is set to `false` on non-input elements.
  589. */
  590. readOnly: {
  591. readOnly : true,
  592. readWrite: false,
  593. },
  594.  
  595. /* Defines the rounding method to use.
  596. * roundingMethod: "S", Round-Half-Up Symmetric (default)
  597. * roundingMethod: "A", Round-Half-Up Asymmetric
  598. * roundingMethod: "s", Round-Half-Down Symmetric (lower case s)
  599. * roundingMethod: "a", Round-Half-Down Asymmetric (lower case a)
  600. * roundingMethod: "B", Round-Half-Even "Bankers Rounding"
  601. * roundingMethod: "U", Round Up "Round-Away-From-Zero"
  602. * roundingMethod: "D", Round Down "Round-Toward-Zero" - same as truncate
  603. * roundingMethod: "C", Round to Ceiling "Toward Positive Infinity"
  604. * roundingMethod: "F", Round to Floor "Toward Negative Infinity"
  605. * roundingMethod: "N05" Rounds to the nearest .05 => same as "CHF" used in 1.9X and still valid
  606. * roundingMethod: "U05" Rounds up to next .05
  607. * roundingMethod: "D05" Rounds down to next .05
  608. */
  609. roundingMethod: {
  610. halfUpSymmetric : 'S',
  611. halfUpAsymmetric : 'A',
  612. halfDownSymmetric : 's',
  613. halfDownAsymmetric : 'a',
  614. halfEvenBankersRounding : 'B',
  615. upRoundAwayFromZero : 'U',
  616. downRoundTowardZero : 'D',
  617. toCeilingTowardPositiveInfinity: 'C',
  618. toFloorTowardNegativeInfinity : 'F',
  619. toNearest05 : 'N05',
  620. toNearest05Alt : 'CHF',
  621. upToNext05 : 'U05',
  622. downToNext05 : 'D05',
  623. },
  624.  
  625. /* Set to `true` to allow the `decimalPlacesShownOnFocus` value to be saved with sessionStorage
  626. * If IE 6 or 7 is detected, the value will be saved as a session cookie.
  627. */
  628. saveValueToSessionStorage: {
  629. save : true,
  630. doNotSave: false,
  631. },
  632.  
  633. /* Determine if the select all keyboard command will select the complete input text, or only the input numeric value
  634. * Note : If the currency symbol is between the numeric value and the negative sign, only the numeric value will be selected
  635. */
  636. selectNumberOnly: {
  637. selectNumbersOnly: true,
  638. selectAll : false,
  639. },
  640.  
  641. /* Defines if the element value should be selected on focus.
  642. * Note: The selection is done using the `selectNumberOnly` option.
  643. */
  644. selectOnFocus: {
  645. select : true,
  646. doNotSelect: false,
  647. },
  648.  
  649. /* Defines how the serialize functions should treat the spaces.
  650. * Those spaces ' ' can either be converted to the plus sign '+', which is the default, or to '%20'.
  651. * Both values being valid per the spec (http://www.w3.org/Addressing/URL/uri-spec.html).
  652. * Also see the summed up answer on http://stackoverflow.com/a/33939287.
  653. *
  654. * tl;dr : Spaces should be converted to '%20' before the '?' sign, then converted to '+' after.
  655. * In our case since we serialize the query, we use '+' as the default (but allow the user to get back the old *wrong* behavior).
  656. */
  657. serializeSpaces: {
  658. plus : '+',
  659. percent: '%20',
  660. },
  661.  
  662. /* Defines if the element value should be converted to the raw value on focus (and back to the formatted on blur).
  663. * If set to `true`, then autoNumeric remove the thousand separator, currency symbol and suffix on focus.
  664. * Example:
  665. * If the input value is '$ 1,999.88 suffix', on focus it becomes '1999.88' and back to '$ 1,999.88 suffix' on blur.
  666. */
  667. showOnlyNumbersOnFocus: {
  668. onlyNumbers: true,
  669. showAll : false,
  670. },
  671.  
  672. /* Allow the positive sign symbol `+` to be displayed for positive numbers.
  673. * By default, this positive sign is not shown.
  674. * The sign placement is controlled by the 'negativePositiveSignPlacement' option, mimicking the negative sign placement rules.
  675. */
  676. showPositiveSign: {
  677. show: true,
  678. hide: false,
  679. },
  680.  
  681. /* Defines if warnings should be shown in the console.
  682. * Those warnings can be ignored, but are usually printed when something could be improved by the user (ie. option conflicts).
  683. */
  684. showWarnings: {
  685. show: true, // All warning are shown
  686. hide: false, // No warnings are shown, only the thrown errors
  687. },
  688.  
  689. /* Defines the rules that calculate the CSS class(es) to apply on the element, based on the raw unformatted value.
  690. * This can also be used to call callbacks whenever the `rawValue` is updated.
  691. * Important: all callbacks must return `null` if no ranges/userDefined classes are selected
  692. * @example
  693. * {
  694. * positive : 'autoNumeric-positive', // Or `null` to not use it
  695. * negative : 'autoNumeric-negative',
  696. * ranges : [
  697. * { min: 0, max: 25, class: 'autoNumeric-red' },
  698. * { min: 25, max: 50, class: 'autoNumeric-orange' },
  699. * { min: 50, max: 75, class: 'autoNumeric-yellow' },
  700. * { min: 75, max: Number.MAX_SAFE_INTEGER, class: 'autoNumeric-green' },
  701. * ],
  702. * userDefined: [
  703. * // If 'classes' is a string, set it if `true`, remove it if `false`
  704. * { callback: rawValue => { return true; }, classes: 'thisIsTrue' },
  705. * // If 'classes' is an array with only 2 elements, set the first class if `true`, the second if `false`
  706. * { callback: rawValue => rawValue % 2 === 0, classes: ['autoNumeric-even', 'autoNumeric-odd'] },
  707. * // Return only one index to use on the `classes` array (here, 'class3')
  708. * { callback: rawValue => { return 2; }, classes: ['class1', 'class2', 'class3'] },
  709. * // Return an array of indexes to use on the `classes` array (here, 'class1' and 'class3')
  710. * { callback: rawValue => { return [0, 2]; }, classes: ['class1', 'class2', 'class3'] },
  711. * // If 'classes' is `undefined` or `null`, then the callback is called with the AutoNumeric object passed as a parameter
  712. * { callback: anElement => { return anElement.getFormatted(); } },
  713. * ],
  714. * }
  715. */
  716. styleRules: {
  717. none : null,
  718. positiveNegative : {
  719. positive: 'autoNumeric-positive',
  720. negative: 'autoNumeric-negative',
  721. },
  722. range0To100With4Steps: {
  723. ranges: [
  724. { min: 0, max: 25, class: 'autoNumeric-red' },
  725. { min: 25, max: 50, class: 'autoNumeric-orange' },
  726. { min: 50, max: 75, class: 'autoNumeric-yellow' },
  727. { min: 75, max: 100, class: 'autoNumeric-green' },
  728. ],
  729. },
  730. evenOdd : {
  731. userDefined: [
  732. { callback: rawValue => rawValue % 2 === 0, classes: ['autoNumeric-even', 'autoNumeric-odd'] },
  733. ],
  734. },
  735. rangeSmallAndZero : {
  736. userDefined: [
  737. {
  738. callback : rawValue => {
  739. if (rawValue >= -1 && rawValue < 0) {
  740. return 0;
  741. }
  742. if (Number(rawValue) === 0) {
  743. return 1;
  744. }
  745. if (rawValue > 0 && rawValue <= 1) {
  746. return 2;
  747. }
  748.  
  749. return null; // In case the rawValue is outside those ranges
  750. },
  751. classes: [
  752. 'autoNumeric-small-negative',
  753. 'autoNumeric-zero',
  754. 'autoNumeric-small-positive',
  755. ],
  756. },
  757. ],
  758. },
  759. },
  760.  
  761. /* Add a text on the right hand side of the element value.
  762. * This suffix text can have any characters in its string, except numeric characters and the negative/positive sign.
  763. * Example: ' dollars'
  764. */
  765. suffixText: {
  766. none : '',
  767. percentage: '%',
  768. permille : '‰',
  769. basisPoint: '‱',
  770. },
  771.  
  772. /* The three options (divisorWhenUnfocused, decimalPlacesShownOnBlur & symbolWhenUnfocused) handle scaling of the input when the input does not have focus
  773. * Please note that the non-scaled value is held in data and it is advised that you use the `saveValueToSessionStorage` option to ensure retaining the value
  774. * ["divisor", "decimal places", "symbol"]
  775. * Example: with the following options set {divisorWhenUnfocused: '1000', decimalPlacesShownOnBlur: '1', symbolWhenUnfocused: ' K'}
  776. * Example: focusin value "1,111.11" focusout value "1.1 K"
  777. */
  778.  
  779. /* The `symbolWhenUnfocused` option is a symbol placed as a suffix when not in focus.
  780. * This is optional too.
  781. */
  782. symbolWhenUnfocused: {
  783. none : null,
  784. percentage: '%',
  785. permille : '‰',
  786. basisPoint: '‱',
  787. },
  788.  
  789. /* Defines if the element value should be unformatted when the user hover his mouse over it while holding the `Alt` key.
  790. * Unformatting there means that this removes any non-number characters and displays the *raw* value, as understood by Javascript (ie. `12.34` is a valid number, while `12,34` is not).
  791. *
  792. * We reformat back before anything else if :
  793. * - the user focus on the element by tabbing or clicking into it,
  794. * - the user releases the `Alt` key, and
  795. * - if we detect a mouseleave event.
  796. *
  797. * We unformat again if :
  798. * - while the mouse is over the element, the user hit `Alt` again
  799. */
  800. unformatOnHover: {
  801. unformat : true,
  802. doNotUnformat: false, //TODO Rename to `keepFormat`
  803. },
  804.  
  805. /* Removes the formatting and use the raw value in each autoNumeric elements of the parent form element, on the form `submit` event.
  806. * The output format is a numeric string (nnnn.nn or -nnnn.nn).
  807. */
  808. unformatOnSubmit: {
  809. unformat : true,
  810. keepCurrentValue: false,
  811. },
  812.  
  813. /* That option is linked to the `modifyValueOnUpDownArrow` one and will only be used if the latter is set to `true`.
  814. * This option will modify the up/down arrow behavior and can be used in two ways, either by setting :
  815. * - a 'fixed' step value (a positive float or integer number (ex: `1000`)), or
  816. * - the `'progressive'` string.
  817. *
  818. * The 'fixed' mode always increment/decrement the element value by that amount, while respecting the `minimumValue` and `maximumValue` settings.
  819. * The 'progressive' mode will increment/decrement the element value based on its current value. The bigger the number, the bigger the step, and vice versa.
  820. */
  821. upDownStep: {
  822. progressive: 'progressive',
  823. },
  824.  
  825. /* Provides a way for automatically replacing the formatted value with a pre-defined string, when the raw value is equal to a specific value
  826. * Here you can specify as many 'conversion' as needed.
  827. */
  828. valuesToStrings: {
  829. none : null,
  830. zeroDash : {
  831. 0: '-',
  832. },
  833. oneAroundZero: {
  834. '-1': 'Min',
  835. 1 : 'Max',
  836. },
  837. },
  838.  
  839. /* Defines if the AutoNumeric element should watch external changes made without using `.set()`, but by using the basic `aNElement.node().value = 42` notation.
  840. * If set to `watch`, then AutoNumeric will format the new value using `.set()` internally.
  841. * Otherwise it will neither format it, nor save it in the history.
  842. */
  843. watchExternalChanges: {
  844. watch : true,
  845. doNotWatch: false,
  846. },
  847.  
  848. /* Defines when the wheel event will increment or decrement the element value.
  849. * When set to `'focus'`, the AutoNumeric-managed element needs to be focused for the wheel event to change the value.
  850. * When set to `'hover'`, using the wheel event while the mouse is hovering the element is sufficient (no focus needed).
  851. *
  852. * Note :
  853. * When `wheelOn` is set to `'focus'`, you can use the 'Shift' modifier key while using the mouse wheel in order to temporarily activate the increment/decrement feature even if the element is not focused.
  854. * When `wheelOn` is set to `'hover'`, you can use the 'Shift' modifier key while using the mouse wheel in order to temporarily disable the increment/decrement feature even if the element is not hovered.
  855. */
  856. wheelOn: {
  857. focus: 'focus',
  858. hover: 'hover',
  859. },
  860.  
  861. /* That option is linked to the `modifyValueOnWheel` one and will only be used if the latter is set to `true`.
  862. * This option will modify the wheel behavior and can be used in two ways, either by setting :
  863. * - a 'fixed' step value (a positive float or integer (ex: number `1000`)), or
  864. * - the `'progressive'` string.
  865. *
  866. * The 'fixed' mode always increment/decrement the element value by that amount, while respecting the `minimumValue` and `maximumValue` settings.
  867. * The 'progressive' mode will increment/decrement the element value based on its current value. The bigger the number, the bigger the step, and vice versa.
  868. */
  869. wheelStep: {
  870. progressive: 'progressive',
  871. },

更新日志:

第4.8.2版(2023-04-04)

  • 调用_historyTableAdd()函数时避免“强制回流”

第4.8.1版(2023-03-24)

  • 添加功能:不允许用“+”和“-”键切换正负号。这为管理“-”和“+”键行为添加了一个新选项negativePositiveSignBehavior
  • 更新依赖于切换行为的现有端到端测试
  • 修复设置无效的positiveSignCharacter选项时出现的错误消息
  • 尝试修复Travis CI中的“node:18”错误
  • 修复了最小化分发文件中的版权横幅未定义的问题
  • 从package.json中删除未使用的wdio脚本
  • 默认情况下,-和+键将不再切换AutoNumeric元素的负/正状态。
  • 此版本中的默认行为已更改,使用“-”或“+”键在正值和负值之间切换不会被激活,而是-将尝试将值设置为负值,+将尝试将其设置为正值(如果结果值超出范围,则此操作可能失败)。
  • 如果要使用以前的行为,请在选项配置中设置negativePositiveSignBehavior:AutoNumeric.options.negativePositiveSignBehavior。toggle。

版本4.7.0(2023-03-21)

  • 错误修复程序
  • 添加两个新选项来控制上下箭头键的行为:modifyValueOnUpDownArrow和upDownStep

第4.6.3版(2023-03-18)

  • 错误修复程序

版本4.6.2(2023-03-07)

  • 错误修复程序

版本4.6.1(2023-03-07)

  • 更新所有依赖项
  • 删除PhantomJS依赖项以支持Puppeteer
  • 完成validate()函数,并以数字形式对新的allowDecimalPadding选项进行额外测试
  • 将默认导出添加到模块
  • 错误修复程序

版本4.6.0(2022-02-14)

  • 允许用户输入超出“minimumValue”和“maximumValue”范围的绑定数字

版本4.5.13(2020-02-16)

  • 已修复Bug

第4.5.9版(2019-10-12)

  • 修正了当前缀位置的currencySymbol包含输入的第一个值(即数值)时,Caret位置设置不正确

第4.5.8版(2019-10-08)

  • 错误修复

第4.5.7版(2019-09-13)

  • 更新的错误修复(&B)

第4.5.4版(2019-01-14)

  • 修复了缺少\u0092数字组分隔符的问题
  • 修复了freezeOptions()可能在某些浏览器中产生问题的问题

第4.5.2版(2019-01-6)

  • 已修复:当连续按Backspace或Delete键清除输入字段时,应触发autoNumeric:格式化的事件

第4.5.1版(2018年10月28日)

  • 已修复:初始加载时忽略html只读属性
  • 修复只读和禁用输入不应处理键盘事件的问题
  • 修复公式模式,以便使用带有decimalCharacter的自定义十进制字符集,而不是默认的“”写入浮点数时的字符

第4.5.0版(2018年10月14日)

  • 输入=12*78等表达式时允许进行基本计算
  • 添加formulaMode选项,默认设置为false,用于控制是否启用公式模式
  • 添加Lexer、Parser、Evaluator、ASTNode和Token类,用于管理数学表达式
  • 修复Dist文件中包含eval的问题
  • 合并4.4.1中的更改,同时确保没有与#609的回归;这将在package.json中的主字段旁边添加浏览器字段。注意:浏览器选项指向缩小的库dist/autoNumeric.min.js
  • 更新index.html测试文件以使用未缩小的开发库dist/autoNumeric.js这允许在调试时临时使用禁用的函数,如控制台或错误的格式,使用yarn build:dev
  • 修复了对_reformatAltHoverd()的调用,即使unfmatOnHover选项设置为false

版本4.4.3(2018-09-24)

  • 已修复:设置为false时不使用unfmatOnHover配置值

第4.4.2版(2018-09-15)

  • 修复了未捕获的错误:在v4.4.1上找不到模块“autonumeric”

版本4.4.1(2018-09-13)

  • 将package.json配置主字段修改为浏览器

v4.4.0版(2018年8月04日)

  • 添加一个功能,其中emptyInputBehavior可以设置为最小值或最大值,emptyInput Behavior现在接受数字(或表示数字的字符串),或“min”或“max”选项
  • 修复检查值限制时的emptyInputBehavior验证测试
  • 允许emptyInputBehavior设置为min、max或数字

版本4.3.7(2018-07-28)

  • 修正:键入减号后预设欧元/法语的空字段的货币位置错误
  • 修正了:在负号BracketsTypeOnBlur输入中输入单个减号会反转货币符号和减号

版本4.3.6(2018-07-27)

  • 已修复:重新打开的“表单重置错误”
  • AutoNumeric现在侦听父窗体上的重置事件,并在检测到时做出相应反应

第4.3.4版(2018年7月26日)

  • 已修复:根据光标移动不触发更改事件

第4.3.3版(2018年7月24日)

  • 修正了将负值粘贴到负值上,该负值作为货币符号及其选定的数字会引发错误
  • 通过删除重复的部分来重构_onPast()处理程序
  • 修复初始化调用new AutoNumeric()时,将字符串(元素选择器)、数字(初始值)和数组(选项数组)按特定顺序用作参数将无法识别的问题

第4.3.1版(2018-07-24)

  • 精确地修改10万亿和10亿的限额
  • 删除maximumValue和minimumValue tenTrillionsNoDecimals选项,并更新tenTrillions,使其等于10万亿
  • 10万亿次期权现在等于“10000000000000”,10亿次期权现在相当于“1000000000”
  • 自从我们在v4.0.0-beta.22中修改了用户如何使用decimalPlaces选项定义小数位数,而不是在最小/最大值中添加特定的小数位数以来,早就应该进行此更改了。

版本4.3.0(2018-06-02)

  • 允许带有readOnly选项的非输入标记将contenteditable属性设置为false
  • 修复只读属性和内容可编辑属性,以便将它们更新为读/写模式
  • 修改_setRadOnly(),使其始终将元素设置为只读模式
  • 创建一个新的_setRadWrite()函数,将元素设置为读写模式
  • 创建一个新的_setWritePermissions()函数,根据readOnly设置设置元素的只读/写模式
  • 目前作为一个纯JAVASCRIPT库工作。
  • 已修复-选项更新时未设置/重置事件侦听器。
  • 修正了冻结选项时Internet Explorer 11抛出的问题

预览截图