You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
20KB

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