jquery.inputmask.numeric.extensions.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. var mask = opts.prefix;
  25. mask += "[+]";
  26. mask += "~{1," + opts.integerDigits + "}";
  27. if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  28. if (opts.digitsOptional)
  29. mask += "[" + opts.radixPoint + "~{" + opts.digits + "}]";
  30. else mask += opts.radixPoint + "~{" + opts.digits + "}";
  31. }
  32. mask += opts.suffix;
  33. return mask;
  34. },
  35. placeholder: "",
  36. greedy: false,
  37. digits: "*", //number of fractionalDigits
  38. digitsOptional: true,
  39. groupSeparator: "",//",", // | "."
  40. radixPoint: ".",
  41. groupSize: 3,
  42. autoGroup: false,
  43. allowPlus: true,
  44. allowMinus: true,
  45. integerDigits: "+", //number of integerDigits
  46. prefix: "",
  47. suffix: "",
  48. skipRadixDance: false, //disable radixpoint caret positioning
  49. getLastValidPosition: function (maskset, closestTo, opts) {
  50. var lastValidPosition = -1, valids = maskset["validPositions"];
  51. for (var posNdx in valids) {
  52. var psNdx = parseInt(posNdx);
  53. if (psNdx > lastValidPosition) lastValidPosition = psNdx;
  54. }
  55. if (closestTo != undefined) {
  56. var buffer = maskset["buffer"];
  57. if (opts.skipRadixDance === false && opts.radixPoint != "" && $.inArray(opts.radixPoint, buffer) != -1)
  58. lastValidPosition = $.inArray(opts.radixPoint, buffer);
  59. }
  60. return lastValidPosition;
  61. },
  62. rightAlign: true,
  63. postFormat: function (buffer, pos, reformatOnly, opts) {
  64. var needsRefresh = false;
  65. if (opts.groupSeparator == "" || ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer))) return { pos: pos };
  66. var cbuf = buffer.slice();
  67. if (!reformatOnly) {
  68. cbuf.splice(pos, 0, "?"); //set position indicator
  69. }
  70. var bufVal = cbuf.join('');
  71. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  72. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  73. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  74. var radixSplit = bufVal.split(opts.radixPoint);
  75. bufVal = radixSplit[0];
  76. if (bufVal != (opts.prefix + "?0")) {
  77. needsRefresh = true;
  78. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  79. while (reg.test(bufVal)) {
  80. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  81. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  82. }
  83. }
  84. if (radixSplit.length > 1)
  85. bufVal += opts.radixPoint + radixSplit[1];
  86. }
  87. buffer.length = bufVal.length; //align the length
  88. for (var i = 0, l = bufVal.length; i < l; i++) {
  89. buffer[i] = bufVal.charAt(i);
  90. }
  91. var newPos = $.inArray("?", buffer);
  92. if (!reformatOnly) buffer.splice(newPos, 1);
  93. return { pos: reformatOnly ? pos : newPos, "refreshFromBuffer": needsRefresh };
  94. },
  95. onKeyDown: function (e, buffer, opts) {
  96. var $input = $(this), input = this;
  97. if (opts.autoGroup && e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
  98. return opts.postFormat(buffer, 0, true, opts);
  99. }
  100. },
  101. regex: {
  102. integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
  103. },
  104. negationhandler: function (chrs, buffer, pos, strict, opts) {
  105. if (!strict && opts.allowMinus && chrs === "-") {
  106. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
  107. if (matchRslt.length > 0) {
  108. if (buffer[matchRslt.index] == "+") {
  109. buffer.splice(matchRslt.index, 1);
  110. return { "pos": matchRslt.index, "c": "-", "refreshFromBuffer": true, "caret": pos };
  111. } else if (buffer[matchRslt.index] == "-") {
  112. buffer.splice(matchRslt.index, 1);
  113. return { "refreshFromBuffer": true, "caret": pos - 1 };
  114. } else {
  115. return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
  116. }
  117. }
  118. }
  119. return false;
  120. },
  121. definitions: {
  122. '~': {
  123. validator: function (chrs, buffer, pos, strict, opts) {
  124. var isValid = opts.negationhandler(chrs, buffer, pos, strict, opts);
  125. if (!isValid) {
  126. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  127. //handle 0 for integerpart
  128. if (isValid != false) {
  129. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, buffer);
  130. if (matchRslt && matchRslt["0"][0] == "0" && pos >= opts.prefix.length && (radixPosition == -1 || pos < radixPosition)) {
  131. buffer.splice(matchRslt.index, 1);
  132. } else if (chrs == "0" && matchRslt && matchRslt["0"].length > 0 && pos == opts.prefix.length) {
  133. return false;
  134. }
  135. }
  136. if (isValid != false && !strict && chrs != opts.radixPoint && opts.autoGroup === true) {
  137. isValid = opts.postFormat(buffer, pos, (chrs == "-" || chrs == "+") ? true : false, opts);
  138. }
  139. }
  140. return isValid;
  141. },
  142. cardinality: 1,
  143. prevalidator: null
  144. },
  145. '+': {
  146. validator: function (chrs, buffer, pos, strict, opts) {
  147. var signed = "[";
  148. if (opts.allowMinus === true) signed += "-";
  149. if (opts.allowPlus === true) signed += "\+";
  150. signed += "]";
  151. var isValid = new RegExp(signed).test(chrs);
  152. return isValid;
  153. },
  154. cardinality: 1,
  155. prevalidator: null
  156. }
  157. },
  158. insertMode: true,
  159. autoUnmask: false,
  160. onUnMask: function (maskedValue, unmaskedValue, opts) {
  161. var processValue = maskedValue.replace(opts.prefix, "");
  162. processValue = processValue.replace(opts.suffix, "");
  163. processValue = processValue.replace(new RegExp(opts.groupSeparator, "g"), "");
  164. processValue = processValue.replace(opts.radixPoint, ".");
  165. return Number(processValue);
  166. },
  167. isComplete: function (buffer, opts) {
  168. var maskedValue = buffer.join('');
  169. var processValue = maskedValue.replace(opts.prefix, "");
  170. processValue = processValue.replace(opts.suffix, "");
  171. processValue = processValue.replace(new RegExp(opts.groupSeparator, "g"), "");
  172. processValue = processValue.replace(opts.radixPoint, ".");
  173. return isFinite(processValue);
  174. }
  175. },
  176. 'decimal': {
  177. alias: "numeric"
  178. },
  179. 'integer': {
  180. alias: "numeric",
  181. digits: "0"
  182. }
  183. });
  184. })(jQuery);