jquery.inputmask.numeric.extensions.js 8.8 KB

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