jquery.inputmask.numeric.extensions.js 23 KB

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