jquery.inputmask.numeric.extensions.js 13 KB

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