jquery.inputmask.numeric.extensions.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. if (opts.autoGroup && (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE)) {
  97. return opts.postFormat(buffer, 0, true, opts);
  98. }
  99. },
  100. onKeyUp: function (e, buffer, opts) {
  101. if (opts.autoGroup && (e.keyCode == 110 || e.keyCode == 188 || e.keyCode == 190)) {
  102. return opts.postFormat(buffer, 0, true, opts);
  103. }
  104. },
  105. regex: {
  106. integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
  107. },
  108. negationhandler: function (chrs, buffer, pos, strict, opts) {
  109. if (!strict && opts.allowMinus && chrs === "-") {
  110. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
  111. if (matchRslt.length > 0) {
  112. if (buffer[matchRslt.index] == "+") {
  113. buffer.splice(matchRslt.index, 1);
  114. return { "pos": matchRslt.index, "c": "-", "refreshFromBuffer": true, "caret": pos };
  115. } else if (buffer[matchRslt.index] == "-") {
  116. buffer.splice(matchRslt.index, 1);
  117. return { "refreshFromBuffer": true, "caret": pos - 1 };
  118. } else {
  119. return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
  120. }
  121. }
  122. }
  123. return false;
  124. },
  125. definitions: {
  126. '~': {
  127. validator: function (chrs, buffer, pos, strict, opts) {
  128. var isValid = opts.negationhandler(chrs, buffer, pos, strict, opts);
  129. if (!isValid) {
  130. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  131. //handle 0 for integerpart
  132. if (isValid != false) {
  133. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, buffer);
  134. if (matchRslt && matchRslt["0"][0] == "0" && pos >= opts.prefix.length && (radixPosition == -1 || pos < radixPosition)) {
  135. buffer.splice(matchRslt.index, 1);
  136. } else if (chrs == "0" && matchRslt && matchRslt["0"].length > 0 && pos == opts.prefix.length) {
  137. return false;
  138. }
  139. }
  140. if (isValid != false && !strict && chrs != opts.radixPoint && opts.autoGroup === true) {
  141. isValid = opts.postFormat(buffer, pos, (chrs == "-" || chrs == "+") ? true : false, opts);
  142. }
  143. }
  144. return isValid;
  145. },
  146. cardinality: 1,
  147. prevalidator: null
  148. },
  149. '+': {
  150. validator: function (chrs, buffer, pos, strict, opts) {
  151. var signed = "[";
  152. if (opts.allowMinus === true) signed += "-";
  153. if (opts.allowPlus === true) signed += "\+";
  154. signed += "]";
  155. var isValid = new RegExp(signed).test(chrs);
  156. return isValid;
  157. },
  158. cardinality: 1,
  159. prevalidator: null
  160. }
  161. },
  162. insertMode: true,
  163. autoUnmask: false,
  164. onUnMask: function (maskedValue, unmaskedValue, opts) {
  165. var processValue = maskedValue.replace(opts.prefix, "");
  166. processValue = processValue.replace(opts.suffix, "");
  167. processValue = processValue.replace(new RegExp(opts.groupSeparator, "g"), "");
  168. processValue = processValue.replace(opts.radixPoint, ".");
  169. return Number(processValue);
  170. },
  171. isComplete: function (buffer, opts) {
  172. var maskedValue = buffer.join('');
  173. var processValue = maskedValue.replace(opts.prefix, "");
  174. processValue = processValue.replace(opts.suffix, "");
  175. processValue = processValue.replace(new RegExp(opts.groupSeparator, "g"), "");
  176. processValue = processValue.replace(opts.radixPoint, ".");
  177. return isFinite(processValue);
  178. },
  179. onBeforeMask: function (initialValue, opts) {
  180. return isFinite(initialValue) ? initialValue.toString().replace(".", opts.radixPoint) : initialValue;
  181. }
  182. },
  183. 'decimal': {
  184. alias: "numeric"
  185. },
  186. 'integer': {
  187. alias: "numeric",
  188. digits: "0"
  189. }
  190. });
  191. })(jQuery);