jquery.inputmask.numeric.extensions.js 13 KB

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