jquery.inputmask.numeric.extensions.js 8.4 KB

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