inputmask.numeric.extensions.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*!
  2. * inputmask.numeric.extensions.js
  3. * https://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2017 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.3.5-152
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "./dependencyLibs/inputmask.dependencyLib", "./inputmask" ], factory) : "object" == typeof exports ? module.exports = factory(require("./dependencyLibs/inputmask.dependencyLib"), require("./inputmask")) : factory(window.dependencyLib || jQuery, window.Inputmask);
  10. }(function($, Inputmask, undefined) {
  11. function autoEscape(txt, opts) {
  12. for (var escapedTxt = "", i = 0; i < txt.length; i++) escapedTxt += opts.definitions[txt.charAt(i)] || opts.optionalmarker.start === txt.charAt(i) || opts.optionalmarker.end === txt.charAt(i) || opts.quantifiermarker.start === txt.charAt(i) || opts.quantifiermarker.end === txt.charAt(i) || opts.groupmarker.start === txt.charAt(i) || opts.groupmarker.end === txt.charAt(i) || opts.alternatormarker === txt.charAt(i) ? "\\" + txt.charAt(i) : txt.charAt(i);
  13. return escapedTxt;
  14. }
  15. return Inputmask.extendAliases({
  16. numeric: {
  17. mask: function(opts) {
  18. if (0 !== opts.repeat && isNaN(opts.integerDigits) && (opts.integerDigits = opts.repeat),
  19. opts.repeat = 0, opts.groupSeparator === opts.radixPoint && ("." === opts.radixPoint ? opts.groupSeparator = "," : "," === opts.radixPoint ? opts.groupSeparator = "." : opts.groupSeparator = ""),
  20. " " === opts.groupSeparator && (opts.skipOptionalPartCharacter = undefined), 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. opts.integerDigits < 1 && (opts.integerDigits = "*");
  26. }
  27. opts.placeholder.length > 1 && (opts.placeholder = opts.placeholder.charAt(0)),
  28. "radixFocus" === opts.positionCaretOnClick && "" === opts.placeholder && opts.integerOptional === !1 && (opts.positionCaretOnClick = "lvp"),
  29. opts.definitions[";"] = opts.definitions["~"], opts.definitions[";"].definitionSymbol = "~",
  30. opts.numericInput === !0 && (opts.positionCaretOnClick = "radixFocus" === opts.positionCaretOnClick ? "lvp" : opts.positionCaretOnClick,
  31. opts.digitsOptional = !1, isNaN(opts.digits) && (opts.digits = 2), opts.decimalProtect = !1);
  32. var mask = "[+]";
  33. if (mask += autoEscape(opts.prefix, opts), mask += opts.integerOptional === !0 ? "~{1," + opts.integerDigits + "}" : "~{" + opts.integerDigits + "}",
  34. opts.digits !== undefined) {
  35. opts.radixPointDefinitionSymbol = opts.decimalProtect ? ":" : opts.radixPoint;
  36. var dq = opts.digits.toString().split(",");
  37. isFinite(dq[0] && dq[1] && isFinite(dq[1])) ? mask += opts.radixPointDefinitionSymbol + ";{" + opts.digits + "}" : (isNaN(opts.digits) || parseInt(opts.digits) > 0) && (mask += opts.digitsOptional ? "[" + opts.radixPointDefinitionSymbol + ";{1," + opts.digits + "}]" : opts.radixPointDefinitionSymbol + ";{" + opts.digits + "}");
  38. }
  39. return mask += autoEscape(opts.suffix, opts), mask += "[-]", opts.greedy = !1, mask;
  40. },
  41. placeholder: "",
  42. greedy: !1,
  43. digits: "*",
  44. digitsOptional: !0,
  45. radixPoint: ".",
  46. positionCaretOnClick: "radixFocus",
  47. groupSize: 3,
  48. groupSeparator: "",
  49. autoGroup: !1,
  50. allowMinus: !0,
  51. negationSymbol: {
  52. front: "-",
  53. back: ""
  54. },
  55. integerDigits: "+",
  56. integerOptional: !0,
  57. prefix: "",
  58. suffix: "",
  59. rightAlign: !0,
  60. decimalProtect: !0,
  61. min: null,
  62. max: null,
  63. step: 1,
  64. insertMode: !0,
  65. autoUnmask: !1,
  66. unmaskAsNumber: !1,
  67. inputmode: "numeric",
  68. preValidation: function(buffer, pos, c, isSelection, opts) {
  69. if ("-" === c || c == opts.negationSymbol.front) return opts.allowMinus === !0 && (opts.isNegative = opts.isNegative === undefined || !opts.isNegative,
  70. "" === buffer.join("") || {
  71. caret: pos,
  72. dopost: !0
  73. });
  74. if (isSelection === !1 && opts.numericInput !== !0 && c === opts.radixPoint && opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  75. var radixPos = $.inArray(opts.radixPoint, buffer);
  76. if (radixPos !== -1) return {
  77. caret: radixPos + 1
  78. };
  79. }
  80. return !0;
  81. },
  82. postValidation: function(buffer, currentResult, opts) {
  83. function buildPostMask(buffer, opts) {
  84. var postMask = "";
  85. if (postMask += "(" + opts.groupSeparator + "*{" + opts.groupSize + "}){*}", "" !== opts.radixPoint) {
  86. var radixSplit = buffer.join("").split(opts.radixPoint);
  87. radixSplit[1] && (postMask += opts.radixPoint + "*{" + radixSplit[1].match(/^\d*\??\d*/)[0].length + "}");
  88. }
  89. return postMask;
  90. }
  91. var suffix = opts.suffix.split(""), prefix = opts.prefix.split("");
  92. if (currentResult.pos == undefined && currentResult.caret !== undefined && currentResult.dopost !== !0) return currentResult;
  93. var caretPos = currentResult.caret != undefined ? currentResult.caret : currentResult.pos, maskedValue = buffer.slice();
  94. opts.numericInput && (caretPos = maskedValue.length - caretPos - 1, maskedValue = maskedValue.reverse());
  95. var charAtPos = maskedValue[caretPos];
  96. if (charAtPos === opts.groupSeparator && (caretPos += 1, charAtPos = maskedValue[caretPos]),
  97. caretPos == maskedValue.length - opts.suffix.length - 1 && charAtPos === opts.radixPoint) return currentResult;
  98. charAtPos !== undefined && charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back && (maskedValue[caretPos] = "?",
  99. opts.prefix.length > 0 && caretPos >= (opts.isNegative === !1 ? 1 : 0) && caretPos < opts.prefix.length - 1 + (opts.isNegative === !1 ? 1 : 0) ? prefix[caretPos - (opts.isNegative === !1 ? 1 : 0)] = "?" : opts.suffix.length > 0 && caretPos >= maskedValue.length - opts.suffix.length - (opts.isNegative === !1 ? 1 : 0) && (suffix[caretPos - (maskedValue.length - opts.suffix.length - (opts.isNegative === !1 ? 1 : 0))] = "?")),
  100. prefix = prefix.join(""), suffix = suffix.join("");
  101. var processValue = maskedValue.join("").replace(prefix, "");
  102. if (processValue = processValue.replace(suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  103. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), ""),
  104. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), ""),
  105. isNaN(opts.placeholder) && (processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.placeholder), "g"), "")),
  106. processValue.length > 1 && 1 !== $.inArray(opts.radixPoint, processValue) && ("0" == charAtPos && (processValue = processValue.replace(/^\?/g, "")),
  107. processValue = processValue.replace(/^0/g, "")), processValue.charAt(0) === opts.radixPoint && opts.numericInput !== !0 && (processValue = "0" + processValue),
  108. "" !== processValue) {
  109. if (processValue = processValue.split(""), !opts.digitsOptional && isFinite(opts.digits)) {
  110. var radixPosition = $.inArray(opts.radixPoint, processValue), rpb = $.inArray(opts.radixPoint, maskedValue);
  111. radixPosition === -1 && (processValue.push(opts.radixPoint), radixPosition = processValue.length - 1);
  112. for (var i = 1; i <= opts.digits; i++) opts.digitsOptional || processValue[radixPosition + i] !== undefined && processValue[radixPosition + i] !== opts.placeholder.charAt(0) ? rpb !== -1 && maskedValue[rpb + i] !== undefined && (processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i]) : processValue[radixPosition + i] = currentResult.placeholder || opts.placeholder.charAt(0);
  113. }
  114. opts.autoGroup !== !0 || "" === opts.groupSeparator || charAtPos === opts.radixPoint && currentResult.pos === undefined && !currentResult.dopost ? processValue = processValue.join("") : (processValue = Inputmask(buildPostMask(processValue, opts), {
  115. numericInput: !0,
  116. jitMasking: !0,
  117. definitions: {
  118. "*": {
  119. validator: "[0-9?]",
  120. cardinality: 1
  121. }
  122. }
  123. }).format(processValue.join("")), processValue.charAt(0) === opts.groupSeparator && processValue.substr(1));
  124. }
  125. if (opts.isNegative && "blur" === currentResult.event && (opts.isNegative = "0" !== processValue),
  126. processValue = prefix + processValue, processValue += suffix, opts.isNegative && (processValue = opts.negationSymbol.front + processValue,
  127. processValue += opts.negationSymbol.back), processValue = processValue.split(""),
  128. charAtPos !== undefined) if (charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back) caretPos = $.inArray("?", processValue),
  129. caretPos > -1 ? processValue[caretPos] = charAtPos : caretPos = currentResult.caret || 0; else if (charAtPos === opts.radixPoint || charAtPos === opts.negationSymbol.front || charAtPos === opts.negationSymbol.back) {
  130. var newCaretPos = $.inArray(charAtPos, processValue);
  131. newCaretPos !== -1 && (caretPos = newCaretPos);
  132. }
  133. opts.numericInput && (caretPos = processValue.length - caretPos - 1, processValue = processValue.reverse());
  134. var rslt = {
  135. caret: charAtPos === undefined || currentResult.pos !== undefined ? caretPos + (opts.numericInput ? -1 : 1) : caretPos,
  136. buffer: processValue,
  137. refreshFromBuffer: currentResult.dopost || buffer.join("") !== processValue.join("")
  138. };
  139. return rslt.refreshFromBuffer ? rslt : currentResult;
  140. },
  141. onBeforeWrite: function(e, buffer, caretPos, opts) {
  142. function parseMinMaxOptions(opts) {
  143. opts.parseMinMaxOptions === undefined && (null !== opts.min && (opts.min = opts.min.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  144. "," === opts.radixPoint && (opts.min = opts.min.replace(opts.radixPoint, ".")),
  145. opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN, isNaN(opts.min) && (opts.min = Number.MIN_VALUE)),
  146. null !== opts.max && (opts.max = opts.max.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  147. "," === opts.radixPoint && (opts.max = opts.max.replace(opts.radixPoint, ".")),
  148. opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN, isNaN(opts.max) && (opts.max = Number.MAX_VALUE)),
  149. opts.parseMinMaxOptions = "done");
  150. }
  151. if (e) switch (e.type) {
  152. case "keydown":
  153. return opts.postValidation(buffer, {
  154. caret: caretPos,
  155. dopost: !0
  156. }, opts);
  157. case "blur":
  158. case "checkval":
  159. var unmasked;
  160. if (parseMinMaxOptions(opts), null !== opts.min || null !== opts.max) {
  161. if (unmasked = opts.onUnMask(buffer.join(""), undefined, $.extend({}, opts, {
  162. unmaskAsNumber: !0
  163. })), null !== opts.min && unmasked < opts.min) return opts.isNegative = opts.min < 0,
  164. opts.postValidation(opts.min.toString().replace(".", opts.radixPoint).split(""), {
  165. caret: caretPos,
  166. dopost: !0,
  167. placeholder: "0"
  168. }, opts);
  169. if (null !== opts.max && unmasked > opts.max) return opts.isNegative = opts.max < 0,
  170. opts.postValidation(opts.max.toString().replace(".", opts.radixPoint).split(""), {
  171. caret: caretPos,
  172. dopost: !0,
  173. placeholder: "0"
  174. }, opts);
  175. }
  176. return opts.postValidation(buffer, {
  177. caret: caretPos,
  178. dopost: !0,
  179. placeholder: "0",
  180. event: "blur"
  181. }, opts);
  182. }
  183. },
  184. regex: {
  185. integerPart: function(opts, emptyCheck) {
  186. return emptyCheck ? new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]?") : new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]?\\d+");
  187. },
  188. integerNPart: function(opts) {
  189. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + Inputmask.escapeRegex(opts.placeholder.charAt(0)) + "]+");
  190. }
  191. },
  192. definitions: {
  193. "~": {
  194. validator: function(chrs, maskset, pos, strict, opts, isSelection) {
  195. var isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  196. if (isValid === !0) {
  197. if (opts.numericInput !== !0 && maskset.validPositions[pos] !== undefined && "~" === maskset.validPositions[pos].match.def && !isSelection) {
  198. var processValue = maskset.buffer.join("");
  199. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), ""),
  200. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), ""),
  201. processValue = processValue.replace(/0/g, opts.placeholder.charAt(0));
  202. var bufferTemplate = maskset._buffer.join("");
  203. for (processValue === opts.radixPoint && (processValue = bufferTemplate); null === processValue.match(Inputmask.escapeRegex(bufferTemplate) + "$"); ) bufferTemplate = bufferTemplate.slice(1);
  204. processValue = processValue.replace(bufferTemplate, ""), processValue = processValue.split(""),
  205. isValid = processValue[pos] === undefined ? {
  206. pos: pos,
  207. remove: pos
  208. } : {
  209. pos: pos
  210. };
  211. }
  212. } else strict || chrs !== opts.radixPoint || maskset.validPositions[pos - 1] !== undefined || (maskset.buffer[pos] = "0",
  213. isValid = {
  214. pos: pos + 1
  215. });
  216. return isValid;
  217. },
  218. cardinality: 1
  219. },
  220. "+": {
  221. validator: function(chrs, maskset, pos, strict, opts) {
  222. return opts.allowMinus && ("-" === chrs || chrs === opts.negationSymbol.front);
  223. },
  224. cardinality: 1,
  225. placeholder: ""
  226. },
  227. "-": {
  228. validator: function(chrs, maskset, pos, strict, opts) {
  229. return opts.allowMinus && chrs === opts.negationSymbol.back;
  230. },
  231. cardinality: 1,
  232. placeholder: ""
  233. },
  234. ":": {
  235. validator: function(chrs, maskset, pos, strict, opts) {
  236. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  237. return isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint && (isValid = {
  238. caret: pos + 1
  239. }), isValid;
  240. },
  241. cardinality: 1,
  242. placeholder: function(opts) {
  243. return opts.radixPoint;
  244. }
  245. }
  246. },
  247. onUnMask: function(maskedValue, unmaskedValue, opts) {
  248. if ("" === unmaskedValue && opts.nullable === !0) return unmaskedValue;
  249. var processValue = maskedValue.replace(opts.prefix, "");
  250. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  251. "" !== opts.placeholder.charAt(0) && (processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0")),
  252. opts.unmaskAsNumber ? ("" !== opts.radixPoint && processValue.indexOf(opts.radixPoint) !== -1 && (processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".")),
  253. Number(processValue)) : processValue;
  254. },
  255. isComplete: function(buffer, opts) {
  256. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  257. if (bufClone.join("") !== maskedValue) return !1;
  258. var processValue = maskedValue.replace(opts.prefix, "");
  259. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""),
  260. "," === opts.radixPoint && (processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".")),
  261. isFinite(processValue);
  262. },
  263. onBeforeMask: function(initialValue, opts) {
  264. if (opts.isNegative = undefined, initialValue = initialValue.toString().charAt(initialValue.length - 1) === opts.radixPoint ? initialValue.toString().substr(0, initialValue.length - 1) : initialValue.toString(),
  265. "" !== opts.radixPoint && isFinite(initialValue)) {
  266. var vs = initialValue.split("."), groupSize = "" !== opts.groupSeparator ? parseInt(opts.groupSize) : 0;
  267. 2 === vs.length && (vs[0].length > groupSize || vs[1].length > groupSize) && (initialValue = initialValue.replace(".", opts.radixPoint));
  268. }
  269. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  270. if (dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  271. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""),
  272. 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"), ""),
  273. 0 === opts.digits && (initialValue.indexOf(".") !== -1 ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : initialValue.indexOf(",") !== -1 && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))),
  274. "" !== opts.radixPoint && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  275. var valueParts = initialValue.split(opts.radixPoint), decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  276. if (parseInt(opts.digits) < decPart.toString().length) {
  277. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  278. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), "."),
  279. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor,
  280. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  281. }
  282. }
  283. return initialValue;
  284. },
  285. canClearPosition: function(maskset, position, lvp, strict, opts) {
  286. var vp = maskset.validPositions[position], canClear = vp.input !== opts.radixPoint || null !== maskset.validPositions[position].match.fn && opts.decimalProtect === !1 || vp.input === opts.radixPoint && maskset.validPositions[position + 1] && null === maskset.validPositions[position + 1].match.fn || isFinite(vp.input) || position === lvp || vp.input === opts.groupSeparator || vp.input === opts.negationSymbol.front || vp.input === opts.negationSymbol.back;
  287. return !canClear || "+" != vp.match.nativeDef && "-" != vp.match.nativeDef || (opts.isNegative = !1),
  288. canClear;
  289. },
  290. onKeyDown: function(e, buffer, caretPos, opts) {
  291. var $input = $(this);
  292. if (e.ctrlKey) switch (e.keyCode) {
  293. case Inputmask.keyCode.UP:
  294. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step)), $input.trigger("setvalue");
  295. break;
  296. case Inputmask.keyCode.DOWN:
  297. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step)), $input.trigger("setvalue");
  298. }
  299. }
  300. },
  301. currency: {
  302. prefix: "$ ",
  303. groupSeparator: ",",
  304. alias: "numeric",
  305. placeholder: "0",
  306. autoGroup: !0,
  307. digits: 2,
  308. digitsOptional: !1,
  309. clearMaskOnLostFocus: !1
  310. },
  311. decimal: {
  312. alias: "numeric"
  313. },
  314. integer: {
  315. alias: "numeric",
  316. digits: 0,
  317. radixPoint: ""
  318. },
  319. percentage: {
  320. alias: "numeric",
  321. digits: 2,
  322. digitsOptional: !0,
  323. radixPoint: ".",
  324. placeholder: "0",
  325. autoGroup: !1,
  326. min: 0,
  327. max: 100,
  328. suffix: " %",
  329. allowMinus: !1
  330. }
  331. }), Inputmask;
  332. });