jquery.inputmask.numeric.extensions.js 17 KB

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