jquery.inputmask.numeric.extensions.js 15 KB

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