jquery.inputmask.numeric.extensions.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*!
  2. * jquery.inputmask.numeric.extensions.js
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2015 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.1.55
  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.radixFocus = opts.radixFocus && "0" == opts.placeholder, 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), opts.greedy = !1, mask;
  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. min: void 0,
  48. max: void 0,
  49. postFormat: function(buffer, pos, reformatOnly, opts) {
  50. pos = pos >= buffer.length ? buffer.length - 1 : pos < opts.prefix.length ? opts.prefix.length : pos;
  51. var needsRefresh = !1, charAtPos = buffer[pos];
  52. if ("" == opts.groupSeparator || -1 != $.inArray(opts.radixPoint, buffer) && pos >= $.inArray(opts.radixPoint, buffer) || new RegExp("[-+]").test(charAtPos)) return {
  53. pos: pos
  54. };
  55. var cbuf = buffer.slice();
  56. charAtPos == opts.groupSeparator && (cbuf.splice(pos--, 1), charAtPos = cbuf[pos]),
  57. reformatOnly ? cbuf[pos] = "?" : cbuf.splice(pos, 0, "?");
  58. var bufVal = cbuf.join(""), bufValOrigin = bufVal;
  59. if (bufVal.length > 0 && opts.autoGroup || reformatOnly && -1 != bufVal.indexOf(opts.groupSeparator)) {
  60. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  61. needsRefresh = 0 == bufVal.indexOf(opts.groupSeparator), bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  62. var radixSplit = bufVal.split(opts.radixPoint);
  63. 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"),
  64. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  65. "" != opts.radixPoint && radixSplit.length > 1 && (bufVal += opts.radixPoint + radixSplit[1]);
  66. }
  67. needsRefresh = bufValOrigin != bufVal, 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. onBeforeWrite: function(e, buffer, caretPos, opts) {
  76. if (e && "blur" == e.type) {
  77. var tmpBufSplit = "" != opts.radixPoint ? buffer.join("").split(opts.radixPoint) : [ buffer.join("") ], matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)), matchRsltDigits = 2 == tmpBufSplit.length ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : void 0;
  78. matchRslt && "-0" == matchRslt[0] && (void 0 == matchRsltDigits || matchRsltDigits[0].match(/^0+$/)) && buffer.splice(matchRslt.index, 1);
  79. var radixPosition = $.inArray(opts.radixPoint, buffer);
  80. if (-1 != radixPosition && isFinite(opts.digits) && !opts.digitsOptional) {
  81. for (var i = 1; i <= opts.digits; i++) (void 0 == buffer[radixPosition + i] || buffer[radixPosition + i] == opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
  82. return {
  83. refreshFromBuffer: !0,
  84. buffer: buffer
  85. };
  86. }
  87. }
  88. if (opts.autoGroup) {
  89. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  90. return rslt.caret = 0 == caretPos ? caretPos : rslt.pos + 1, rslt;
  91. }
  92. },
  93. regex: {
  94. integerPart: function() {
  95. return new RegExp("[-+]?\\d+");
  96. },
  97. integerNPart: function(opts) {
  98. return new RegExp("[\\d" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]+");
  99. }
  100. },
  101. signHandler: function(chrs, maskset, pos, strict, opts) {
  102. if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs)) {
  103. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  104. if (matchRslt && matchRslt[0].length > 0) return maskset.buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
  105. pos: matchRslt.index,
  106. c: chrs,
  107. remove: matchRslt.index,
  108. caret: pos
  109. } : maskset.buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
  110. remove: matchRslt.index,
  111. caret: pos - 1
  112. } : {
  113. pos: matchRslt.index,
  114. c: chrs,
  115. caret: pos + 1
  116. };
  117. }
  118. return !1;
  119. },
  120. radixHandler: function(chrs, maskset, pos, strict, opts) {
  121. if (!strict && chrs === opts.radixPoint && opts.digits > 0) {
  122. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  123. if (-1 != radixPos && maskset.validPositions[radixPos]) return maskset.validPositions[radixPos - 1] ? {
  124. caret: radixPos + 1
  125. } : {
  126. pos: integerValue.index,
  127. c: integerValue[0],
  128. caret: radixPos + 1
  129. };
  130. if (!integerValue || "0" == integerValue[0] && integerValue.index + 1 != pos) return maskset.buffer[integerValue ? integerValue.index : pos] = "0",
  131. {
  132. pos: (integerValue ? integerValue.index : pos) + 1
  133. };
  134. }
  135. return !1;
  136. },
  137. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  138. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  139. if (matchRslt && !strict && (-1 == radixPosition || radixPosition >= pos)) if (0 == matchRslt[0].indexOf("0")) {
  140. pos < opts.prefix.length && (pos = matchRslt.index);
  141. var _radixPosition = $.inArray(opts.radixPoint, maskset._buffer), digitsMatch = maskset._buffer && maskset.buffer.slice(radixPosition).join("") == maskset._buffer.slice(_radixPosition).join("") || 0 == parseInt(maskset.buffer.slice(radixPosition + 1).join("")), integerMatch = maskset._buffer && maskset.buffer.slice(matchRslt.index, radixPosition).join("") == maskset._buffer.slice(opts.prefix.length, _radixPosition).join("") || "0" == maskset.buffer.slice(matchRslt.index, radixPosition).join("");
  142. if (-1 == radixPosition || digitsMatch && integerMatch) return maskset.buffer.splice(matchRslt.index, 1),
  143. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  144. pos: pos,
  145. remove: matchRslt.index
  146. };
  147. if (matchRslt.index + 1 == pos || "0" == chrs) return maskset.buffer.splice(matchRslt.index, 1),
  148. pos = matchRslt.index, {
  149. pos: pos,
  150. remove: matchRslt.index
  151. };
  152. } else if ("0" === chrs && pos <= matchRslt.index) return !1;
  153. return !0;
  154. },
  155. verifyRange: function() {
  156. return !0;
  157. },
  158. definitions: {
  159. "~": {
  160. validator: function(chrs, maskset, pos, strict, opts) {
  161. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  162. if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts),
  163. !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs),
  164. isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts),
  165. isValid === !0)))) {
  166. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  167. isValid = opts.digitsOptional === !1 && pos > radixPosition && !strict ? {
  168. pos: pos,
  169. remove: pos
  170. } : {
  171. pos: pos
  172. };
  173. }
  174. return isValid;
  175. },
  176. cardinality: 1,
  177. prevalidator: null
  178. },
  179. "+": {
  180. validator: function(chrs, maskset, pos, strict, opts) {
  181. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  182. return isValid || (isValid = opts.allowMinus && "-" == chrs || opts.allowPlus && "+" == chrs),
  183. isValid;
  184. },
  185. cardinality: 1,
  186. prevalidator: null,
  187. placeholder: ""
  188. },
  189. ":": {
  190. validator: function(chrs, maskset, pos, strict, opts) {
  191. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  192. if (!isValid) {
  193. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  194. isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {
  195. caret: pos + 1
  196. });
  197. }
  198. return isValid;
  199. },
  200. cardinality: 1,
  201. prevalidator: null,
  202. placeholder: function(opts) {
  203. return opts.radixPoint;
  204. }
  205. }
  206. },
  207. insertMode: !0,
  208. autoUnmask: !1,
  209. onUnMask: function(maskedValue, unmaskedValue, opts) {
  210. var processValue = maskedValue.replace(opts.prefix, "");
  211. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  212. },
  213. isComplete: function(buffer, opts) {
  214. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  215. if (opts.postFormat(bufClone, 0, !0, opts), bufClone.join("") != maskedValue) return !1;
  216. var processValue = maskedValue.replace(opts.prefix, "");
  217. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""),
  218. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."),
  219. isFinite(processValue);
  220. },
  221. onBeforeMask: function(initialValue, opts) {
  222. if ("" != opts.radixPoint && isFinite(initialValue)) initialValue = initialValue.toString().replace(".", opts.radixPoint); else {
  223. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  224. dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  225. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length ? (initialValue = initialValue.replace(/,/g, ""),
  226. 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"), "");
  227. }
  228. return 0 == opts.digits && (-1 != initialValue.indexOf(".") ? initialValue = initialValue.substring(0, initialValue.indexOf(".")) : -1 != initialValue.indexOf(",") && (initialValue = initialValue.substring(0, initialValue.indexOf(",")))),
  229. initialValue;
  230. },
  231. canClearPosition: function(maskset, position, lvp, strict, opts) {
  232. var positionInput = maskset.validPositions[position].input, canClear = positionInput != opts.radixPoint && isFinite(positionInput) || position == lvp || positionInput == opts.groupSeparator;
  233. if (canClear && isFinite(positionInput)) {
  234. if (!strict) for (var pos = position + 1; maskset.validPositions[pos] && (maskset.validPositions[pos].input == opts.groupSeparator || "0" == maskset.validPositions[pos].input); ) delete maskset.validPositions[pos],
  235. pos++;
  236. var buffer = [];
  237. for (var vp in maskset.validPositions) buffer.push(maskset.validPositions[vp].input);
  238. var matchRslt = buffer.join("").match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  239. if (matchRslt && (-1 == radixPosition || radixPosition >= position)) if (0 == matchRslt[0].indexOf("0")) canClear = matchRslt.index != position || -1 == radixPosition; else {
  240. var intPart = parseInt(matchRslt[0].replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""));
  241. -1 != radixPosition && 10 > intPart && "0" == opts.placeholder.charAt(0) && (maskset.validPositions[position].input = "0",
  242. maskset.p = opts.prefix.length + 1, canClear = !1);
  243. }
  244. }
  245. return canClear;
  246. }
  247. },
  248. currency: {
  249. prefix: "$ ",
  250. groupSeparator: ",",
  251. alias: "numeric",
  252. placeholder: "0",
  253. autoGroup: !0,
  254. digits: 2,
  255. digitsOptional: !1,
  256. clearMaskOnLostFocus: !1
  257. },
  258. decimal: {
  259. alias: "numeric"
  260. },
  261. integer: {
  262. alias: "numeric",
  263. digits: "0",
  264. radixPoint: ""
  265. }
  266. }), $.fn.inputmask;
  267. });