jquery.inputmask.numeric.extensions.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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) {if (typeof define === 'function' && define.amd) {define(["jquery","./jquery.inputmask"], factory);} else {factory(jQuery);}}/*
  9. Input Mask plugin extensions
  10. http://github.com/RobinHerbots/jquery.inputmask
  11. Copyright (c) 2010 - 2014 Robin Herbots
  12. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  13. Version: 0.0.0
  14. Optional extensions on the jquery.inputmask base
  15. */
  16. (function ($) {
  17. //number aliases
  18. $.extend($.inputmask.defaults.aliases, {
  19. 'numeric': {
  20. mask: function (opts) {
  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 && isFinite(opts.integerDigits)) {
  37. var seps = Math.floor(opts.integerDigits / opts.groupSize);
  38. var mod = opts.integerDigits % opts.groupSize;
  39. opts.integerDigits += mod == 0 ? seps - 1 : seps;
  40. }
  41. opts.definitions[";"] = opts.definitions["~"]; //clone integer def for decimals
  42. var mask = opts.prefix;
  43. mask += "[+]";
  44. mask += "~{1," + opts.integerDigits + "}";
  45. if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  46. if (opts.digitsOptional)
  47. mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}]";
  48. else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}";
  49. }
  50. mask += opts.suffix;
  51. return mask;
  52. },
  53. placeholder: "",
  54. greedy: false,
  55. digits: "*", //number of fractionalDigits
  56. digitsOptional: true,
  57. groupSeparator: "",//",", // | "."
  58. radixPoint: ".",
  59. groupSize: 3,
  60. autoGroup: false,
  61. allowPlus: true,
  62. allowMinus: true,
  63. integerDigits: "+", //number of integerDigits
  64. prefix: "",
  65. suffix: "",
  66. rightAlign: true,
  67. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  68. postFormat: function (buffer, pos, reformatOnly, opts) { //this needs to be removed // this is crap
  69. var needsRefresh = false, charAtPos = buffer[pos];
  70. if (opts.groupSeparator == "" ||
  71. ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer)) ||
  72. new RegExp('[-\+]').test(charAtPos)
  73. ) return { pos: pos };
  74. var cbuf = buffer.slice();
  75. if (charAtPos == opts.groupSeparator) {
  76. cbuf.splice(pos--, 1);
  77. charAtPos = cbuf[pos];
  78. }
  79. if (reformatOnly) cbuf[pos] = "?"; else cbuf.splice(pos, 0, "?"); //set position indicator
  80. var bufVal = cbuf.join('');
  81. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  82. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  83. needsRefresh = bufVal.indexOf(opts.groupSeparator) == 0;
  84. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  85. var radixSplit = bufVal.split(opts.radixPoint);
  86. bufVal = radixSplit[0];
  87. if (bufVal != (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
  88. needsRefresh = true;
  89. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  90. while (reg.test(bufVal)) {
  91. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  92. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  93. }
  94. }
  95. if (radixSplit.length > 1)
  96. bufVal += opts.radixPoint + radixSplit[1];
  97. }
  98. buffer.length = bufVal.length; //align the length
  99. for (var i = 0, l = bufVal.length; i < l; i++) {
  100. buffer[i] = bufVal.charAt(i);
  101. }
  102. var newPos = $.inArray("?", buffer);
  103. if (reformatOnly) buffer[newPos] = charAtPos; else buffer.splice(newPos, 1);
  104. return { pos: newPos, "refreshFromBuffer": needsRefresh };
  105. },
  106. onKeyDown: function (e, buffer, caretPos, opts) {
  107. if (e.keyCode == $.inputmask.keyCode.TAB && opts.placeholder.charAt(0) != "0") {
  108. var radixPosition = $.inArray(opts.radixPoint, buffer);
  109. if (radixPosition != -1 && isFinite(opts.digits)) {
  110. for (var i = 1; i <= opts.digits; i++) {
  111. if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == opts.placeholder.charAt(0)) buffer[radixPosition + i] = "0";
  112. }
  113. return { "refreshFromBuffer": { start: ++radixPosition, end: radixPosition + opts.digits } };
  114. }
  115. } else if (opts.autoGroup && (e.keyCode == $.inputmask.keyCode.DELETE || e.keyCode == $.inputmask.keyCode.BACKSPACE)) {
  116. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  117. rslt.caret = rslt.pos + 1;
  118. return rslt;
  119. }
  120. },
  121. onKeyPress: function (e, buffer, caretPos, opts) {
  122. if (opts.autoGroup /*&& String.fromCharCode(k) == opts.radixPoint*/) {
  123. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  124. rslt.caret = rslt.pos + 1;
  125. return rslt;
  126. }
  127. },
  128. regex: {
  129. integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
  130. },
  131. negationhandler: function (chrs, buffer, pos, strict, opts) {
  132. if (!strict && opts.allowMinus && chrs === "-") {
  133. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
  134. if (matchRslt.length > 0) {
  135. if (buffer[matchRslt.index] == "+") {
  136. return { "pos": matchRslt.index, "c": "-", "remove": matchRslt.index, "caret": pos };
  137. } else if (buffer[matchRslt.index] == "-") {
  138. return { "remove": matchRslt.index, "caret": pos - 1 };
  139. } else {
  140. return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
  141. }
  142. }
  143. }
  144. return false;
  145. },
  146. radixhandler: function (chrs, maskset, pos, strict, opts) {
  147. if (!strict && chrs === opts.radixPoint) {
  148. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join('').match(opts.regex.integerPart(opts));
  149. if (radixPos != -1) {
  150. if (maskset["validPositions"][radixPos - 1])
  151. return { "caret": radixPos + 1 };
  152. else return { "pos": integerValue.index, c: integerValue[0], "caret": radixPos + 1 };
  153. }
  154. }
  155. return false;
  156. },
  157. leadingZeroHandler: function (chrs, maskset, pos, strict, opts) {
  158. var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  159. if (matchRslt && !strict && (radixPosition == -1 || matchRslt.index < radixPosition)) {
  160. if (matchRslt["0"].indexOf("0") == 0 && pos >= opts.prefix.length) {
  161. if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
  162. maskset.buffer.splice(matchRslt.index, 1);
  163. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  164. return { "pos": pos, "remove": matchRslt.index };
  165. } else if (pos > matchRslt.index && pos <= radixPosition) {
  166. maskset.buffer.splice(matchRslt.index, 1);
  167. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  168. return { "pos": pos, "remove": matchRslt.index };
  169. }
  170. } else if (chrs == "0" && pos <= matchRslt.index) {
  171. return false;
  172. }
  173. }
  174. return true;
  175. },
  176. definitions: {
  177. '~': {
  178. validator: function (chrs, maskset, pos, strict, opts) {
  179. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  180. if (!isValid) {
  181. isValid = opts.radixhandler(chrs, maskset, pos, strict, opts);
  182. if (!isValid) {
  183. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  184. if (isValid === true) {
  185. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  186. if (isValid === true) {
  187. //handle overwrite when fixed precision
  188. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  189. if (opts.digitsOptional === false && pos > radixPosition && !strict) {
  190. return { "pos": pos, "remove": pos };
  191. } else return { pos: pos };
  192. }
  193. }
  194. }
  195. }
  196. return isValid;
  197. },
  198. cardinality: 1,
  199. prevalidator: null
  200. },
  201. '+': {
  202. validator: function (chrs, maskset, pos, strict, opts) {
  203. var signed = "[";
  204. if (opts.allowMinus === true) signed += "-";
  205. if (opts.allowPlus === true) signed += "\+";
  206. signed += "]";
  207. return new RegExp(signed).test(chrs);
  208. },
  209. cardinality: 1,
  210. prevalidator: null,
  211. placeholder: ''
  212. },
  213. ':': {
  214. validator: function (chrs, maskset, pos, strict, opts) {
  215. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  216. if (!isValid) {
  217. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  218. isValid = new RegExp(radix).test(chrs);
  219. if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
  220. isValid = { "pos": pos, "remove": pos };
  221. }
  222. }
  223. return isValid;
  224. },
  225. cardinality: 1,
  226. prevalidator: null,
  227. placeholder: function (opts) { return opts.radixPoint; }
  228. }
  229. },
  230. insertMode: true,
  231. autoUnmask: false,
  232. onUnMask: function (maskedValue, unmaskedValue, opts) {
  233. var processValue = maskedValue.replace(opts.prefix, "");
  234. processValue = processValue.replace(opts.suffix, "");
  235. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  236. //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  237. return processValue;
  238. },
  239. isComplete: function (buffer, opts) {
  240. var maskedValue = buffer.join(''), bufClone = buffer.slice();
  241. //verify separator positions
  242. opts.postFormat(bufClone, 0, true, opts);
  243. if (bufClone.join('') != maskedValue) return false;
  244. var processValue = maskedValue.replace(opts.prefix, "");
  245. processValue = processValue.replace(opts.suffix, "");
  246. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  247. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  248. return isFinite(processValue);
  249. },
  250. onBeforeMask: function (initialValue, opts) {
  251. if (isFinite(initialValue)) {
  252. return initialValue.toString().replace(".", opts.radixPoint);
  253. } else {
  254. var kommaMatches = initialValue.match(/,/g);
  255. var dotMatches = initialValue.match(/\./g);
  256. if (dotMatches && kommaMatches) {
  257. if (dotMatches.length > kommaMatches.length) {
  258. initialValue = initialValue.replace(/\./g, "");
  259. initialValue = initialValue.replace(",", opts.radixPoint);
  260. } else if (kommaMatches.length > dotMatches.length) {
  261. initialValue = initialValue.replace(/,/g, "");
  262. initialValue = initialValue.replace(".", opts.radixPoint);
  263. }
  264. } else {
  265. initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  266. }
  267. return initialValue;
  268. }
  269. }
  270. },
  271. 'currency': {
  272. prefix: "$ ",
  273. groupSeparator: ",",
  274. radixPoint: ".",
  275. alias: "numeric",
  276. placeholder: "0",
  277. autoGroup: true,
  278. digits: 2,
  279. digitsOptional: false,
  280. clearMaskOnLostFocus: false,
  281. decimalProtect: true,
  282. },
  283. 'decimal': {
  284. alias: "numeric"
  285. },
  286. 'integer': {
  287. alias: "numeric",
  288. digits: "0"
  289. }
  290. });
  291. return $.fn.inputmask;
  292. }));