jquery.inputmask.numeric.extensions.js 18 KB

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