inputmask.numeric.extensions.js 42 KB

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