jquery.inputmask.numeric.extensions.js 8.7 KB

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