jquery.inputmask.numeric.extensions.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 (e.keyCode == opts.keyCode.TAB && opts.placeholder.charAt(0) != "0") {
  90. var radixPosition = $.inArray(opts.radixPoint, buffer);
  91. if (radixPosition != -1 && isFinite(opts.digits)) {
  92. for (var i = 1; i <= opts.digits; i++) {
  93. if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == opts.placeholder.charAt(0)) buffer[radixPosition + i] = "0";
  94. }
  95. return { "refreshFromBuffer": { start: ++radixPosition, end: radixPosition + opts.digits } };
  96. }
  97. } else if (opts.autoGroup && (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE)) {
  98. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  99. rslt.caret = rslt.pos + 1;
  100. return rslt;
  101. }
  102. },
  103. onKeyPress: function (e, buffer, caretPos, opts) {
  104. if (opts.autoGroup /*&& String.fromCharCode(k) == opts.radixPoint*/) {
  105. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  106. rslt.caret = rslt.pos + 1;
  107. return rslt;
  108. }
  109. },
  110. regex: {
  111. integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
  112. },
  113. negationhandler: function (chrs, buffer, pos, strict, opts) {
  114. if (!strict && opts.allowMinus && chrs === "-") {
  115. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
  116. if (matchRslt.length > 0) {
  117. if (buffer[matchRslt.index] == "+") {
  118. return { "pos": matchRslt.index, "c": "-", "remove": matchRslt.index, "caret": pos };
  119. } else if (buffer[matchRslt.index] == "-") {
  120. return { "remove": matchRslt.index, "caret": pos - 1 };
  121. } else {
  122. return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
  123. }
  124. }
  125. }
  126. return false;
  127. },
  128. definitions: {
  129. '~': {
  130. validator: function (chrs, maskset, pos, strict, opts) {
  131. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  132. if (!isValid) {
  133. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  134. if (isValid === true) isValid = { pos: pos };
  135. if (isValid != false && !strict) {
  136. //handle 0 for integerpart
  137. var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  138. if (matchRslt) {
  139. if (matchRslt["0"].indexOf("0") == 0 && pos >= opts.prefix.length) {
  140. if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
  141. maskset.buffer.splice(matchRslt.index, 1);
  142. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  143. $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
  144. } else if (pos > matchRslt.index && pos <= radixPosition) {
  145. maskset.buffer.splice(matchRslt.index, 1);
  146. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  147. $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
  148. }
  149. } else if (chrs == "0" && pos <= matchRslt.index) {
  150. return false;
  151. }
  152. }
  153. //handle overwrite when fixed precision
  154. if (opts.digitsOptional === false && pos > radixPosition) {
  155. return { "pos": pos, "remove": pos };
  156. }
  157. }
  158. }
  159. return isValid;
  160. },
  161. cardinality: 1,
  162. prevalidator: null
  163. },
  164. '+': {
  165. validator: function (chrs, maskset, pos, strict, opts) {
  166. var signed = "[";
  167. if (opts.allowMinus === true) signed += "-";
  168. if (opts.allowPlus === true) signed += "\+";
  169. signed += "]";
  170. var isValid = new RegExp(signed).test(chrs);
  171. return isValid;
  172. },
  173. cardinality: 1,
  174. prevalidator: null,
  175. placeholder: ""
  176. },
  177. ':': {
  178. validator: function (chrs, maskset, pos, strict, opts) {
  179. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  180. if (!isValid) {
  181. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  182. isValid = new RegExp(radix).test(chrs);
  183. if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
  184. isValid = { "pos": pos, "remove": pos };
  185. }
  186. }
  187. return isValid;
  188. },
  189. cardinality: 1,
  190. prevalidator: null,
  191. placeholder: "" //radixpoint will be set in the mask function
  192. }
  193. },
  194. insertMode: true,
  195. autoUnmask: false,
  196. onUnMask: function (maskedValue, unmaskedValue, opts) {
  197. var processValue = maskedValue.replace(opts.prefix, "");
  198. processValue = processValue.replace(opts.suffix, "");
  199. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  200. //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  201. return processValue;
  202. },
  203. isComplete: function (buffer, opts) {
  204. var maskedValue = buffer.join(''), bufClone = buffer.slice();
  205. //verify separator positions
  206. opts.postFormat(bufClone, 0, true, opts);
  207. if (bufClone.join('') != maskedValue) return false;
  208. var processValue = maskedValue.replace(opts.prefix, "");
  209. processValue = processValue.replace(opts.suffix, "");
  210. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  211. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  212. return isFinite(processValue);
  213. },
  214. onBeforeMask: function (initialValue, opts) {
  215. if (isFinite(initialValue)) {
  216. return initialValue.toString().replace(".", opts.radixPoint);
  217. } else {
  218. var kommaMatches = initialValue.match(/,/g);
  219. var dotMatches = initialValue.match(/\./g);
  220. if (dotMatches && kommaMatches) {
  221. if (dotMatches.length > kommaMatches.length) {
  222. initialValue = initialValue.replace(/\./g, "");
  223. initialValue = initialValue.replace(",", opts.radixPoint);
  224. } else if (kommaMatches.length > dotMatches.length) {
  225. initialValue = initialValue.replace(/,/g, "");
  226. initialValue = initialValue.replace(".", opts.radixPoint);
  227. }
  228. } else {
  229. initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  230. }
  231. return initialValue;
  232. }
  233. }
  234. },
  235. 'decimal': {
  236. alias: "numeric"
  237. },
  238. 'integer': {
  239. alias: "numeric",
  240. digits: "0"
  241. }
  242. });
  243. return $.fn.inputmask;
  244. })(jQuery);