jquery.inputmask.numeric.extensions.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.36
  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 && ("string" == typeof opts.groupSize && isFinite(opts.groupSize) && (opts.groupSize = parseInt(opts.groupSize)),
  18. isFinite(opts.integerDigits))) {
  19. var seps = Math.floor(opts.integerDigits / opts.groupSize), mod = opts.integerDigits % opts.groupSize;
  20. opts.integerDigits = parseInt(opts.integerDigits) + (0 == mod ? seps - 1 : seps);
  21. }
  22. opts.definitions[";"] = opts.definitions["~"];
  23. var mask = opts.prefix;
  24. 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 + "}"),
  25. mask += opts.suffix;
  26. },
  27. placeholder: "",
  28. greedy: !1,
  29. digits: "*",
  30. digitsOptional: !0,
  31. groupSeparator: "",
  32. radixPoint: ".",
  33. radixFocus: !0,
  34. groupSize: 3,
  35. autoGroup: !1,
  36. allowPlus: !0,
  37. allowMinus: !0,
  38. integerDigits: "+",
  39. prefix: "",
  40. suffix: "",
  41. rightAlign: !0,
  42. decimalProtect: !0,
  43. postFormat: function(buffer, pos, reformatOnly, opts) {
  44. var needsRefresh = !1, charAtPos = buffer[pos];
  45. if ("" == opts.groupSeparator || -1 != $.inArray(opts.radixPoint, buffer) && pos >= $.inArray(opts.radixPoint, buffer) || new RegExp("[-+]").test(charAtPos)) return {
  46. pos: pos
  47. };
  48. var cbuf = buffer.slice();
  49. charAtPos == opts.groupSeparator && (cbuf.splice(pos--, 1), charAtPos = cbuf[pos]),
  50. reformatOnly ? cbuf[pos] = "?" : cbuf.splice(pos, 0, "?");
  51. var bufVal = cbuf.join("");
  52. if (bufVal.length > 0 && opts.autoGroup || reformatOnly && -1 != bufVal.indexOf(opts.groupSeparator)) {
  53. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  54. needsRefresh = 0 == bufVal.indexOf(opts.groupSeparator), bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  55. var radixSplit = bufVal.split(opts.radixPoint);
  56. if (bufVal = "" == opts.radixPoint ? bufVal : radixSplit[0], bufVal != opts.prefix + "?0" && bufVal.length >= opts.groupSize + opts.prefix.length) {
  57. needsRefresh = !0;
  58. for (var reg = new RegExp("([-+]?[\\d?]+)([\\d?]{" + opts.groupSize + "})"); reg.test(bufVal); ) bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2"),
  59. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  60. }
  61. "" != opts.radixPoint && radixSplit.length > 1 && (bufVal += opts.radixPoint + radixSplit[1]);
  62. }
  63. buffer.length = bufVal.length;
  64. for (var i = 0, l = bufVal.length; l > i; i++) buffer[i] = bufVal.charAt(i);
  65. var newPos = $.inArray("?", buffer);
  66. return reformatOnly ? buffer[newPos] = charAtPos : buffer.splice(newPos, 1), {
  67. pos: newPos,
  68. refreshFromBuffer: needsRefresh
  69. };
  70. },
  71. onKeyDown: function(e, buffer, caretPos, opts) {
  72. if (opts.autoGroup && (e.keyCode == $.inputmask.keyCode.DELETE || e.keyCode == $.inputmask.keyCode.BACKSPACE)) {
  73. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  74. return rslt.caret = rslt.pos + 1, rslt;
  75. }
  76. },
  77. onKeyPress: function(e, buffer, caretPos, opts) {
  78. if (opts.autoGroup) {
  79. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  80. return rslt.caret = rslt.pos + 1, rslt;
  81. }
  82. },
  83. postProcessOnBlur: function(tmpBuffer, opts) {
  84. var tmpBufSplit = "" != opts.radixPoint ? tmpBuffer.join("").split(opts.radixPoint) : [ tmpBuffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
  85. matchRslt && "-0" == matchRslt[matchRslt.index] && (void 0 == matchRsltDigits || matchRsltDigits[matchRsltDigits.index].match(/^0+$/)) && tmpBuffer.splice(0, 1);
  86. var radixPosition = $.inArray(opts.radixPoint, tmpBuffer);
  87. if (-1 != radixPosition && isFinite(opts.digits)) for (var i = 1; i <= opts.digits; i++) (void 0 == tmpBuffer[radixPosition + i] || tmpBuffer[radixPosition + i] == opts.placeholder.charAt(0)) && (tmpBuffer[radixPosition + i] = "0");
  88. },
  89. regex: {
  90. integerPart: function() {
  91. return new RegExp("[-+]?\\d+");
  92. },
  93. integerNPart: function() {
  94. return new RegExp("\\d+");
  95. }
  96. },
  97. signHandler: function(chrs, maskset, pos, strict, opts) {
  98. if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs)) {
  99. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  100. if (matchRslt && matchRslt[matchRslt.index].length > 0 && ("0" !== matchRslt[matchRslt.index] || maskset.buffer && maskset._buffer && maskset.buffer.join("") != maskset._buffer.join(""))) return maskset.buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
  101. pos: matchRslt.index,
  102. c: chrs,
  103. remove: matchRslt.index,
  104. caret: pos
  105. } : maskset.buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
  106. remove: matchRslt.index,
  107. caret: pos - 1
  108. } : {
  109. pos: matchRslt.index,
  110. c: chrs,
  111. caret: pos + 1
  112. };
  113. }
  114. return !1;
  115. },
  116. radixHandler: function(chrs, maskset, pos, strict, opts) {
  117. if (!strict && chrs === opts.radixPoint && opts.digits > 0) {
  118. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  119. if (-1 != radixPos && maskset.validPositions[radixPos]) return maskset.validPositions[radixPos - 1] ? {
  120. caret: radixPos + 1
  121. } : {
  122. pos: integerValue.index,
  123. c: integerValue[0],
  124. caret: radixPos + 1
  125. };
  126. if (!integerValue || "0" == integerValue[0] && integerValue.index + 1 != pos) return maskset.buffer[integerValue ? integerValue.index : pos] = "0",
  127. {
  128. pos: (integerValue ? integerValue.index : pos) + 1
  129. };
  130. }
  131. return !1;
  132. },
  133. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  134. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  135. if (matchRslt && !strict && (-1 == radixPosition || matchRslt.index < radixPosition)) if ("0" == matchRslt[0] && pos >= opts.prefix.length) {
  136. if (-1 == radixPosition || radixPosition >= pos && void 0 == maskset.validPositions[radixPosition]) return maskset.buffer.splice(matchRslt.index, 1),
  137. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  138. pos: pos,
  139. remove: matchRslt.index
  140. };
  141. if (pos > matchRslt.index && radixPosition >= pos) return maskset.buffer.splice(matchRslt.index, 1),
  142. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  143. pos: pos,
  144. remove: matchRslt.index
  145. };
  146. if (void 0 == maskset.validPositions[radixPosition]) return maskset.buffer[pos] = chrs,
  147. {
  148. refreshFromBuffer: !0
  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. pos: pos,
  191. remove: pos
  192. });
  193. }
  194. return isValid;
  195. },
  196. cardinality: 1,
  197. prevalidator: null,
  198. placeholder: function(opts) {
  199. return opts.radixPoint;
  200. }
  201. }
  202. },
  203. insertMode: !0,
  204. autoUnmask: !1,
  205. onUnMask: function(maskedValue, unmaskedValue, opts) {
  206. var processValue = maskedValue.replace(opts.prefix, "");
  207. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  208. },
  209. isComplete: function(buffer, opts) {
  210. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  211. if (opts.postFormat(bufClone, 0, !0, opts), bufClone.join("") != maskedValue) return !1;
  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. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."),
  215. isFinite(processValue);
  216. },
  217. onBeforeMask: function(initialValue, opts) {
  218. if ("" != opts.radixPoint && isFinite(initialValue)) initialValue = initialValue.toString().replace(".", opts.radixPoint); else {
  219. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  220. dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  221. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""),
  222. 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"), "");
  223. }
  224. return 0 == opts.digits && (-1 != initialValue.indexOf(".") ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : -1 != initialValue.indexOf(",") && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))),
  225. initialValue;
  226. }
  227. },
  228. currency: {
  229. prefix: "$ ",
  230. groupSeparator: ",",
  231. alias: "numeric",
  232. placeholder: "0",
  233. autoGroup: !0,
  234. digits: 2,
  235. digitsOptional: !1,
  236. clearMaskOnLostFocus: !1
  237. },
  238. decimal: {
  239. alias: "numeric"
  240. },
  241. integer: {
  242. alias: "numeric",
  243. digits: "0",
  244. radixPoint: ""
  245. }
  246. }), $.fn.inputmask;
  247. });