jquery.inputmask.numeric.extensions.js 9.0 KB

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