jquery.inputmask.numeric.extensions.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*!
  2. * jquery.inputmask.numeric.extensions.js
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2014 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.1.32
  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. if (0 !== opts.repeat && isNaN(opts.integerDigits) && (opts.integerDigits = opts.repeat),
  15. opts.repeat = 0, opts.groupSeparator == opts.radixPoint && (opts.groupSeparator = "." == opts.radixPoint ? "," : "," == opts.radixPoint ? "." : ""),
  16. " " === opts.groupSeparator && (opts.skipOptionalPartCharacter = void 0), opts.autoGroup = opts.autoGroup && "" != opts.groupSeparator,
  17. opts.autoGroup && isFinite(opts.integerDigits)) {
  18. var seps = Math.floor(opts.integerDigits / opts.groupSize), mod = opts.integerDigits % opts.groupSize;
  19. opts.integerDigits += 0 == mod ? seps - 1 : seps;
  20. }
  21. opts.definitions[";"] = opts.definitions["~"];
  22. var mask = opts.prefix;
  23. 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 + "}"),
  24. mask += opts.suffix;
  25. },
  26. placeholder: "",
  27. greedy: !1,
  28. digits: "*",
  29. digitsOptional: !0,
  30. groupSeparator: "",
  31. radixPoint: ".",
  32. radixFocus: !0,
  33. groupSize: 3,
  34. autoGroup: !1,
  35. allowPlus: !0,
  36. allowMinus: !0,
  37. integerDigits: "+",
  38. prefix: "",
  39. suffix: "",
  40. rightAlign: !0,
  41. decimalProtect: !0,
  42. postFormat: function(buffer, pos, reformatOnly, opts) {
  43. var needsRefresh = !1, charAtPos = buffer[pos];
  44. if ("" == opts.groupSeparator || -1 != $.inArray(opts.radixPoint, buffer) && pos >= $.inArray(opts.radixPoint, buffer) || new RegExp("[-+]").test(charAtPos)) return {
  45. pos: pos
  46. };
  47. var cbuf = buffer.slice();
  48. charAtPos == opts.groupSeparator && (cbuf.splice(pos--, 1), charAtPos = cbuf[pos]),
  49. reformatOnly ? cbuf[pos] = "?" : cbuf.splice(pos, 0, "?");
  50. var bufVal = cbuf.join("");
  51. if (bufVal.length > 0 && opts.autoGroup || reformatOnly && -1 != bufVal.indexOf(opts.groupSeparator)) {
  52. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  53. needsRefresh = 0 == bufVal.indexOf(opts.groupSeparator), bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  54. var radixSplit = bufVal.split(opts.radixPoint);
  55. if (bufVal = "" == opts.radixPoint ? bufVal : radixSplit[0], bufVal != opts.prefix + "?0" && bufVal.length >= opts.groupSize + opts.prefix.length) {
  56. needsRefresh = !0;
  57. for (var reg = new RegExp("([-+]?[\\d?]+)([\\d?]{" + opts.groupSize + "})"); reg.test(bufVal); ) bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2"),
  58. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  59. }
  60. "" != opts.radixPoint && radixSplit.length > 1 && (bufVal += opts.radixPoint + radixSplit[1]);
  61. }
  62. buffer.length = bufVal.length;
  63. for (var i = 0, l = bufVal.length; l > i; i++) buffer[i] = bufVal.charAt(i);
  64. var newPos = $.inArray("?", buffer);
  65. return reformatOnly ? buffer[newPos] = charAtPos : buffer.splice(newPos, 1), {
  66. pos: newPos,
  67. refreshFromBuffer: needsRefresh
  68. };
  69. },
  70. onKeyDown: function(e, buffer, caretPos, opts) {
  71. if (e.keyCode == $.inputmask.keyCode.TAB && "0" != opts.placeholder.charAt(0)) {
  72. var radixPosition = $.inArray(opts.radixPoint, buffer);
  73. if (-1 != radixPosition && isFinite(opts.digits)) {
  74. for (var i = 1; i <= opts.digits; i++) (void 0 == buffer[radixPosition + i] || buffer[radixPosition + i] == opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
  75. return {
  76. refreshFromBuffer: {
  77. start: ++radixPosition,
  78. end: radixPosition + opts.digits
  79. }
  80. };
  81. }
  82. } else if (opts.autoGroup && (e.keyCode == $.inputmask.keyCode.DELETE || e.keyCode == $.inputmask.keyCode.BACKSPACE)) {
  83. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  84. return rslt.caret = rslt.pos + 1, rslt;
  85. }
  86. },
  87. onKeyPress: function(e, buffer, caretPos, opts) {
  88. if (opts.autoGroup) {
  89. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  90. return rslt.caret = rslt.pos + 1, rslt;
  91. }
  92. },
  93. regex: {
  94. integerPart: function() {
  95. return new RegExp("[-+]?\\d+");
  96. },
  97. integerNPart: function() {
  98. return new RegExp("\\d+");
  99. }
  100. },
  101. signHandler: function(chrs, maskset, pos, strict, opts) {
  102. if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs || "0" === chrs)) {
  103. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  104. if (matchRslt && matchRslt.length > 0 && ("0" !== matchRslt[matchRslt.index] || maskset.buffer && maskset._buffer && maskset.buffer.join("") != maskset._buffer.join(""))) {
  105. if ("0" !== chrs) return maskset.buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
  106. pos: matchRslt.index,
  107. c: chrs,
  108. remove: matchRslt.index,
  109. caret: pos
  110. } : maskset.buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
  111. remove: matchRslt.index,
  112. caret: pos - 1
  113. } : {
  114. pos: matchRslt.index,
  115. c: chrs,
  116. caret: pos + 1
  117. };
  118. if ("-" == maskset.buffer[matchRslt.index] || "+" == maskset.buffer[matchRslt.index]) return {
  119. remove: matchRslt.index,
  120. caret: pos - 1
  121. };
  122. }
  123. }
  124. return !1;
  125. },
  126. radixHandler: function(chrs, maskset, pos, strict, opts) {
  127. if (!strict && chrs === opts.radixPoint) {
  128. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  129. if (-1 != radixPos && maskset.validPositions[radixPos]) return maskset.validPositions[radixPos - 1] ? {
  130. caret: radixPos + 1
  131. } : {
  132. pos: integerValue.index,
  133. c: integerValue[0],
  134. caret: radixPos + 1
  135. };
  136. }
  137. return !1;
  138. },
  139. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  140. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  141. if (matchRslt && !strict && (-1 == radixPosition || matchRslt.index < radixPosition)) if ("0" == matchRslt[0] && pos >= opts.prefix.length) {
  142. if (-1 == radixPosition || radixPosition >= pos && void 0 == maskset.validPositions[radixPosition]) return maskset.buffer.splice(matchRslt.index, 1),
  143. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  144. pos: pos,
  145. remove: matchRslt.index
  146. };
  147. if (pos > matchRslt.index && radixPosition >= pos) 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 (void 0 == maskset.validPositions[radixPosition]) return maskset.buffer[pos] = chrs,
  153. {
  154. refreshFromBuffer: !0
  155. };
  156. } else if ("0" == chrs && pos <= matchRslt.index) return !1;
  157. return !0;
  158. },
  159. definitions: {
  160. "~": {
  161. validator: function(chrs, maskset, pos, strict, opts) {
  162. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  163. if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts),
  164. !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs),
  165. isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts),
  166. isValid === !0)))) {
  167. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  168. isValid = opts.digitsOptional === !1 && pos > radixPosition && !strict ? {
  169. pos: pos,
  170. remove: pos
  171. } : {
  172. pos: pos
  173. };
  174. }
  175. return isValid;
  176. },
  177. cardinality: 1,
  178. prevalidator: null
  179. },
  180. "+": {
  181. validator: function(chrs, maskset, pos, strict, opts) {
  182. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  183. return isValid || (isValid = opts.allowMinus && "-" == chrs || opts.allowPlus && "+" == chrs),
  184. isValid;
  185. },
  186. cardinality: 1,
  187. prevalidator: null,
  188. placeholder: ""
  189. },
  190. ":": {
  191. validator: function(chrs, maskset, pos, strict, opts) {
  192. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  193. if (!isValid) {
  194. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  195. isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {
  196. pos: pos,
  197. remove: pos
  198. });
  199. }
  200. return isValid;
  201. },
  202. cardinality: 1,
  203. prevalidator: null,
  204. placeholder: function(opts) {
  205. return opts.radixPoint;
  206. }
  207. }
  208. },
  209. insertMode: !0,
  210. autoUnmask: !1,
  211. onUnMask: function(maskedValue, unmaskedValue, opts) {
  212. var processValue = maskedValue.replace(opts.prefix, "");
  213. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  214. },
  215. isComplete: function(buffer, opts) {
  216. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  217. if (opts.postFormat(bufClone, 0, !0, opts), bufClone.join("") != maskedValue) return !1;
  218. var processValue = maskedValue.replace(opts.prefix, "");
  219. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""),
  220. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."),
  221. isFinite(processValue);
  222. },
  223. onBeforeMask: function(initialValue, opts) {
  224. if (isFinite(initialValue)) return initialValue.toString().replace(".", opts.radixPoint);
  225. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  226. return dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  227. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length && (initialValue = initialValue.replace(/,/g, ""),
  228. initialValue = initialValue.replace(".", opts.radixPoint)) : initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""),
  229. initialValue;
  230. }
  231. },
  232. currency: {
  233. prefix: "$ ",
  234. groupSeparator: ",",
  235. alias: "numeric",
  236. placeholder: "0",
  237. autoGroup: !0,
  238. digits: 2,
  239. digitsOptional: !1,
  240. clearMaskOnLostFocus: !1
  241. },
  242. decimal: {
  243. alias: "numeric"
  244. },
  245. integer: {
  246. alias: "numeric",
  247. digits: "0",
  248. radixPoint: ""
  249. }
  250. }), $.fn.inputmask;
  251. });