jquery.inputmask.numeric.extensions.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 (factory) {
  10. if (typeof define === 'function' && define.amd) {
  11. define("jquery.inputmask.numeric.extensions", ['jquery', 'jquery.inputmask'], factory);
  12. } else {
  13. factory(jQuery);
  14. }
  15. }(function ($) {
  16. //number aliases
  17. $.extend($.inputmask.defaults.aliases, {
  18. 'numeric': {
  19. mask: function (opts) {
  20. if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
  21. opts.integerDigits = opts.repeat;
  22. }
  23. opts.repeat = 0;
  24. opts.autoGroup = opts.autoGroup && opts.groupSeparator != "";
  25. if (opts.autoGroup && isFinite(opts.integerDigits)) {
  26. var seps = Math.floor(opts.integerDigits / opts.groupSize);
  27. var mod = opts.integerDigits % opts.groupSize;
  28. opts.integerDigits += mod == 0 ? seps - 1 : seps;
  29. }
  30. opts.definitions[":"].placeholder = opts.radixPoint;
  31. var mask = opts.prefix;
  32. mask += "[+]";
  33. mask += "~{1," + opts.integerDigits + "}";
  34. if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  35. if (opts.digitsOptional)
  36. mask += "[" + ":" + "~{" + opts.digits + "}]";
  37. else mask += ":" + "~{" + opts.digits + "}";
  38. }
  39. mask += opts.suffix;
  40. return mask;
  41. },
  42. placeholder: "",
  43. greedy: false,
  44. digits: "*", //number of fractionalDigits
  45. digitsOptional: true,
  46. groupSeparator: "",//",", // | "."
  47. radixPoint: ".",
  48. groupSize: 3,
  49. autoGroup: false,
  50. allowPlus: true,
  51. allowMinus: true,
  52. integerDigits: "+", //number of integerDigits
  53. prefix: "",
  54. suffix: "",
  55. rightAlign: true,
  56. postFormat: function (buffer, pos, reformatOnly, opts) { //this needs to be removed // this is crap
  57. var needsRefresh = false, charAtPos = buffer[pos];
  58. if (opts.groupSeparator == "" ||
  59. ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer)) ||
  60. new RegExp('[-\+]').test(charAtPos)
  61. ) return { pos: pos };
  62. var cbuf = buffer.slice();
  63. if (charAtPos == opts.groupSeparator) {
  64. cbuf.splice(pos--, 1);
  65. charAtPos = cbuf[pos];
  66. }
  67. if (reformatOnly) cbuf[pos] = "?"; else cbuf.splice(pos, 0, "?"); //set position indicator
  68. var bufVal = cbuf.join('');
  69. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  70. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  71. needsRefresh = bufVal.indexOf(opts.groupSeparator) == 0;
  72. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  73. var radixSplit = bufVal.split(opts.radixPoint);
  74. bufVal = radixSplit[0];
  75. if (bufVal != (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
  76. needsRefresh = true;
  77. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  78. while (reg.test(bufVal)) {
  79. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  80. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  81. }
  82. }
  83. if (radixSplit.length > 1)
  84. bufVal += opts.radixPoint + radixSplit[1];
  85. }
  86. buffer.length = bufVal.length; //align the length
  87. for (var i = 0, l = bufVal.length; i < l; i++) {
  88. buffer[i] = bufVal.charAt(i);
  89. }
  90. var newPos = $.inArray("?", buffer);
  91. if (reformatOnly) buffer[newPos] = charAtPos; else buffer.splice(newPos, 1);
  92. return { pos: newPos, "refreshFromBuffer": needsRefresh };
  93. },
  94. onKeyDown: function (e, buffer, caretPos, opts) {
  95. if (opts.autoGroup && (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE)) {
  96. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  97. rslt.caret = rslt.pos + 1;
  98. return rslt;
  99. }
  100. },
  101. onKeyPress: function (e, buffer, caretPos, opts) {
  102. if (opts.autoGroup /*&& String.fromCharCode(k) == opts.radixPoint*/) {
  103. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  104. rslt.caret = rslt.pos + 1;
  105. return rslt;
  106. }
  107. },
  108. regex: {
  109. integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
  110. },
  111. negationhandler: function (chrs, buffer, pos, strict, opts) {
  112. if (!strict && opts.allowMinus && chrs === "-") {
  113. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
  114. if (matchRslt.length > 0) {
  115. if (buffer[matchRslt.index] == "+") {
  116. return { "pos": matchRslt.index, "c": "-", "remove": matchRslt.index, "caret": pos };
  117. } else if (buffer[matchRslt.index] == "-") {
  118. return { "remove": matchRslt.index, "caret": pos - 1 };
  119. } else {
  120. return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
  121. }
  122. }
  123. }
  124. return false;
  125. },
  126. definitions: {
  127. '~': {
  128. validator: function (chrs, maskset, pos, strict, opts) {
  129. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  130. if (!isValid) {
  131. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  132. if (isValid === true) isValid = { pos: pos };
  133. if (isValid != false && !strict) {
  134. //handle 0 for integerpart
  135. var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  136. if (matchRslt) {
  137. if (matchRslt["0"].indexOf("0") == 0 && pos >= opts.prefix.length) {
  138. if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
  139. maskset.buffer.splice(matchRslt.index, 1);
  140. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  141. $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
  142. } else if (pos > matchRslt.index && pos <= radixPosition) {
  143. maskset.buffer.splice(matchRslt.index, 1);
  144. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  145. $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
  146. }
  147. } else if (chrs == "0" && pos <= matchRslt.index) {
  148. return false;
  149. }
  150. }
  151. //handle overwrite when fixed precision
  152. if (opts.digitsOptional === false && pos > radixPosition) {
  153. return { "pos": pos, "remove": pos };
  154. }
  155. }
  156. }
  157. return isValid;
  158. },
  159. cardinality: 1,
  160. prevalidator: null
  161. },
  162. '+': {
  163. validator: function (chrs, maskset, pos, strict, opts) {
  164. var signed = "[";
  165. if (opts.allowMinus === true) signed += "-";
  166. if (opts.allowPlus === true) signed += "\+";
  167. signed += "]";
  168. var isValid = new RegExp(signed).test(chrs);
  169. return isValid;
  170. },
  171. cardinality: 1,
  172. prevalidator: null
  173. },
  174. ':': {
  175. validator: function (chrs, maskset, pos, strict, opts) {
  176. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  177. if (!isValid) {
  178. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  179. isValid = new RegExp(radix).test(chrs);
  180. if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
  181. isValid = { "pos": pos, "remove": pos };
  182. }
  183. }
  184. return isValid;
  185. },
  186. cardinality: 1,
  187. prevalidator: null,
  188. placeholder: "" //radixpoint will be set in the mask function
  189. }
  190. },
  191. insertMode: true,
  192. autoUnmask: false,
  193. onUnMask: function (maskedValue, unmaskedValue, opts) {
  194. var processValue = maskedValue.replace(opts.prefix, "");
  195. processValue = processValue.replace(opts.suffix, "");
  196. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  197. //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  198. return processValue;
  199. },
  200. isComplete: function (buffer, opts) {
  201. var maskedValue = buffer.join(''), bufClone = buffer.slice();
  202. //verify separator positions
  203. opts.postFormat(bufClone, 0, true, opts);
  204. if (bufClone.join('') != maskedValue) return false;
  205. var processValue = maskedValue.replace(opts.prefix, "");
  206. processValue = processValue.replace(opts.suffix, "");
  207. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  208. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  209. return isFinite(processValue);
  210. },
  211. onBeforeMask: function (initialValue, opts) {
  212. if (isFinite(initialValue)) {
  213. return initialValue.toString().replace(".", opts.radixPoint);
  214. } else {
  215. var kommaMatches = initialValue.match(/,/g);
  216. var dotMatches = initialValue.match(/\./g);
  217. if (dotMatches && kommaMatches) {
  218. if (dotMatches.length > kommaMatches.length) {
  219. initialValue = initialValue.replace(/\./g, "");
  220. initialValue = initialValue.replace(",", opts.radixPoint);
  221. } else if (kommaMatches.length > dotMatches.length) {
  222. initialValue = initialValue.replace(/,/g, "");
  223. initialValue = initialValue.replace(".", opts.radixPoint);
  224. }
  225. } else {
  226. initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  227. }
  228. return initialValue;
  229. }
  230. }
  231. },
  232. 'decimal': {
  233. alias: "numeric"
  234. },
  235. 'integer': {
  236. alias: "numeric",
  237. digits: "0"
  238. }
  239. });
  240. }));