jquery.inputmask.numeric.extensions.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. Input Mask plugin extensions
  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: 0.0.0
  7. Optional extensions on the jquery.inputmask base
  8. */
  9. (function ($) {
  10. //number aliases
  11. $.extend($.inputmask.defaults.aliases, {
  12. 'numeric': {
  13. mask: function (opts) {
  14. function autoEscape(txt) {
  15. var escapedTxt = "";
  16. for (var i = 0; i < txt.length; i++) {
  17. escapedTxt += opts.definitions[txt[i]] ? "\\" + txt[i] : txt[i];
  18. }
  19. return escapedTxt;
  20. }
  21. if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
  22. opts.integerDigits = opts.repeat;
  23. }
  24. opts.repeat = 0;
  25. if (opts.groupSeparator == opts.radixPoint) { //treat equal separator and radixpoint
  26. if (opts.radixPoint == ".")
  27. opts.groupSeparator = ",";
  28. else if (opts.radixPoint == ",")
  29. opts.groupSeparator = ".";
  30. else opts.groupSeparator = "";
  31. }
  32. if (opts.groupSeparator === " ") { //prevent conflict with default skipOptionalPartCharacter
  33. opts.skipOptionalPartCharacter = undefined;
  34. }
  35. opts.autoGroup = opts.autoGroup && opts.groupSeparator != "";
  36. if (opts.autoGroup) {
  37. if (typeof opts.groupSize == "string" && isFinite(opts.groupSize)) opts.groupSize = parseInt(opts.groupSize);
  38. if (isFinite(opts.integerDigits)) {
  39. var seps = Math.floor(opts.integerDigits / opts.groupSize);
  40. var mod = opts.integerDigits % opts.groupSize;
  41. opts.integerDigits = parseInt(opts.integerDigits) + (mod == 0 ? seps - 1 : seps);
  42. }
  43. }
  44. opts.definitions[";"] = opts.definitions["~"]; //clone integer def for decimals
  45. var mask = autoEscape(opts.prefix);
  46. mask += "[+]";
  47. mask += "~{1," + opts.integerDigits + "}";
  48. if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  49. if (opts.digitsOptional)
  50. mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}]";
  51. else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}";
  52. }
  53. mask += autoEscape(opts.suffix);
  54. return mask;
  55. },
  56. placeholder: "",
  57. greedy: false,
  58. digits: "*", //number of fractionalDigits
  59. digitsOptional: true,
  60. groupSeparator: "",//",", // | "."
  61. radixPoint: ".",
  62. radixFocus: true,
  63. groupSize: 3,
  64. autoGroup: false,
  65. allowPlus: true,
  66. allowMinus: true,
  67. integerDigits: "+", //number of integerDigits
  68. prefix: "",
  69. suffix: "",
  70. rightAlign: true,
  71. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  72. postFormat: function (buffer, pos, reformatOnly, opts) { //this needs to be removed // this is crap
  73. //position overflow corrections
  74. pos = pos >= buffer.length ? buffer.length - 1 : (pos < opts.prefix.length ? opts.prefix.length : pos);
  75. var needsRefresh = false, charAtPos = buffer[pos];
  76. if (opts.groupSeparator == "" ||
  77. ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer)) ||
  78. new RegExp('[-\+]').test(charAtPos)
  79. )
  80. return { pos: pos };
  81. var cbuf = buffer.slice();
  82. if (charAtPos == opts.groupSeparator) {
  83. cbuf.splice(pos--, 1);
  84. charAtPos = cbuf[pos];
  85. }
  86. if (reformatOnly) cbuf[pos] = "?"; else cbuf.splice(pos, 0, "?"); //set position indicator
  87. var bufVal = cbuf.join('');
  88. if (bufVal.length > 0 && opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  89. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  90. needsRefresh = bufVal.indexOf(opts.groupSeparator) == 0;
  91. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  92. var radixSplit = bufVal.split(opts.radixPoint);
  93. bufVal = opts.radixPoint == "" ? bufVal : radixSplit[0];
  94. if (bufVal != (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
  95. //needsRefresh = true;
  96. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  97. while (reg.test(bufVal)) {
  98. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  99. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  100. }
  101. }
  102. if (opts.radixPoint != "" && radixSplit.length > 1)
  103. bufVal += opts.radixPoint + radixSplit[1];
  104. }
  105. needsRefresh = buffer.join('') != bufVal;
  106. buffer.length = bufVal.length; //align the length
  107. for (var i = 0, l = bufVal.length; i < l; i++) {
  108. buffer[i] = bufVal.charAt(i);
  109. }
  110. var newPos = $.inArray("?", buffer);
  111. if (reformatOnly) buffer[newPos] = charAtPos; else buffer.splice(newPos, 1);
  112. return { pos: newPos, "refreshFromBuffer": needsRefresh };
  113. },
  114. onBeforeWrite: function (e, buffer, caretPos, opts) {
  115. if (e && e.type == "blur") {
  116. var tmpBufSplit = opts.radixPoint != "" ? buffer.join('').split(opts.radixPoint) : [buffer.join('')],
  117. matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)),
  118. matchRsltDigits = tmpBufSplit.length == 2 ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : undefined;
  119. if (matchRslt && matchRslt[0] == "-0" && (matchRsltDigits == undefined || matchRsltDigits[0].match(/^0+$/))) {
  120. buffer.splice(0, 1);
  121. }
  122. var radixPosition = $.inArray(opts.radixPoint, buffer);
  123. if (radixPosition != -1 && isFinite(opts.digits) && !opts.digitsOptional) {
  124. for (var i = 1; i <= opts.digits; i++) {
  125. if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == opts.placeholder.charAt(0)) buffer[radixPosition + i] = "0";
  126. }
  127. return { "refreshFromBuffer": true, "buffer": buffer };
  128. }
  129. }
  130. if (opts.autoGroup) {
  131. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  132. rslt.caret = rslt.pos + 1;
  133. return rslt;
  134. }
  135. },
  136. regex: {
  137. integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); },
  138. integerNPart: function (opts) { return new RegExp('[\\d' + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + ']+'); }
  139. },
  140. signHandler: function (chrs, maskset, pos, strict, opts) {
  141. if (!strict && (opts.allowMinus && chrs === "-" || opts.allowPlus && chrs === "+")) {
  142. var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts));
  143. if (matchRslt && matchRslt[0].length > 0 && (matchRslt[0] !== "0" || (maskset.buffer && maskset._buffer && maskset.buffer.join('') != maskset._buffer.join('')))) {
  144. if (maskset.buffer[matchRslt.index] == (chrs === "-" ? "+" : "-")) {
  145. return { "pos": matchRslt.index, "c": chrs, "remove": matchRslt.index, "caret": pos };
  146. } else if (maskset.buffer[matchRslt.index] == (chrs === "-" ? "-" : "+")) {
  147. return { "remove": matchRslt.index, "caret": pos - 1 };
  148. } else {
  149. return { "pos": matchRslt.index, "c": chrs, "caret": pos + 1 };
  150. }
  151. }
  152. }
  153. return false;
  154. },
  155. radixHandler: function (chrs, maskset, pos, strict, opts) {
  156. if (!strict && chrs === opts.radixPoint && opts.digits > 0) {
  157. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join('').match(opts.regex.integerPart(opts));
  158. if (radixPos != -1 && maskset["validPositions"][radixPos]) {
  159. if (maskset["validPositions"][radixPos - 1])
  160. return { "caret": radixPos + 1 };
  161. else return { "pos": integerValue.index, c: integerValue[0], "caret": radixPos + 1 };
  162. } else if (!integerValue || (integerValue["0"] == "0" && (integerValue.index + 1) != pos)) {
  163. maskset.buffer[integerValue ? integerValue.index : pos] = "0";
  164. return { "pos": (integerValue ? integerValue.index : pos) + 1 };
  165. }
  166. }
  167. return false;
  168. },
  169. leadingZeroHandler: function (chrs, maskset, pos, strict, opts) {
  170. var matchRslt = maskset.buffer.join('').match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  171. if (matchRslt && !strict && (radixPosition == -1 || pos <= radixPosition)) {
  172. if (matchRslt["0"].indexOf("0") == 0) {
  173. if (pos < opts.prefix.length) pos = matchRslt.index; //position
  174. var _radixPosition = $.inArray(opts.radixPoint, maskset._buffer);
  175. var digitsMatch = maskset._buffer && maskset.buffer.slice(radixPosition).join('') == maskset._buffer.slice(_radixPosition).join('');
  176. var integerMatch = maskset._buffer && maskset.buffer.slice(matchRslt.index, radixPosition).join('') == maskset._buffer.slice(opts.prefix.length, _radixPosition).join('');
  177. if (radixPosition == -1 || digitsMatch && integerMatch) {
  178. maskset.buffer.splice(matchRslt.index, 1);
  179. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  180. return { "pos": pos, "remove": matchRslt.index };
  181. } else if (matchRslt.index + 1 == pos || chrs == "0") {
  182. maskset.buffer.splice(matchRslt.index, 1);
  183. pos = matchRslt.index;
  184. return { "pos": pos, "remove": matchRslt.index };
  185. }
  186. } else if (chrs === "0" && pos <= matchRslt.index) {
  187. return false;
  188. }
  189. }
  190. return true;
  191. },
  192. definitions: {
  193. '~': {
  194. validator: function (chrs, maskset, pos, strict, opts) {
  195. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  196. if (!isValid) {
  197. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  198. if (!isValid) {
  199. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  200. if (isValid === true) {
  201. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  202. if (isValid === true) {
  203. //handle overwrite when fixed precision
  204. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  205. if (opts.digitsOptional === false && pos > radixPosition && !strict) {
  206. isValid = { "pos": pos, "remove": pos };
  207. } else isValid = { pos: pos };
  208. }
  209. }
  210. }
  211. }
  212. return isValid;
  213. },
  214. cardinality: 1,
  215. prevalidator: null
  216. },
  217. '+': {
  218. validator: function (chrs, maskset, pos, strict, opts) {
  219. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  220. if (!isValid) {
  221. isValid = (opts.allowMinus && chrs == "-") || (opts.allowPlus && chrs == "+");
  222. }
  223. return isValid;
  224. },
  225. cardinality: 1,
  226. prevalidator: null,
  227. placeholder: ''
  228. },
  229. ':': {
  230. validator: function (chrs, maskset, pos, strict, opts) {
  231. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  232. if (!isValid) {
  233. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  234. isValid = new RegExp(radix).test(chrs);
  235. if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
  236. isValid = { "caret": pos + 1 };
  237. }
  238. }
  239. return isValid;
  240. },
  241. cardinality: 1,
  242. prevalidator: null,
  243. placeholder: function (opts) { return opts.radixPoint; }
  244. }
  245. },
  246. insertMode: true,
  247. autoUnmask: false,
  248. onUnMask: function (maskedValue, unmaskedValue, opts) {
  249. var processValue = maskedValue.replace(opts.prefix, "");
  250. processValue = processValue.replace(opts.suffix, "");
  251. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  252. //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  253. return processValue;
  254. },
  255. isComplete: function (buffer, opts) {
  256. var maskedValue = buffer.join(''), bufClone = buffer.slice();
  257. //verify separator positions
  258. opts.postFormat(bufClone, 0, true, opts);
  259. if (bufClone.join('') != maskedValue) return false;
  260. var processValue = maskedValue.replace(opts.prefix, "");
  261. processValue = processValue.replace(opts.suffix, "");
  262. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  263. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  264. return isFinite(processValue);
  265. },
  266. onBeforeMask: function (initialValue, opts) {
  267. if (opts.radixPoint != "" && isFinite(initialValue)) {
  268. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  269. } else {
  270. var kommaMatches = initialValue.match(/,/g);
  271. var dotMatches = initialValue.match(/\./g);
  272. if (dotMatches && kommaMatches) {
  273. if (dotMatches.length > kommaMatches.length) {
  274. initialValue = initialValue.replace(/\./g, "");
  275. initialValue = initialValue.replace(",", opts.radixPoint);
  276. } else if (kommaMatches.length > dotMatches.length) {
  277. initialValue = initialValue.replace(/,/g, "");
  278. initialValue = initialValue.replace(".", opts.radixPoint);
  279. } else { //equal
  280. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  281. }
  282. } else {
  283. initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  284. }
  285. }
  286. if (opts.digits == 0) {
  287. if (initialValue.indexOf(".") != -1) {
  288. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  289. } else if (initialValue.indexOf(",") != -1) {
  290. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  291. }
  292. }
  293. return initialValue;
  294. },
  295. canClearPosition: function (maskset, position, lvp, opts) {
  296. var canClear = maskset["validPositions"][position].input != opts.radixPoint || position == lvp;
  297. if (canClear) {
  298. var matchRslt = maskset.buffer.join('').match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  299. if (matchRslt && (radixPosition == -1 || position <= radixPosition)) {
  300. if (matchRslt["0"].indexOf("0") == 0) {
  301. canClear = matchRslt.index != position;
  302. } else if (radixPosition != -1 && matchRslt["0"].length == 1 && matchRslt.index == position) {
  303. maskset["validPositions"][position].input = "0";
  304. canClear = false;
  305. }
  306. }
  307. }
  308. if (canClear && maskset["validPositions"][position + 1] && maskset["validPositions"][position + 1].input == opts.groupSeparator) {
  309. delete maskset["validPositions"][position + 1];
  310. }
  311. return canClear;
  312. }
  313. },
  314. 'currency': {
  315. prefix: "$ ",
  316. groupSeparator: ",",
  317. alias: "numeric",
  318. placeholder: "0",
  319. autoGroup: true,
  320. digits: 2,
  321. digitsOptional: false,
  322. clearMaskOnLostFocus: false
  323. },
  324. 'decimal': {
  325. alias: "numeric"
  326. },
  327. 'integer': {
  328. alias: "numeric",
  329. digits: "0",
  330. radixPoint: ""
  331. }
  332. });
  333. return $.fn.inputmask;
  334. })(jQuery);