jquery.inputmask.numeric.extensions.js 15 KB

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