jquery.inputmask.numeric.extensions.js 8.2 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 nptStr = input._valueGet();
  70. var radixPosition = nptStr.indexOf(opts.radixPoint);
  71. if (radixPosition != -1) {
  72. for (var i = 1; i < opts.digits; i++) {
  73. if (nptStr[radixPosition + i]) nptStr = nptStr + "0";
  74. }
  75. if (nptStr !== $input.val()) {
  76. $input.val(nptStr);
  77. }
  78. }
  79. } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
  80. opts.postFormat(buffer, 0, true, opts);
  81. input._valueSet(buffer.join(''));
  82. }
  83. },
  84. definitions: {
  85. '~': { //real number
  86. validator: function (chrs, buffer, pos, strict, opts) {
  87. if (chrs == "") return false;
  88. if (pos == 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs)) { //handle first char
  89. buffer[0] = "";
  90. return { "pos": 0 };
  91. }
  92. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  93. cbuf.splice(pos + 1, 0, chrs);
  94. var bufferStr = cbuf.join('');
  95. if (opts.autoGroup && !strict) //strip groupseparator
  96. bufferStr = bufferStr.replace(new RegExp("\\" + opts.groupSeparator, "g"), '');
  97. var isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  98. if (!isValid) {
  99. //let's help the regex a bit
  100. bufferStr += "0";
  101. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  102. if (!isValid) {
  103. //make a valid group
  104. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  105. for (i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  106. bufferStr += "0";
  107. }
  108. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  109. if (!isValid && !strict) {
  110. if (chrs == opts.radixPoint) {
  111. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test("0" + bufferStr + "0");
  112. if (isValid) {
  113. buffer[pos] = "0";
  114. pos++;
  115. return { "pos": pos };
  116. }
  117. }
  118. }
  119. }
  120. }
  121. if (isValid != false && !strict && chrs != opts.radixPoint) {
  122. var newPos = opts.postFormat(buffer, pos + 1, false, opts);
  123. return { "pos": newPos };
  124. }
  125. return isValid;
  126. },
  127. cardinality: 1,
  128. prevalidator: null
  129. }
  130. },
  131. insertMode: true,
  132. autoUnmask: false
  133. },
  134. 'non-negative-decimal': {
  135. regex: {
  136. number: function (groupSeparator, groupSize, radixPoint, digits) {
  137. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  138. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, radixPoint);
  139. var digitExpression = isNaN(digits) ? digits : '{0,' + digits + '}'
  140. return new RegExp("^[\+]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
  141. }
  142. },
  143. alias: "decimal"
  144. },
  145. 'integer': {
  146. regex: {
  147. number: function (groupSeparator, groupSize) { return new RegExp("^([\+\-]?\\d*)$"); }
  148. },
  149. alias: "decimal"
  150. }
  151. });
  152. })(jQuery);