jquery.inputmask.numeric.extensions.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. 'decimal': {
  13. mask: "~",
  14. placeholder: "",
  15. repeat: "*",
  16. greedy: false,
  17. numericInput: false,
  18. isNumeric: true,
  19. digits: "*", //number of fractionalDigits
  20. groupSeparator: "",//",", // | "."
  21. radixPoint: ".",
  22. groupSize: 3,
  23. autoGroup: false,
  24. allowPlus: true,
  25. allowMinus: true,
  26. //todo
  27. integerDigits: "*", //number of integerDigits
  28. defaultValue: "",
  29. prefix: "",
  30. suffix: "",
  31. //todo
  32. getMaskLength: function (buffer, greedy, repeat, currentBuffer, opts) { //custom getMaskLength to take the groupSeparator into account
  33. var calculatedLength = buffer.length;
  34. if (!greedy) {
  35. if (repeat == "*") {
  36. calculatedLength = currentBuffer.length + 1;
  37. } else if (repeat > 1) {
  38. calculatedLength += (buffer.length * (repeat - 1));
  39. }
  40. }
  41. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  42. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  43. var currentBufferStr = currentBuffer.join(''), strippedBufferStr = currentBufferStr.replace(new RegExp(escapedGroupSeparator, "g"), "").replace(new RegExp(escapedRadixPoint), ""),
  44. groupOffset = currentBufferStr.length - strippedBufferStr.length;
  45. return calculatedLength + groupOffset;
  46. },
  47. postFormat: function (buffer, pos, reformatOnly, opts) {
  48. if (opts.groupSeparator == "") return pos;
  49. var cbuf = buffer.slice(),
  50. radixPos = $.inArray(opts.radixPoint, buffer);
  51. if (!reformatOnly) {
  52. cbuf.splice(pos, 0, "?"); //set position indicator
  53. }
  54. var bufVal = cbuf.join('');
  55. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  56. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  57. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  58. var radixSplit = bufVal.split(opts.radixPoint);
  59. bufVal = radixSplit[0];
  60. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  61. while (reg.test(bufVal)) {
  62. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  63. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  64. }
  65. if (radixSplit.length > 1)
  66. bufVal += opts.radixPoint + radixSplit[1];
  67. }
  68. buffer.length = bufVal.length; //align the length
  69. for (var i = 0, l = bufVal.length; i < l; i++) {
  70. buffer[i] = bufVal.charAt(i);
  71. }
  72. var newPos = $.inArray("?", buffer);
  73. if (!reformatOnly) buffer.splice(newPos, 1);
  74. return reformatOnly ? pos : newPos;
  75. },
  76. regex: {
  77. number: function (opts) {
  78. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  79. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  80. var digitExpression = isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}';
  81. var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
  82. return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
  83. }
  84. },
  85. onKeyDown: function (e, buffer, opts) {
  86. var $input = $(this), input = this;
  87. if (e.keyCode == opts.keyCode.TAB) {
  88. var radixPosition = $.inArray(opts.radixPoint, buffer);
  89. if (radixPosition != -1) {
  90. var masksets = $input.data('_inputmask')['masksets'];
  91. var activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex'];
  92. for (var i = 1; i <= opts.digits && i < opts.getMaskLength(masksets[activeMasksetIndex]["_buffer"], masksets[activeMasksetIndex]["greedy"], masksets[activeMasksetIndex]["repeat"], buffer, opts) ; i++) {
  93. if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == "") buffer[radixPosition + i] = "0";
  94. }
  95. input._valueSet(buffer.join(''));
  96. }
  97. } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
  98. opts.postFormat(buffer, 0, true, opts);
  99. input._valueSet(buffer.join(''));
  100. return true;
  101. }
  102. },
  103. definitions: {
  104. '~': { //real number
  105. validator: function (chrs, buffer, pos, strict, opts) {
  106. if (chrs == "") return false;
  107. if (!strict && pos <= 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs) && buffer.join('').length == 1) { //handle first char
  108. buffer[0] = "";
  109. return { "pos": 0 };
  110. }
  111. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  112. cbuf.splice(pos, 0, chrs);
  113. var bufferStr = cbuf.join('');
  114. //strip groupseparator
  115. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  116. bufferStr = bufferStr.replace(new RegExp(escapedGroupSeparator, "g"), '');
  117. var isValid = opts.regex.number(opts).test(bufferStr);
  118. if (!isValid) {
  119. //let's help the regex a bit
  120. bufferStr += "0";
  121. isValid = opts.regex.number(opts).test(bufferStr);
  122. if (!isValid) {
  123. //make a valid group
  124. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  125. for (var i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  126. bufferStr += "0";
  127. }
  128. isValid = opts.regex.number(opts).test(bufferStr);
  129. if (!isValid && !strict) {
  130. if (chrs == opts.radixPoint) {
  131. isValid = opts.regex.number(opts).test("0" + bufferStr + "0");
  132. if (isValid) {
  133. buffer[pos] = "0";
  134. pos++;
  135. return { "pos": pos };
  136. }
  137. }
  138. }
  139. }
  140. }
  141. if (isValid != false && !strict && chrs != opts.radixPoint) {
  142. var newPos = opts.postFormat(buffer, pos, false, opts);
  143. return { "pos": newPos };
  144. }
  145. return isValid;
  146. },
  147. cardinality: 1,
  148. prevalidator: null
  149. }
  150. },
  151. insertMode: true,
  152. autoUnmask: false
  153. },
  154. 'integer': {
  155. regex: {
  156. number: function (opts) {
  157. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  158. var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
  159. return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)$");
  160. }
  161. },
  162. alias: "decimal"
  163. }
  164. });
  165. })(jQuery);