jquery.inputmask.numeric.extensions.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. console.log(pos);
  90. if (pos <= 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs)) { //handle first char
  91. buffer[0] = "";
  92. return { "pos": 0 };
  93. }
  94. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  95. cbuf.splice(pos + 1, 0, chrs);
  96. var bufferStr = cbuf.join('');
  97. if (opts.autoGroup && !strict) { //strip groupseparator
  98. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  99. bufferStr = bufferStr.replace(new RegExp(escapedGroupSeparator, "g"), '');
  100. }
  101. var isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  102. if (!isValid) {
  103. //let's help the regex a bit
  104. bufferStr += "0";
  105. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  106. if (!isValid) {
  107. //make a valid group
  108. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  109. for (i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  110. bufferStr += "0";
  111. }
  112. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  113. if (!isValid && !strict) {
  114. if (chrs == opts.radixPoint) {
  115. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test("0" + bufferStr + "0");
  116. if (isValid) {
  117. buffer[pos] = "0";
  118. pos++;
  119. return { "pos": pos };
  120. }
  121. }
  122. }
  123. }
  124. }
  125. if (isValid != false && !strict && chrs != opts.radixPoint) {
  126. var newPos = opts.postFormat(buffer, pos + 1, false, opts);
  127. return { "pos": newPos };
  128. }
  129. return isValid;
  130. },
  131. cardinality: 1,
  132. prevalidator: null
  133. }
  134. },
  135. insertMode: true,
  136. autoUnmask: false
  137. },
  138. 'non-negative-decimal': {
  139. regex: {
  140. number: function (groupSeparator, groupSize, radixPoint, digits) {
  141. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  142. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, radixPoint);
  143. var digitExpression = isNaN(digits) ? digits : '{0,' + digits + '}'
  144. return new RegExp("^[\+]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
  145. }
  146. },
  147. alias: "decimal"
  148. },
  149. 'integer': {
  150. regex: {
  151. number: function (groupSeparator, groupSize) {
  152. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  153. return new RegExp("^[\+-]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)$");
  154. }
  155. },
  156. alias: "decimal"
  157. },
  158. 'non-negative-integer': {
  159. regex: {
  160. number: function (groupSeparator, groupSize) {
  161. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  162. return new RegExp("^[\+]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)$");
  163. }
  164. },
  165. alias: "integer"
  166. }
  167. });
  168. })(jQuery);