jquery.inputmask.numeric.extensions.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*!
  2. * jquery.inputmask.numeric.extensions.js
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2015 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.1.64-20
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "jquery", "./jquery.inputmask" ], factory) : "object" == typeof exports ? module.exports = factory(require("jquery"), require("./jquery.inputmask")) : factory(jQuery);
  10. }(function($) {
  11. return $.extend($.inputmask.defaults.aliases, {
  12. numeric: {
  13. mask: function(opts) {
  14. function autoEscape(txt) {
  15. for (var escapedTxt = "", i = 0; i < txt.length; i++) escapedTxt += opts.definitions[txt[i]] ? "\\" + txt[i] : txt[i];
  16. return escapedTxt;
  17. }
  18. if (0 !== opts.repeat && isNaN(opts.integerDigits) && (opts.integerDigits = opts.repeat),
  19. opts.repeat = 0, opts.groupSeparator == opts.radixPoint && ("." == opts.radixPoint ? opts.groupSeparator = "," : "," == opts.radixPoint ? opts.groupSeparator = "." : opts.groupSeparator = ""),
  20. " " === opts.groupSeparator && (opts.skipOptionalPartCharacter = void 0), opts.autoGroup = opts.autoGroup && "" != opts.groupSeparator,
  21. opts.autoGroup && ("string" == typeof opts.groupSize && isFinite(opts.groupSize) && (opts.groupSize = parseInt(opts.groupSize)),
  22. isFinite(opts.integerDigits))) {
  23. var seps = Math.floor(opts.integerDigits / opts.groupSize), mod = opts.integerDigits % opts.groupSize;
  24. opts.integerDigits = parseInt(opts.integerDigits) + (0 == mod ? seps - 1 : seps);
  25. }
  26. opts.placeholder.length > 1 && (opts.placeholder = opts.placeholder.charAt(0)),
  27. opts.radixFocus = opts.radixFocus && "0" == opts.placeholder, opts.definitions[";"] = opts.definitions["~"],
  28. opts.definitions[";"].definitionSymbol = "~";
  29. var mask = autoEscape(opts.prefix);
  30. return mask += "[+]", mask += "~{1," + opts.integerDigits + "}", void 0 != opts.digits && (isNaN(opts.digits) || parseInt(opts.digits) > 0) && (mask += opts.digitsOptional ? "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}]" : (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}"),
  31. "" != opts.negationSymbol.back && (mask += "[-]"), mask += autoEscape(opts.suffix),
  32. opts.greedy = !1, mask;
  33. },
  34. placeholder: "",
  35. greedy: !1,
  36. digits: "*",
  37. digitsOptional: !0,
  38. groupSeparator: "",
  39. radixPoint: ".",
  40. radixFocus: !0,
  41. groupSize: 3,
  42. autoGroup: !1,
  43. allowPlus: !0,
  44. allowMinus: !0,
  45. negationSymbol: {
  46. front: "-",
  47. back: ""
  48. },
  49. integerDigits: "+",
  50. prefix: "",
  51. suffix: "",
  52. rightAlign: !0,
  53. decimalProtect: !0,
  54. min: void 0,
  55. max: void 0,
  56. postFormat: function(buffer, pos, reformatOnly, opts) {
  57. var suffixStripped = !1;
  58. buffer.length >= opts.suffix.length && buffer.join("").indexOf(opts.suffix) == buffer.length - opts.suffix.length && (buffer.length = buffer.length - opts.suffix.length,
  59. suffixStripped = !0), pos = pos >= buffer.length ? buffer.length - 1 : pos < opts.prefix.length ? opts.prefix.length : pos;
  60. var needsRefresh = !1, charAtPos = buffer[pos];
  61. if ("" == opts.groupSeparator || -1 != $.inArray(opts.radixPoint, buffer) && pos > $.inArray(opts.radixPoint, buffer) || new RegExp("[" + $.inputmask.escapeRegex(opts.negationSymbol.front) + "+]").test(charAtPos)) {
  62. if (suffixStripped) for (var i = 0, l = opts.suffix.length; l > i; i++) buffer.push(opts.suffix.charAt(i));
  63. return {
  64. pos: pos
  65. };
  66. }
  67. var cbuf = buffer.slice();
  68. charAtPos == opts.groupSeparator && (cbuf.splice(pos--, 1), charAtPos = cbuf[pos]),
  69. reformatOnly ? charAtPos != opts.radixPoint && (cbuf[pos] = "?") : cbuf.splice(pos, 0, "?");
  70. var bufVal = cbuf.join(""), bufValOrigin = bufVal;
  71. if (bufVal.length > 0 && opts.autoGroup || reformatOnly && -1 != bufVal.indexOf(opts.groupSeparator)) {
  72. var escapedGroupSeparator = $.inputmask.escapeRegex(opts.groupSeparator);
  73. needsRefresh = 0 == bufVal.indexOf(opts.groupSeparator), bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  74. var radixSplit = bufVal.split(opts.radixPoint);
  75. if (bufVal = "" == opts.radixPoint ? bufVal : radixSplit[0], bufVal != opts.prefix + "?0" && bufVal.length >= opts.groupSize + opts.prefix.length) for (var reg = new RegExp("([-+]?[\\d?]+)([\\d?]{" + opts.groupSize + "})"); reg.test(bufVal); ) bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2"),
  76. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  77. "" != opts.radixPoint && radixSplit.length > 1 && (bufVal += opts.radixPoint + radixSplit[1]);
  78. }
  79. needsRefresh = bufValOrigin != bufVal, buffer.length = bufVal.length;
  80. for (var i = 0, l = bufVal.length; l > i; i++) buffer[i] = bufVal.charAt(i);
  81. var newPos = $.inArray("?", buffer);
  82. if (-1 == newPos && (newPos = pos), reformatOnly ? buffer[newPos] = charAtPos : buffer.splice(newPos, 1),
  83. !needsRefresh && suffixStripped) for (var i = 0, l = opts.suffix.length; l > i; i++) buffer.push(opts.suffix.charAt(i));
  84. return {
  85. pos: newPos,
  86. refreshFromBuffer: needsRefresh,
  87. buffer: buffer
  88. };
  89. },
  90. onBeforeWrite: function(e, buffer, caretPos, opts) {
  91. if (e && "blur" == e.type) {
  92. var maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
  93. if (processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  94. "," === opts.radixPoint && (processValue = processValue.replace($.inputmask.escapeRegex(opts.radixPoint), ".")),
  95. isFinite(processValue) && isFinite(opts.min) && parseFloat(processValue) < parseFloat(opts.min)) return $.extend(!0, {
  96. refreshFromBuffer: !0,
  97. buffer: (opts.prefix + opts.min).split("")
  98. }, opts.postFormat((opts.prefix + opts.min).split(""), 0, !0, opts));
  99. var tmpBufSplit = "" != opts.radixPoint ? buffer.join("").split(opts.radixPoint) : [ buffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
  100. !matchRslt || matchRslt[0] != opts.negationSymbol.front + "0" && matchRslt[0] != opts.negationSymbol.front && "+" != matchRslt[0] || void 0 != matchRsltDigits && !matchRsltDigits[0].match(/^0+$/) || buffer.splice(matchRslt.index, 1);
  101. var radixPosition = $.inArray(opts.radixPoint, buffer);
  102. if (-1 != radixPosition && isFinite(opts.digits) && !opts.digitsOptional) {
  103. for (var i = 1; i <= opts.digits; i++) (void 0 == buffer[radixPosition + i] || buffer[radixPosition + i] == opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
  104. return {
  105. refreshFromBuffer: !0,
  106. buffer: buffer
  107. };
  108. }
  109. }
  110. if (opts.autoGroup) {
  111. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  112. return rslt.caret = caretPos <= opts.prefix.length ? rslt.pos : rslt.pos + 1, rslt;
  113. }
  114. },
  115. regex: {
  116. integerPart: function(opts) {
  117. return new RegExp("[" + $.inputmask.escapeRegex(opts.negationSymbol.front) + "+]?\\d+");
  118. },
  119. integerNPart: function(opts) {
  120. return new RegExp("[\\d" + $.inputmask.escapeRegex(opts.groupSeparator) + "]+");
  121. }
  122. },
  123. signHandler: function(chrs, maskset, pos, strict, opts) {
  124. if (!strict && opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs) {
  125. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  126. if (matchRslt && matchRslt[0].length > 0) return maskset.buffer[matchRslt.index] == ("-" === chrs ? "+" : opts.negationSymbol.front) ? "-" == chrs ? "" != opts.negationSymbol.back ? {
  127. pos: matchRslt.index,
  128. c: opts.negationSymbol.front,
  129. remove: matchRslt.index,
  130. caret: pos,
  131. insert: {
  132. pos: maskset.buffer.length - opts.suffix.length - 1,
  133. c: opts.negationSymbol.back
  134. }
  135. } : {
  136. pos: matchRslt.index,
  137. c: opts.negationSymbol.front,
  138. remove: matchRslt.index,
  139. caret: pos
  140. } : "" != opts.negationSymbol.back ? {
  141. pos: matchRslt.index,
  142. c: "+",
  143. remove: [ matchRslt.index, maskset.buffer.length - opts.suffix.length - 1 ],
  144. caret: pos
  145. } : {
  146. pos: matchRslt.index,
  147. c: "+",
  148. remove: matchRslt.index,
  149. caret: pos
  150. } : maskset.buffer[matchRslt.index] == ("-" === chrs ? opts.negationSymbol.front : "+") ? "-" == chrs && "" != opts.negationSymbol.back ? {
  151. remove: [ matchRslt.index, maskset.buffer.length - opts.suffix.length - 1 ],
  152. caret: pos - 1
  153. } : {
  154. remove: matchRslt.index,
  155. caret: pos - 1
  156. } : "-" == chrs ? "" != opts.negationSymbol.back ? {
  157. pos: matchRslt.index,
  158. c: opts.negationSymbol.front,
  159. caret: pos + 1,
  160. insert: {
  161. pos: maskset.buffer.length - opts.suffix.length,
  162. c: opts.negationSymbol.back
  163. }
  164. } : {
  165. pos: matchRslt.index,
  166. c: opts.negationSymbol.front,
  167. caret: pos + 1
  168. } : {
  169. pos: matchRslt.index,
  170. c: chrs,
  171. caret: pos + 1
  172. };
  173. }
  174. return !1;
  175. },
  176. radixHandler: function(chrs, maskset, pos, strict, opts) {
  177. if (!strict && chrs === opts.radixPoint && opts.digits > 0) {
  178. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  179. if (-1 != radixPos && maskset.validPositions[radixPos]) return maskset.validPositions[radixPos - 1] ? {
  180. caret: radixPos + 1
  181. } : {
  182. pos: integerValue.index,
  183. c: integerValue[0],
  184. caret: radixPos + 1
  185. };
  186. if (!integerValue || "0" == integerValue[0] && integerValue.index + 1 != pos) return maskset.buffer[integerValue ? integerValue.index : pos] = "0",
  187. {
  188. pos: (integerValue ? integerValue.index : pos) + 1
  189. };
  190. }
  191. return !1;
  192. },
  193. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  194. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  195. if (matchRslt && !strict && (-1 == radixPosition || radixPosition >= pos)) if (0 == matchRslt[0].indexOf("0")) {
  196. pos < opts.prefix.length && (pos = matchRslt.index);
  197. var _radixPosition = $.inArray(opts.radixPoint, maskset._buffer), digitsMatch = maskset._buffer && maskset.buffer.slice(radixPosition).join("") == maskset._buffer.slice(_radixPosition).join("") || 0 == parseInt(maskset.buffer.slice(radixPosition + 1).join("")), integerMatch = maskset._buffer && maskset.buffer.slice(matchRslt.index, radixPosition).join("") == maskset._buffer.slice(opts.prefix.length, _radixPosition).join("") || "0" == maskset.buffer.slice(matchRslt.index, radixPosition).join("");
  198. if (-1 == radixPosition || digitsMatch && integerMatch) return maskset.buffer.splice(matchRslt.index, 1),
  199. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  200. pos: pos,
  201. remove: matchRslt.index
  202. };
  203. if (matchRslt.index + 1 == pos || "0" == chrs) return maskset.buffer.splice(matchRslt.index, 1),
  204. pos = matchRslt.index, {
  205. pos: pos,
  206. remove: matchRslt.index
  207. };
  208. } else if ("0" === chrs && pos <= matchRslt.index && matchRslt[0] != opts.groupSeparator) return !1;
  209. return !0;
  210. },
  211. postValidation: function(buffer, opts) {
  212. var isValid = !0, maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
  213. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  214. "," === opts.radixPoint && (processValue = processValue.replace($.inputmask.escapeRegex(opts.radixPoint), ".")),
  215. processValue = processValue.replace(new RegExp("^" + $.inputmask.escapeRegex(opts.negationSymbol.front)), "-"),
  216. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex(opts.negationSymbol.back) + "$"), ""),
  217. isFinite(processValue) && isFinite(opts.max) && (isValid = parseFloat(processValue) <= parseFloat(opts.max)),
  218. isValid;
  219. },
  220. definitions: {
  221. "~": {
  222. validator: function(chrs, maskset, pos, strict, opts) {
  223. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  224. if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts),
  225. !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs),
  226. isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts),
  227. isValid === !0)))) {
  228. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  229. isValid = opts.digitsOptional === !1 && pos > radixPosition && !strict ? {
  230. pos: pos,
  231. remove: pos
  232. } : {
  233. pos: pos
  234. };
  235. }
  236. return isValid;
  237. },
  238. cardinality: 1,
  239. prevalidator: null
  240. },
  241. "+": {
  242. validator: function(chrs, maskset, pos, strict, opts) {
  243. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  244. return !isValid && (strict && opts.allowMinus && chrs === opts.negationSymbol.front || opts.allowMinus && "-" == chrs || opts.allowPlus && "+" == chrs) && (isValid = "-" == chrs ? "" != opts.negationSymbol.back ? {
  245. pos: pos,
  246. c: "-" === chrs ? opts.negationSymbol.front : "+",
  247. caret: pos + 1,
  248. insert: {
  249. pos: maskset.buffer.length,
  250. c: opts.negationSymbol.back
  251. }
  252. } : {
  253. pos: pos,
  254. c: "-" === chrs ? opts.negationSymbol.front : "+",
  255. caret: pos + 1
  256. } : !0), isValid;
  257. },
  258. cardinality: 1,
  259. prevalidator: null,
  260. placeholder: ""
  261. },
  262. "-": {
  263. validator: function(chrs, maskset, pos, strict, opts) {
  264. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  265. return !isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back && (isValid = !0),
  266. isValid;
  267. },
  268. cardinality: 1,
  269. prevalidator: null,
  270. placeholder: ""
  271. },
  272. ":": {
  273. validator: function(chrs, maskset, pos, strict, opts) {
  274. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  275. if (!isValid) {
  276. var radix = "[" + $.inputmask.escapeRegex(opts.radixPoint) + "]";
  277. isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {
  278. caret: pos + 1
  279. });
  280. }
  281. return isValid;
  282. },
  283. cardinality: 1,
  284. prevalidator: null,
  285. placeholder: function(opts) {
  286. return opts.radixPoint;
  287. }
  288. }
  289. },
  290. insertMode: !0,
  291. autoUnmask: !1,
  292. unmaskAsNumber: !1,
  293. onUnMask: function(maskedValue, unmaskedValue, opts) {
  294. var processValue = maskedValue.replace(opts.prefix, "");
  295. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  296. opts.unmaskAsNumber ? (processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."),
  297. Number(processValue)) : processValue;
  298. },
  299. isComplete: function(buffer, opts) {
  300. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  301. if (opts.postFormat(bufClone, 0, !0, opts), bufClone.join("") != maskedValue) return !1;
  302. var processValue = maskedValue.replace(opts.prefix, "");
  303. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  304. "," === opts.radixPoint && (processValue = processValue.replace($.inputmask.escapeRegex(opts.radixPoint), ".")),
  305. isFinite(processValue);
  306. },
  307. onBeforeMask: function(initialValue, opts) {
  308. if ("" != opts.radixPoint && isFinite(initialValue)) initialValue = initialValue.toString().replace(".", opts.radixPoint); else {
  309. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  310. dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  311. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""),
  312. initialValue = initialValue.replace(".", opts.radixPoint)) : initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "") : initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  313. }
  314. return 0 == opts.digits && (-1 != initialValue.indexOf(".") ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : -1 != initialValue.indexOf(",") && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))),
  315. initialValue;
  316. },
  317. canClearPosition: function(maskset, position, lvp, strict, opts) {
  318. var positionInput = maskset.validPositions[position].input, canClear = positionInput != opts.radixPoint && isFinite(positionInput) || position == lvp || positionInput == opts.groupSeparator || positionInput == opts.negationSymbol.front || positionInput == opts.negationSymbol.back;
  319. if (canClear && isFinite(positionInput)) {
  320. var matchRslt;
  321. if (!strict && maskset.buffer) {
  322. matchRslt = maskset.buffer.join("").substr(0, position).match(opts.regex.integerNPart(opts));
  323. var pos = position + 1, isNull = null == matchRslt || 0 == parseInt(matchRslt[0].replace(new RegExp($.inputmask.escapeRegex(opts.groupSeparator), "g"), ""));
  324. if (isNull) for (;maskset.validPositions[pos] && (maskset.validPositions[pos].input == opts.groupSeparator || "0" == maskset.validPositions[pos].input); ) delete maskset.validPositions[pos],
  325. pos++;
  326. }
  327. var buffer = [];
  328. for (var vp in maskset.validPositions) buffer.push(maskset.validPositions[vp].input);
  329. matchRslt = buffer.join("").match(opts.regex.integerNPart(opts));
  330. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  331. if (matchRslt && (-1 == radixPosition || radixPosition >= position)) if (0 == matchRslt[0].indexOf("0")) canClear = matchRslt.index != position || -1 == radixPosition; else {
  332. var intPart = parseInt(matchRslt[0].replace(new RegExp($.inputmask.escapeRegex(opts.groupSeparator), "g"), ""));
  333. -1 != radixPosition && 10 > intPart && (maskset.validPositions[position].input = "0",
  334. maskset.p = opts.prefix.length + 1, canClear = !1);
  335. }
  336. }
  337. return canClear;
  338. }
  339. },
  340. currency: {
  341. prefix: "$ ",
  342. groupSeparator: ",",
  343. alias: "numeric",
  344. placeholder: "0",
  345. autoGroup: !0,
  346. digits: 2,
  347. digitsOptional: !1,
  348. clearMaskOnLostFocus: !1
  349. },
  350. decimal: {
  351. alias: "numeric"
  352. },
  353. integer: {
  354. alias: "numeric",
  355. digits: "0",
  356. radixPoint: ""
  357. }
  358. }), $.fn.inputmask;
  359. });