jquery.inputmask.numeric.extensions.js 9.5 KB

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