jquery.inputmask.numeric.extensions.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.34
  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. postProcessOnBlur: function(tmpBuffer, opts) {
  94. 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;
  95. matchRslt && "-0" == matchRslt[matchRslt.index] && (void 0 == matchRsltDigits || matchRsltDigits[matchRsltDigits.index].match(/^0+$/)) && tmpBuffer.splice(0, 1);
  96. },
  97. regex: {
  98. integerPart: function() {
  99. return new RegExp("[-+]?\\d+");
  100. },
  101. integerNPart: function() {
  102. return new RegExp("\\d+");
  103. }
  104. },
  105. signHandler: function(chrs, maskset, pos, strict, opts) {
  106. if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs)) {
  107. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  108. 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 ? "+" : "-") ? {
  109. pos: matchRslt.index,
  110. c: chrs,
  111. remove: matchRslt.index,
  112. caret: pos
  113. } : maskset.buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
  114. remove: matchRslt.index,
  115. caret: pos - 1
  116. } : {
  117. pos: matchRslt.index,
  118. c: chrs,
  119. caret: pos + 1
  120. };
  121. }
  122. return !1;
  123. },
  124. radixHandler: function(chrs, maskset, pos, strict, opts) {
  125. if (!strict && chrs === opts.radixPoint && opts.digits > 0) {
  126. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  127. if (-1 != radixPos && maskset.validPositions[radixPos]) return maskset.validPositions[radixPos - 1] ? {
  128. caret: radixPos + 1
  129. } : {
  130. pos: integerValue.index,
  131. c: integerValue[0],
  132. caret: radixPos + 1
  133. };
  134. if (!integerValue || "0" == integerValue[0]) return maskset.buffer[integerValue ? integerValue.index : pos] = "0",
  135. {
  136. pos: (integerValue ? integerValue.index : pos) + 1
  137. };
  138. }
  139. return !1;
  140. },
  141. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  142. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  143. if (matchRslt && !strict && (-1 == radixPosition || matchRslt.index < radixPosition)) if ("0" == matchRslt[0] && pos >= opts.prefix.length) {
  144. if (-1 == radixPosition || radixPosition >= pos && void 0 == maskset.validPositions[radixPosition]) return maskset.buffer.splice(matchRslt.index, 1),
  145. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  146. pos: pos,
  147. remove: matchRslt.index
  148. };
  149. if (pos > matchRslt.index && radixPosition >= pos) return maskset.buffer.splice(matchRslt.index, 1),
  150. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  151. pos: pos,
  152. remove: matchRslt.index
  153. };
  154. if (void 0 == maskset.validPositions[radixPosition]) return maskset.buffer[pos] = chrs,
  155. {
  156. refreshFromBuffer: !0
  157. };
  158. } else if ("0" == chrs && pos <= matchRslt.index) return !1;
  159. return !0;
  160. },
  161. definitions: {
  162. "~": {
  163. validator: function(chrs, maskset, pos, strict, opts) {
  164. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  165. if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts),
  166. !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs),
  167. isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts),
  168. isValid === !0)))) {
  169. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  170. isValid = opts.digitsOptional === !1 && pos > radixPosition && !strict ? {
  171. pos: pos,
  172. remove: pos
  173. } : {
  174. pos: pos
  175. };
  176. }
  177. return isValid;
  178. },
  179. cardinality: 1,
  180. prevalidator: null
  181. },
  182. "+": {
  183. validator: function(chrs, maskset, pos, strict, opts) {
  184. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  185. return isValid || (isValid = opts.allowMinus && "-" == chrs || opts.allowPlus && "+" == chrs),
  186. isValid;
  187. },
  188. cardinality: 1,
  189. prevalidator: null,
  190. placeholder: ""
  191. },
  192. ":": {
  193. validator: function(chrs, maskset, pos, strict, opts) {
  194. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  195. if (!isValid) {
  196. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  197. isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {
  198. pos: pos,
  199. remove: pos
  200. });
  201. }
  202. return isValid;
  203. },
  204. cardinality: 1,
  205. prevalidator: null,
  206. placeholder: function(opts) {
  207. return opts.radixPoint;
  208. }
  209. }
  210. },
  211. insertMode: !0,
  212. autoUnmask: !1,
  213. onUnMask: function(maskedValue, unmaskedValue, opts) {
  214. var processValue = maskedValue.replace(opts.prefix, "");
  215. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  216. },
  217. isComplete: function(buffer, opts) {
  218. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  219. if (opts.postFormat(bufClone, 0, !0, opts), bufClone.join("") != maskedValue) return !1;
  220. var processValue = maskedValue.replace(opts.prefix, "");
  221. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""),
  222. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."),
  223. isFinite(processValue);
  224. },
  225. onBeforeMask: function(initialValue, opts) {
  226. if ("" != opts.radixPoint && isFinite(initialValue)) initialValue = initialValue.toString().replace(".", opts.radixPoint); else {
  227. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  228. dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  229. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""),
  230. 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"), "");
  231. }
  232. return 0 == opts.digits && (-1 != initialValue.indexOf(".") ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : -1 != initialValue.indexOf(",") && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))),
  233. initialValue;
  234. }
  235. },
  236. currency: {
  237. prefix: "$ ",
  238. groupSeparator: ",",
  239. alias: "numeric",
  240. placeholder: "0",
  241. autoGroup: !0,
  242. digits: 2,
  243. digitsOptional: !1,
  244. clearMaskOnLostFocus: !1
  245. },
  246. decimal: {
  247. alias: "numeric"
  248. },
  249. integer: {
  250. alias: "numeric",
  251. digits: "0",
  252. radixPoint: ""
  253. }
  254. }), $.fn.inputmask;
  255. });