jquery.inputmask.numeric.extensions.js 23 KB

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