jquery.inputmask.numeric.extensions.js 19 KB

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