inputmask.numeric.extensions.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*!
  2. * 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.2.6-26
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "inputmask.dependencyLib", "inputmask" ], factory) : "object" == typeof exports ? module.exports = factory(require("./inputmask.dependencyLib.jquery"), require("./inputmask")) : factory(window.dependencyLib || jQuery, window.Inputmask);
  10. }(function($, Inputmask) {
  11. return Inputmask.extendAliases({
  12. numeric2: {
  13. mask: function(opts) {
  14. function autoEscape(txt) {
  15. for (var escapedTxt = "", i = 0; i < txt.length; i++) escapedTxt += opts.definitions[txt.charAt(i)] ? "\\" + txt.charAt(i) : txt.charAt(i);
  16. return escapedTxt;
  17. }
  18. 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. var seps = "+", mod = 0;
  22. opts.autoGroup && ("string" == typeof opts.groupSize && isFinite(opts.groupSize) && (opts.groupSize = parseInt(opts.groupSize)),
  23. isFinite(opts.integerDigits) && (seps = Math.floor(opts.integerDigits / opts.groupSize),
  24. mod = opts.integerDigits % opts.groupSize)), opts.placeholder.length > 1 && (opts.placeholder = opts.placeholder.charAt(0)),
  25. opts.jitMasking === !0 && opts.placeholder.length > 0 && (opts.jitMasking = 1),
  26. opts.radixFocus = opts.radixFocus && "" !== opts.placeholder && opts.integerOptional === !0,
  27. opts.definitions[";"] = opts.definitions["~"], opts.definitions[";"].definitionSymbol = "~";
  28. var mask = autoEscape(opts.prefix);
  29. return mask += "[+]", opts.autoGroup ? (mask += "(" + opts.groupSeparator + "~{" + opts.groupSize + "}){" + seps + "}",
  30. mod > 0 && (mask += "~{" + mod + "}")) : mask += "~{" + opts.integerDigits + "}",
  31. void 0 !== opts.digits && (isNaN(opts.digits) || parseInt(opts.digits) > 0) && (opts.digitsOptional ? mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{1," + opts.digits + "}]" : (mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}",
  32. isFinite(opts.jitMasking) && opts.jitMasking < 1 + opts.radixPoint.length + (isFinite(opts.digits) ? opts.digits : 2) && (opts.jitMasking = 1 + opts.radixPoint.length + (isFinite(opts.digits) ? opts.digits : 2)))),
  33. "" !== opts.negationSymbol.back && (mask += "[-]"), mask += autoEscape(opts.suffix),
  34. opts.greedy = !1, mask;
  35. },
  36. placeholder: "",
  37. greedy: !1,
  38. digits: "*",
  39. digitsOptional: !0,
  40. radixPoint: ".",
  41. radixFocus: !0,
  42. groupSize: 3,
  43. groupSeparator: "",
  44. autoGroup: !1,
  45. allowPlus: !0,
  46. allowMinus: !0,
  47. negationSymbol: {
  48. front: "-",
  49. back: ""
  50. },
  51. integerDigits: "+",
  52. integerOptional: !0,
  53. prefix: "",
  54. suffix: "",
  55. rightAlign: !0,
  56. decimalProtect: !0,
  57. min: null,
  58. max: null,
  59. step: 1,
  60. insertMode: !0,
  61. autoUnmask: !1,
  62. unmaskAsNumber: !1,
  63. jitMasking: !0,
  64. numericInput: !0,
  65. onBeforeWrite: function(e, buffer, caretPos, opts) {
  66. if (e && ("blur" === e.type || "checkval" === e.type)) {
  67. var maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
  68. if (processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  69. "," === opts.radixPoint && (processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".")),
  70. isFinite(processValue) && isFinite(opts.min) && parseFloat(processValue) < parseFloat(opts.min)) return {
  71. refreshFromBuffer: !0,
  72. buffer: (opts.prefix + opts.min).split("")
  73. };
  74. 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;
  75. if (matchRslt) {
  76. 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);
  77. var radixPosition = $.inArray(opts.radixPoint, buffer);
  78. if (-1 !== radixPosition) {
  79. if (isFinite(opts.digits) && !opts.digitsOptional) {
  80. for (var i = 1; i <= opts.digits; i++) (void 0 === buffer[radixPosition + i] || buffer[radixPosition + i] === opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
  81. return {
  82. refreshFromBuffer: maskedValue !== buffer.join(""),
  83. buffer: buffer
  84. };
  85. }
  86. if (radixPosition === buffer.length - opts.suffix.length - 1) return buffer.splice(radixPosition, 1),
  87. {
  88. refreshFromBuffer: !0,
  89. buffer: buffer
  90. };
  91. }
  92. }
  93. }
  94. },
  95. regex: {
  96. integerPart: function(opts) {
  97. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + "]+[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]?$");
  98. },
  99. integerNPart: function(opts) {
  100. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + "]+");
  101. }
  102. },
  103. signHandler: function(chrs, maskset, pos, strict, opts) {
  104. return !1;
  105. },
  106. radixHandler: function(chrs, maskset, pos, strict, opts) {
  107. return !1;
  108. },
  109. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  110. return !0;
  111. },
  112. postValidation: function(buffer, opts) {
  113. var isValid = !0, maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
  114. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  115. "," === opts.radixPoint && (processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".")),
  116. processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-"),
  117. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), ""),
  118. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue,
  119. isFinite(processValue) && (null !== opts.max && isFinite(opts.max) && (isValid = parseFloat(processValue) <= parseFloat(opts.max)),
  120. isValid && null !== opts.min && isFinite(opts.min) && (0 >= processValue || processValue.toString().length >= opts.min.toString().length) && (isValid = parseFloat(processValue) >= parseFloat(opts.min),
  121. isValid || (isValid = {
  122. refreshFromBuffer: !0,
  123. buffer: (opts.prefix + opts.min).split("")
  124. }))), isValid;
  125. },
  126. definitions: {
  127. "~": {
  128. validator: function(chrs, maskset, pos, strict, opts) {
  129. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  130. if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts),
  131. !isValid && (isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs),
  132. isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts),
  133. isValid === !0)))) {
  134. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  135. -1 !== radixPosition && opts.digitsOptional === !1 && pos > radixPosition + 1 && !strict && (isValid = {
  136. pos: pos - 1,
  137. remove: pos - 1
  138. });
  139. }
  140. return isValid;
  141. },
  142. cardinality: 1,
  143. prevalidator: null
  144. },
  145. "+": {
  146. validator: function(chrs, maskset, pos, strict, opts) {
  147. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  148. return !isValid && (strict && opts.allowMinus && chrs === opts.negationSymbol.front || opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs) && (isValid = "-" === chrs ? "" !== opts.negationSymbol.back ? {
  149. pos: pos,
  150. c: "-" === chrs ? opts.negationSymbol.front : "+",
  151. caret: pos + 1,
  152. insert: {
  153. pos: maskset.buffer.length,
  154. c: opts.negationSymbol.back
  155. }
  156. } : {
  157. pos: pos,
  158. c: "-" === chrs ? opts.negationSymbol.front : "+",
  159. caret: pos + 1
  160. } : !0), isValid;
  161. },
  162. cardinality: 1,
  163. prevalidator: null,
  164. placeholder: ""
  165. },
  166. "-": {
  167. validator: function(chrs, maskset, pos, strict, opts) {
  168. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  169. return !isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back && (isValid = !0),
  170. isValid;
  171. },
  172. cardinality: 1,
  173. prevalidator: null,
  174. placeholder: ""
  175. },
  176. ":": {
  177. validator: function(chrs, maskset, pos, strict, opts) {
  178. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  179. if (!isValid) {
  180. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + ",\\.]";
  181. isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint && (isValid = {
  182. caret: pos + 1
  183. });
  184. }
  185. return isValid ? {
  186. c: opts.radixPoint
  187. } : isValid;
  188. },
  189. cardinality: 1,
  190. prevalidator: null,
  191. placeholder: function(opts) {
  192. return opts.radixPoint;
  193. }
  194. }
  195. },
  196. onUnMask: function(maskedValue, unmaskedValue, opts) {
  197. var processValue = maskedValue.replace(opts.prefix, "");
  198. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  199. opts.unmaskAsNumber ? ("" !== opts.radixPoint && -1 !== processValue.indexOf(opts.radixPoint) && (processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".")),
  200. Number(processValue)) : processValue;
  201. },
  202. isComplete: function(buffer, opts) {
  203. var maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
  204. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  205. "," === opts.radixPoint && (processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".")),
  206. isFinite(processValue);
  207. },
  208. onBeforeMask: function(initialValue, opts) {
  209. if ("" !== opts.radixPoint && isFinite(initialValue)) initialValue = initialValue.toString().replace(".", opts.radixPoint); else {
  210. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  211. dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  212. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""),
  213. 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"), "");
  214. }
  215. if (0 === opts.digits && (-1 !== initialValue.indexOf(".") ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : -1 !== initialValue.indexOf(",") && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))),
  216. "" !== opts.radixPoint && isFinite(opts.digits) && -1 !== initialValue.indexOf(opts.radixPoint)) {
  217. var valueParts = initialValue.split(opts.radixPoint), decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  218. if (parseInt(opts.digits) < decPart.toString().length) {
  219. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  220. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), "."),
  221. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor,
  222. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  223. }
  224. }
  225. return initialValue.toString();
  226. },
  227. canClearPosition: function(maskset, position, lvp, strict, opts) {
  228. var positionInput = maskset.validPositions[position].input, canClear = positionInput !== opts.radixPoint || null !== maskset.validPositions[position].match.fn && opts.decimalProtect === !1 || isFinite(positionInput) || position === lvp || positionInput === opts.groupSeparator || positionInput === opts.negationSymbol.front || positionInput === opts.negationSymbol.back;
  229. if (canClear && isFinite(positionInput)) {
  230. var matchRslt, radixPos = $.inArray(opts.radixPoint, maskset.buffer), radixInjection = !1;
  231. if (void 0 === maskset.validPositions[radixPos] && (maskset.validPositions[radixPos] = {
  232. input: opts.radixPoint
  233. }, radixInjection = !0), !strict && maskset.buffer) {
  234. matchRslt = maskset.buffer.join("").substr(0, position).match(opts.regex.integerNPart(opts));
  235. var pos = position + 1, isNull = null == matchRslt || 0 === parseInt(matchRslt[0].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""));
  236. if (isNull) for (;maskset.validPositions[pos] && (maskset.validPositions[pos].input === opts.groupSeparator || "0" === maskset.validPositions[pos].input); ) delete maskset.validPositions[pos],
  237. pos++;
  238. }
  239. var buffer = [];
  240. for (var vp in maskset.validPositions) void 0 !== maskset.validPositions[vp].input && buffer.push(maskset.validPositions[vp].input);
  241. if (radixInjection && delete maskset.validPositions[radixPos], radixPos > 0) {
  242. var bufVal = buffer.join("");
  243. if (matchRslt = bufVal.match(opts.regex.integerNPart(opts))) if (radixPos >= position) if (0 === matchRslt[0].indexOf("0")) canClear = matchRslt.index !== position || "0" === opts.placeholder; else {
  244. var intPart = parseInt(matchRslt[0].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "")), radixPart = parseInt(bufVal.split(opts.radixPoint)[1]);
  245. 10 > intPart && maskset.validPositions[position] && ("0" !== opts.placeholder || radixPart > 0) && (maskset.validPositions[position].input = "0",
  246. maskset.p = opts.prefix.length + 1, canClear = !1);
  247. } else 0 === matchRslt[0].indexOf("0") && 3 === bufVal.length && (maskset.validPositions = {},
  248. canClear = !1);
  249. }
  250. }
  251. return canClear;
  252. },
  253. onKeyDown: function(e, buffer, caretPos, opts) {
  254. var $input = $(this);
  255. if (e.ctrlKey) switch (e.keyCode) {
  256. case Inputmask.keyCode.UP:
  257. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step)), $input.trigger("setvalue");
  258. break;
  259. case Inputmask.keyCode.DOWN:
  260. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step)), $input.trigger("setvalue");
  261. }
  262. }
  263. },
  264. numeric: {
  265. mask: function(opts) {
  266. function autoEscape(txt) {
  267. for (var escapedTxt = "", i = 0; i < txt.length; i++) escapedTxt += opts.definitions[txt.charAt(i)] ? "\\" + txt.charAt(i) : txt.charAt(i);
  268. return escapedTxt;
  269. }
  270. if (0 !== opts.repeat && isNaN(opts.integerDigits) && (opts.integerDigits = opts.repeat),
  271. opts.repeat = 0, opts.groupSeparator === opts.radixPoint && ("." === opts.radixPoint ? opts.groupSeparator = "," : "," === opts.radixPoint ? opts.groupSeparator = "." : opts.groupSeparator = ""),
  272. " " === opts.groupSeparator && (opts.skipOptionalPartCharacter = void 0), opts.autoGroup = opts.autoGroup && "" !== opts.groupSeparator,
  273. opts.autoGroup && ("string" == typeof opts.groupSize && isFinite(opts.groupSize) && (opts.groupSize = parseInt(opts.groupSize)),
  274. isFinite(opts.integerDigits))) {
  275. var seps = Math.floor(opts.integerDigits / opts.groupSize), mod = opts.integerDigits % opts.groupSize;
  276. opts.integerDigits = parseInt(opts.integerDigits) + (0 === mod ? seps - 1 : seps),
  277. opts.integerDigits < 1 && (opts.integerDigits = "*");
  278. }
  279. opts.placeholder.length > 1 && (opts.placeholder = opts.placeholder.charAt(0)),
  280. opts.radixFocus = opts.radixFocus && "" !== opts.placeholder && opts.integerOptional === !0,
  281. opts.definitions[";"] = opts.definitions["~"], opts.definitions[";"].definitionSymbol = "~";
  282. var mask = autoEscape(opts.prefix);
  283. return mask += "[+]", mask += opts.integerOptional === !0 ? "~{1," + opts.integerDigits + "}" : "~{" + opts.integerDigits + "}",
  284. void 0 !== opts.digits && (isNaN(opts.digits) || parseInt(opts.digits) > 0) && (mask += opts.digitsOptional ? "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{1," + opts.digits + "}]" : (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}"),
  285. "" !== opts.negationSymbol.back && (mask += "[-]"), mask += autoEscape(opts.suffix),
  286. opts.greedy = !1, mask;
  287. },
  288. placeholder: "",
  289. greedy: !1,
  290. digits: "*",
  291. digitsOptional: !0,
  292. radixPoint: ".",
  293. radixFocus: !0,
  294. groupSize: 3,
  295. groupSeparator: "",
  296. autoGroup: !1,
  297. allowPlus: !0,
  298. allowMinus: !0,
  299. negationSymbol: {
  300. front: "-",
  301. back: ""
  302. },
  303. integerDigits: "+",
  304. integerOptional: !0,
  305. prefix: "",
  306. suffix: "",
  307. rightAlign: !0,
  308. decimalProtect: !0,
  309. min: null,
  310. max: null,
  311. step: 1,
  312. insertMode: !0,
  313. autoUnmask: !1,
  314. unmaskAsNumber: !1,
  315. postFormat: function(buffer, pos, reformatOnly, opts) {
  316. opts.numericInput === !0 && (buffer = buffer.reverse(), isFinite(pos) && (pos = buffer.join("").length - pos - 1));
  317. var i, l, suffixStripped = !1;
  318. buffer.length >= opts.suffix.length && buffer.join("").indexOf(opts.suffix) === buffer.length - opts.suffix.length && (buffer.length = buffer.length - opts.suffix.length,
  319. suffixStripped = !0), pos = pos >= buffer.length ? buffer.length - 1 : pos < opts.prefix.length ? opts.prefix.length : pos;
  320. var needsRefresh = !1, charAtPos = buffer[pos];
  321. if ("" === opts.groupSeparator || opts.numericInput !== !0 && -1 !== $.inArray(opts.radixPoint, buffer) && pos > $.inArray(opts.radixPoint, buffer) || new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]").test(charAtPos)) {
  322. if (suffixStripped) for (i = 0, l = opts.suffix.length; l > i; i++) buffer.push(opts.suffix.charAt(i));
  323. return {
  324. pos: pos
  325. };
  326. }
  327. var cbuf = buffer.slice();
  328. charAtPos === opts.groupSeparator && (cbuf.splice(pos--, 1), charAtPos = cbuf[pos]),
  329. reformatOnly ? charAtPos !== opts.radixPoint && (cbuf[pos] = "?") : cbuf.splice(pos, 0, "?");
  330. var bufVal = cbuf.join(""), bufValOrigin = bufVal;
  331. if (bufVal.length > 0 && opts.autoGroup || reformatOnly && -1 !== bufVal.indexOf(opts.groupSeparator)) {
  332. var escapedGroupSeparator = Inputmask.escapeRegex(opts.groupSeparator);
  333. needsRefresh = 0 === bufVal.indexOf(opts.groupSeparator), bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  334. var radixSplit = bufVal.split(opts.radixPoint);
  335. 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"),
  336. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  337. "" !== opts.radixPoint && radixSplit.length > 1 && (bufVal += opts.radixPoint + radixSplit[1]);
  338. }
  339. for (needsRefresh = bufValOrigin !== bufVal, buffer.length = bufVal.length, i = 0,
  340. l = bufVal.length; l > i; i++) buffer[i] = bufVal.charAt(i);
  341. var newPos = $.inArray("?", buffer);
  342. if (-1 === newPos && charAtPos === opts.radixPoint && (newPos = $.inArray(opts.radixPoint, buffer)),
  343. reformatOnly ? buffer[newPos] = charAtPos : buffer.splice(newPos, 1), !needsRefresh && suffixStripped) for (i = 0,
  344. l = opts.suffix.length; l > i; i++) buffer.push(opts.suffix.charAt(i));
  345. return newPos = opts.numericInput && isFinite(pos) ? buffer.join("").length - newPos - 1 : newPos,
  346. opts.numericInput && (buffer = buffer.reverse(), $.inArray(opts.radixPoint, buffer) < newPos && buffer.join("").length - opts.suffix.length !== newPos && (newPos -= 1)),
  347. {
  348. pos: newPos,
  349. refreshFromBuffer: needsRefresh,
  350. buffer: buffer
  351. };
  352. },
  353. onBeforeWrite: function(e, buffer, caretPos, opts) {
  354. if (e && ("blur" === e.type || "checkval" === e.type)) {
  355. var maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
  356. if (processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  357. "," === opts.radixPoint && (processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".")),
  358. isFinite(processValue) && isFinite(opts.min) && parseFloat(processValue) < parseFloat(opts.min)) return $.extend(!0, {
  359. refreshFromBuffer: !0,
  360. buffer: (opts.prefix + opts.min).split("")
  361. }, opts.postFormat((opts.prefix + opts.min).split(""), 0, !0, opts));
  362. if (opts.numericInput !== !0) {
  363. 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;
  364. if (matchRslt) {
  365. 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);
  366. var radixPosition = $.inArray(opts.radixPoint, buffer);
  367. if (-1 !== radixPosition) {
  368. if (isFinite(opts.digits) && !opts.digitsOptional) {
  369. for (var i = 1; i <= opts.digits; i++) (void 0 === buffer[radixPosition + i] || buffer[radixPosition + i] === opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
  370. return {
  371. refreshFromBuffer: maskedValue !== buffer.join(""),
  372. buffer: buffer
  373. };
  374. }
  375. if (radixPosition === buffer.length - opts.suffix.length - 1) return buffer.splice(radixPosition, 1),
  376. {
  377. refreshFromBuffer: !0,
  378. buffer: buffer
  379. };
  380. }
  381. }
  382. }
  383. }
  384. if (opts.autoGroup) {
  385. var rslt = opts.postFormat(buffer, opts.numericInput ? caretPos : caretPos - 1, !0, opts);
  386. return rslt.caret = caretPos <= opts.prefix.length ? rslt.pos : rslt.pos + 1, rslt;
  387. }
  388. },
  389. regex: {
  390. integerPart: function(opts) {
  391. return new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]?\\d+");
  392. },
  393. integerNPart: function(opts) {
  394. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + "]+");
  395. }
  396. },
  397. signHandler: function(chrs, maskset, pos, strict, opts) {
  398. if (!strict && opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs) {
  399. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  400. if (matchRslt && matchRslt[0].length > 0) return maskset.buffer[matchRslt.index] === ("-" === chrs ? "+" : opts.negationSymbol.front) ? "-" === chrs ? "" !== opts.negationSymbol.back ? {
  401. pos: matchRslt.index,
  402. c: opts.negationSymbol.front,
  403. remove: matchRslt.index,
  404. caret: pos,
  405. insert: {
  406. pos: maskset.buffer.length - opts.suffix.length - 1,
  407. c: opts.negationSymbol.back
  408. }
  409. } : {
  410. pos: matchRslt.index,
  411. c: opts.negationSymbol.front,
  412. remove: matchRslt.index,
  413. caret: pos
  414. } : "" !== opts.negationSymbol.back ? {
  415. pos: matchRslt.index,
  416. c: "+",
  417. remove: [ matchRslt.index, maskset.buffer.length - opts.suffix.length - 1 ],
  418. caret: pos
  419. } : {
  420. pos: matchRslt.index,
  421. c: "+",
  422. remove: matchRslt.index,
  423. caret: pos
  424. } : maskset.buffer[matchRslt.index] === ("-" === chrs ? opts.negationSymbol.front : "+") ? "-" === chrs && "" !== opts.negationSymbol.back ? {
  425. remove: [ matchRslt.index, maskset.buffer.length - opts.suffix.length - 1 ],
  426. caret: pos - 1
  427. } : {
  428. remove: matchRslt.index,
  429. caret: pos - 1
  430. } : "-" === chrs ? "" !== opts.negationSymbol.back ? {
  431. pos: matchRslt.index,
  432. c: opts.negationSymbol.front,
  433. caret: pos + 1,
  434. insert: {
  435. pos: maskset.buffer.length - opts.suffix.length,
  436. c: opts.negationSymbol.back
  437. }
  438. } : {
  439. pos: matchRslt.index,
  440. c: opts.negationSymbol.front,
  441. caret: pos + 1
  442. } : {
  443. pos: matchRslt.index,
  444. c: chrs,
  445. caret: pos + 1
  446. };
  447. }
  448. return !1;
  449. },
  450. radixHandler: function(chrs, maskset, pos, strict, opts) {
  451. if (!strict && (-1 !== $.inArray(chrs, [ ",", "." ]) && (chrs = opts.radixPoint),
  452. chrs === opts.radixPoint && void 0 !== opts.digits && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  453. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  454. if (-1 !== radixPos && maskset.validPositions[radixPos]) return maskset.validPositions[radixPos - 1] ? {
  455. caret: radixPos + 1
  456. } : {
  457. pos: integerValue.index,
  458. c: integerValue[0],
  459. caret: radixPos + 1
  460. };
  461. if (!integerValue || "0" === integerValue[0] && integerValue.index + 1 !== pos) return maskset.buffer[integerValue ? integerValue.index : pos] = "0",
  462. {
  463. pos: (integerValue ? integerValue.index : pos) + 1,
  464. c: opts.radixPoint
  465. };
  466. }
  467. return !1;
  468. },
  469. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  470. if (opts.numericInput === !0) {
  471. if ("0" === maskset.buffer[maskset.buffer.length - opts.prefix.length - 1]) return {
  472. pos: pos,
  473. remove: maskset.buffer.length - opts.prefix.length - 1
  474. };
  475. } else {
  476. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  477. if (matchRslt && !strict && (-1 === radixPosition || radixPosition >= pos)) if (0 === matchRslt[0].indexOf("0")) {
  478. pos < opts.prefix.length && (pos = matchRslt.index);
  479. 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("");
  480. if (-1 === radixPosition || digitsMatch && integerMatch) return maskset.buffer.splice(matchRslt.index, 1),
  481. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  482. pos: pos,
  483. remove: matchRslt.index
  484. };
  485. if (matchRslt.index + 1 === pos || "0" === chrs) return maskset.buffer.splice(matchRslt.index, 1),
  486. pos = matchRslt.index, {
  487. pos: pos,
  488. remove: matchRslt.index
  489. };
  490. } else if ("0" === chrs && pos <= matchRslt.index && matchRslt[0] !== opts.groupSeparator) return !1;
  491. }
  492. return !0;
  493. },
  494. postValidation: function(buffer, opts) {
  495. var isValid = !0, maskedValue = buffer.join(""), processValue = maskedValue.replace(opts.prefix, "");
  496. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  497. "," === opts.radixPoint && (processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".")),
  498. processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-"),
  499. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), ""),
  500. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue,
  501. isFinite(processValue) && (null !== opts.max && isFinite(opts.max) && (isValid = parseFloat(processValue) <= parseFloat(opts.max)),
  502. isValid && null !== opts.min && isFinite(opts.min) && (0 >= processValue || processValue.toString().length >= opts.min.toString().length) && (isValid = parseFloat(processValue) >= parseFloat(opts.min),
  503. isValid || (isValid = $.extend(!0, {
  504. refreshFromBuffer: !0,
  505. buffer: (opts.prefix + opts.min).split("")
  506. }, opts.postFormat((opts.prefix + opts.min).split(""), 0, !0, opts)), isValid.refreshFromBuffer = !0))),
  507. isValid;
  508. },
  509. definitions: {
  510. "~": {
  511. validator: function(chrs, maskset, pos, strict, opts) {
  512. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  513. if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts),
  514. !isValid && (isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs),
  515. isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts),
  516. isValid === !0)))) {
  517. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  518. isValid = -1 !== radixPosition && opts.digitsOptional === !1 && opts.numericInput !== !0 && pos > radixPosition && !strict ? {
  519. pos: pos,
  520. remove: pos
  521. } : {
  522. pos: pos
  523. };
  524. }
  525. return isValid;
  526. },
  527. cardinality: 1,
  528. prevalidator: null
  529. },
  530. "+": {
  531. validator: function(chrs, maskset, pos, strict, opts) {
  532. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  533. return !isValid && (strict && opts.allowMinus && chrs === opts.negationSymbol.front || opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs) && (isValid = "-" === chrs ? "" !== opts.negationSymbol.back ? {
  534. pos: pos,
  535. c: "-" === chrs ? opts.negationSymbol.front : "+",
  536. caret: pos + 1,
  537. insert: {
  538. pos: maskset.buffer.length,
  539. c: opts.negationSymbol.back
  540. }
  541. } : {
  542. pos: pos,
  543. c: "-" === chrs ? opts.negationSymbol.front : "+",
  544. caret: pos + 1
  545. } : !0), isValid;
  546. },
  547. cardinality: 1,
  548. prevalidator: null,
  549. placeholder: ""
  550. },
  551. "-": {
  552. validator: function(chrs, maskset, pos, strict, opts) {
  553. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  554. return !isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back && (isValid = !0),
  555. isValid;
  556. },
  557. cardinality: 1,
  558. prevalidator: null,
  559. placeholder: ""
  560. },
  561. ":": {
  562. validator: function(chrs, maskset, pos, strict, opts) {
  563. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  564. if (!isValid) {
  565. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + ",\\.]";
  566. isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint && (isValid = {
  567. caret: pos + 1
  568. });
  569. }
  570. return isValid ? {
  571. c: opts.radixPoint
  572. } : isValid;
  573. },
  574. cardinality: 1,
  575. prevalidator: null,
  576. placeholder: function(opts) {
  577. return opts.radixPoint;
  578. }
  579. }
  580. },
  581. onUnMask: function(maskedValue, unmaskedValue, opts) {
  582. var processValue = maskedValue.replace(opts.prefix, "");
  583. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  584. opts.unmaskAsNumber ? ("" !== opts.radixPoint && -1 !== processValue.indexOf(opts.radixPoint) && (processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".")),
  585. Number(processValue)) : processValue;
  586. },
  587. isComplete: function(buffer, opts) {
  588. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  589. if (opts.postFormat(bufClone, 0, !0, opts), bufClone.join("") !== maskedValue) return !1;
  590. var processValue = maskedValue.replace(opts.prefix, "");
  591. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  592. "," === opts.radixPoint && (processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".")),
  593. isFinite(processValue);
  594. },
  595. onBeforeMask: function(initialValue, opts) {
  596. if ("" !== opts.radixPoint && isFinite(initialValue)) initialValue = initialValue.toString().replace(".", opts.radixPoint); else {
  597. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  598. dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  599. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""),
  600. 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"), "");
  601. }
  602. if (0 === opts.digits && (-1 !== initialValue.indexOf(".") ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : -1 !== initialValue.indexOf(",") && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))),
  603. "" !== opts.radixPoint && isFinite(opts.digits) && -1 !== initialValue.indexOf(opts.radixPoint)) {
  604. var valueParts = initialValue.split(opts.radixPoint), decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  605. if (parseInt(opts.digits) < decPart.toString().length) {
  606. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  607. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), "."),
  608. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor,
  609. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  610. }
  611. }
  612. return initialValue.toString();
  613. },
  614. canClearPosition: function(maskset, position, lvp, strict, opts) {
  615. var positionInput = maskset.validPositions[position].input, canClear = positionInput !== opts.radixPoint || null !== maskset.validPositions[position].match.fn && opts.decimalProtect === !1 || isFinite(positionInput) || position === lvp || positionInput === opts.groupSeparator || positionInput === opts.negationSymbol.front || positionInput === opts.negationSymbol.back;
  616. if (canClear && isFinite(positionInput)) {
  617. var matchRslt, radixPos = $.inArray(opts.radixPoint, maskset.buffer), radixInjection = !1;
  618. if (void 0 === maskset.validPositions[radixPos] && (maskset.validPositions[radixPos] = {
  619. input: opts.radixPoint
  620. }, radixInjection = !0), !strict && maskset.buffer) {
  621. matchRslt = maskset.buffer.join("").substr(0, position).match(opts.regex.integerNPart(opts));
  622. var pos = position + 1, isNull = null == matchRslt || 0 === parseInt(matchRslt[0].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""));
  623. if (isNull) for (;maskset.validPositions[pos] && (maskset.validPositions[pos].input === opts.groupSeparator || "0" === maskset.validPositions[pos].input); ) delete maskset.validPositions[pos],
  624. pos++;
  625. }
  626. var buffer = [];
  627. for (var vp in maskset.validPositions) void 0 !== maskset.validPositions[vp].input && buffer.push(maskset.validPositions[vp].input);
  628. if (radixInjection && delete maskset.validPositions[radixPos], radixPos > 0) {
  629. var bufVal = buffer.join("");
  630. if (matchRslt = bufVal.match(opts.regex.integerNPart(opts))) if (radixPos >= position) if (0 === matchRslt[0].indexOf("0")) canClear = matchRslt.index !== position || "0" === opts.placeholder; else {
  631. var intPart = parseInt(matchRslt[0].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "")), radixPart = parseInt(bufVal.split(opts.radixPoint)[1]);
  632. 10 > intPart && maskset.validPositions[position] && ("0" !== opts.placeholder || radixPart > 0) && (maskset.validPositions[position].input = "0",
  633. maskset.p = opts.prefix.length + 1, canClear = !1);
  634. } else 0 === matchRslt[0].indexOf("0") && 3 === bufVal.length && (maskset.validPositions = {},
  635. canClear = !1);
  636. }
  637. }
  638. return canClear;
  639. },
  640. onKeyDown: function(e, buffer, caretPos, opts) {
  641. var $input = $(this);
  642. if (e.ctrlKey) switch (e.keyCode) {
  643. case Inputmask.keyCode.UP:
  644. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step)), $input.trigger("setvalue");
  645. break;
  646. case Inputmask.keyCode.DOWN:
  647. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step)), $input.trigger("setvalue");
  648. }
  649. }
  650. },
  651. currency: {
  652. prefix: "$ ",
  653. groupSeparator: ",",
  654. alias: "numeric",
  655. placeholder: "0",
  656. autoGroup: !0,
  657. digits: 2,
  658. digitsOptional: !1,
  659. clearMaskOnLostFocus: !1
  660. },
  661. decimal: {
  662. alias: "numeric"
  663. },
  664. integer: {
  665. alias: "numeric",
  666. digits: 0,
  667. radixPoint: ""
  668. },
  669. percentage: {
  670. alias: "numeric",
  671. digits: 2,
  672. radixPoint: ".",
  673. placeholder: "0",
  674. autoGroup: !1,
  675. min: 0,
  676. max: 100,
  677. suffix: " %",
  678. allowPlus: !1,
  679. allowMinus: !1
  680. }
  681. }), Inputmask;
  682. });