jquery.inputmask.numeric.extensions.js 9.3 KB

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