jquery.inputmask.numeric.extensions.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. groupSize: 3,
  33. autoGroup: !1,
  34. allowPlus: !0,
  35. allowMinus: !0,
  36. integerDigits: "+",
  37. prefix: "",
  38. suffix: "",
  39. rightAlign: !0,
  40. decimalProtect: !0,
  41. postFormat: function(buffer, pos, reformatOnly, opts) {
  42. var needsRefresh = !1, charAtPos = buffer[pos];
  43. if ("" == opts.groupSeparator || -1 != $.inArray(opts.radixPoint, buffer) && pos >= $.inArray(opts.radixPoint, buffer) || new RegExp("[-+]").test(charAtPos)) return {
  44. pos: pos
  45. };
  46. var cbuf = buffer.slice();
  47. charAtPos == opts.groupSeparator && (cbuf.splice(pos--, 1), charAtPos = cbuf[pos]),
  48. reformatOnly ? cbuf[pos] = "?" : cbuf.splice(pos, 0, "?");
  49. var bufVal = cbuf.join("");
  50. if (opts.autoGroup || reformatOnly && -1 != bufVal.indexOf(opts.groupSeparator)) {
  51. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  52. needsRefresh = 0 == bufVal.indexOf(opts.groupSeparator), bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  53. var radixSplit = bufVal.split(opts.radixPoint);
  54. if (bufVal = radixSplit[0], bufVal != opts.prefix + "?0" && bufVal.length >= opts.groupSize + opts.prefix.length) {
  55. needsRefresh = !0;
  56. for (var reg = new RegExp("([-+]?[\\d?]+)([\\d?]{" + opts.groupSize + "})"); reg.test(bufVal); ) bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2"),
  57. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  58. }
  59. radixSplit.length > 1 && (bufVal += opts.radixPoint + radixSplit[1]);
  60. }
  61. buffer.length = bufVal.length;
  62. for (var i = 0, l = bufVal.length; l > i; i++) buffer[i] = bufVal.charAt(i);
  63. var newPos = $.inArray("?", buffer);
  64. return reformatOnly ? buffer[newPos] = charAtPos : buffer.splice(newPos, 1), {
  65. pos: newPos,
  66. refreshFromBuffer: needsRefresh
  67. };
  68. },
  69. onKeyDown: function(e, buffer, caretPos, opts) {
  70. if (e.keyCode == $.inputmask.keyCode.TAB && "0" != opts.placeholder.charAt(0)) {
  71. var radixPosition = $.inArray(opts.radixPoint, buffer);
  72. if (-1 != radixPosition && isFinite(opts.digits)) {
  73. for (var i = 1; i <= opts.digits; i++) (void 0 == buffer[radixPosition + i] || buffer[radixPosition + i] == opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
  74. return {
  75. refreshFromBuffer: {
  76. start: ++radixPosition,
  77. end: radixPosition + opts.digits
  78. }
  79. };
  80. }
  81. } else if (opts.autoGroup && (e.keyCode == $.inputmask.keyCode.DELETE || e.keyCode == $.inputmask.keyCode.BACKSPACE)) {
  82. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  83. return rslt.caret = rslt.pos + 1, rslt;
  84. }
  85. },
  86. onKeyPress: function(e, buffer, caretPos, opts) {
  87. if (opts.autoGroup) {
  88. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  89. return rslt.caret = rslt.pos + 1, rslt;
  90. }
  91. },
  92. regex: {
  93. integerPart: function() {
  94. return new RegExp("[-+]?\\d+");
  95. }
  96. },
  97. negationhandler: function(chrs, buffer, pos, strict, opts) {
  98. if (!strict && opts.allowMinus && "-" === chrs) {
  99. var matchRslt = buffer.join("").match(opts.regex.integerPart(opts));
  100. if (matchRslt.length > 0) return "+" == buffer[matchRslt.index] ? {
  101. pos: matchRslt.index,
  102. c: "-",
  103. remove: matchRslt.index,
  104. caret: pos
  105. } : "-" == buffer[matchRslt.index] ? {
  106. remove: matchRslt.index,
  107. caret: pos - 1
  108. } : {
  109. pos: matchRslt.index,
  110. c: "-",
  111. caret: pos + 1
  112. };
  113. }
  114. return !1;
  115. },
  116. radixhandler: function(chrs, maskset, pos, strict, opts) {
  117. if (!strict && chrs === opts.radixPoint) {
  118. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  119. if (-1 != 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. }
  127. return !1;
  128. },
  129. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  130. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  131. if (matchRslt && !strict && (-1 == radixPosition || matchRslt.index < radixPosition)) if (0 == matchRslt[0].indexOf("0") && pos >= opts.prefix.length) {
  132. if (-1 == radixPosition || radixPosition >= pos && void 0 == maskset.validPositions[radixPosition]) return maskset.buffer.splice(matchRslt.index, 1),
  133. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  134. pos: pos,
  135. remove: matchRslt.index
  136. };
  137. if (pos > matchRslt.index && radixPosition >= pos) return maskset.buffer.splice(matchRslt.index, 1),
  138. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  139. pos: pos,
  140. remove: matchRslt.index
  141. };
  142. } else if ("0" == chrs && pos <= matchRslt.index) return !1;
  143. return !0;
  144. },
  145. definitions: {
  146. "~": {
  147. validator: function(chrs, maskset, pos, strict, opts) {
  148. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  149. if (!isValid && (isValid = opts.radixhandler(chrs, maskset, pos, strict, opts),
  150. !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs),
  151. isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts),
  152. isValid === !0)))) {
  153. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  154. return opts.digitsOptional === !1 && pos > radixPosition && !strict ? {
  155. pos: pos,
  156. remove: pos
  157. } : {
  158. pos: pos
  159. };
  160. }
  161. return isValid;
  162. },
  163. cardinality: 1,
  164. prevalidator: null
  165. },
  166. "+": {
  167. validator: function(chrs, maskset, pos, strict, opts) {
  168. var signed = "[";
  169. return opts.allowMinus === !0 && (signed += "-"), opts.allowPlus === !0 && (signed += "+"),
  170. signed += "]", new RegExp(signed).test(chrs);
  171. },
  172. cardinality: 1,
  173. prevalidator: null,
  174. placeholder: ""
  175. },
  176. ":": {
  177. validator: function(chrs, maskset, pos, strict, opts) {
  178. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  179. if (!isValid) {
  180. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  181. isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {
  182. pos: pos,
  183. remove: pos
  184. });
  185. }
  186. return isValid;
  187. },
  188. cardinality: 1,
  189. prevalidator: null,
  190. placeholder: function(opts) {
  191. return opts.radixPoint;
  192. }
  193. }
  194. },
  195. insertMode: !0,
  196. autoUnmask: !1,
  197. onUnMask: function(maskedValue, unmaskedValue, opts) {
  198. var processValue = maskedValue.replace(opts.prefix, "");
  199. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  200. },
  201. isComplete: function(buffer, opts) {
  202. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  203. if (opts.postFormat(bufClone, 0, !0, opts), bufClone.join("") != maskedValue) return !1;
  204. var processValue = maskedValue.replace(opts.prefix, "");
  205. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""),
  206. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."),
  207. isFinite(processValue);
  208. },
  209. onBeforeMask: function(initialValue, opts) {
  210. if (isFinite(initialValue)) return initialValue.toString().replace(".", opts.radixPoint);
  211. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  212. return dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  213. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length && (initialValue = initialValue.replace(/,/g, ""),
  214. initialValue = initialValue.replace(".", opts.radixPoint)) : initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""),
  215. initialValue;
  216. }
  217. },
  218. currency: {
  219. prefix: "$ ",
  220. groupSeparator: ",",
  221. radixPoint: ".",
  222. alias: "numeric",
  223. placeholder: "0",
  224. autoGroup: !0,
  225. digits: 2,
  226. digitsOptional: !1,
  227. clearMaskOnLostFocus: !1,
  228. decimalProtect: !0
  229. },
  230. decimal: {
  231. alias: "numeric"
  232. },
  233. integer: {
  234. alias: "numeric",
  235. digits: "0"
  236. }
  237. }), $.fn.inputmask;
  238. });