jquery.inputmask.numeric.extensions.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. 'numeric': {
  13. mask: function (opts) {
  14. var mask = opts.prefix + "~{1," + opts.integerDigits + "}";
  15. mask += "[" + opts.radixPoint + "~{" + opts.digits + "}]";
  16. mask += opts.suffix;
  17. return mask;
  18. },
  19. placeholder: "",
  20. greedy: false,
  21. numericInput: false,
  22. isNumeric: false,
  23. digits: "2", //number of fractionalDigits
  24. groupSeparator: "",//",", // | "."
  25. radixPoint: ".",
  26. groupSize: 3,
  27. autoGroup: false,
  28. allowPlus: true,
  29. allowMinus: true,
  30. integerDigits: "20", //number of integerDigits
  31. defaultValue: "",
  32. prefix: "",
  33. suffix: "",
  34. postFormat: function (buffer, pos, reformatOnly, opts) {
  35. if (opts.groupSeparator == "") return pos;
  36. var cbuf = buffer.slice(),
  37. radixPos = $.inArray(opts.radixPoint, buffer);
  38. if (!reformatOnly) {
  39. cbuf.splice(pos, 0, "?"); //set position indicator
  40. }
  41. var bufVal = cbuf.join('');
  42. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  43. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  44. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  45. var radixSplit = bufVal.split(opts.radixPoint);
  46. bufVal = radixSplit[0];
  47. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  48. while (reg.test(bufVal)) {
  49. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  50. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  51. }
  52. if (radixSplit.length > 1)
  53. bufVal += opts.radixPoint + radixSplit[1];
  54. }
  55. buffer.length = bufVal.length; //align the length
  56. for (var i = 0, l = bufVal.length; i < l; i++) {
  57. buffer[i] = bufVal.charAt(i);
  58. }
  59. var newPos = $.inArray("?", buffer);
  60. if (!reformatOnly) buffer.splice(newPos, 1);
  61. return reformatOnly ? pos : newPos;
  62. },
  63. definitions: {
  64. '~': {
  65. validator: function (chrs, buffer, pos, strict, opts) {
  66. var isValid = new RegExp("[0-9]").test(chrs);
  67. return isValid;
  68. },
  69. cardinality: 1,
  70. prevalidator: null
  71. }
  72. },
  73. insertMode: true,
  74. autoUnmask: false
  75. },
  76. 'decimal': {
  77. mask: "~",
  78. placeholder: "",
  79. repeat: "*",
  80. greedy: false,
  81. numericInput: false,
  82. isNumeric: true,
  83. digits: "*", //number of fractionalDigits
  84. groupSeparator: "",//",", // | "."
  85. radixPoint: ".",
  86. groupSize: 3,
  87. autoGroup: false,
  88. allowPlus: true,
  89. allowMinus: true,
  90. //todo
  91. integerDigits: "*", //number of integerDigits
  92. defaultValue: "",
  93. prefix: "",
  94. suffix: "",
  95. postFormat: function (buffer, pos, reformatOnly, opts) {
  96. if (opts.groupSeparator == "") return pos;
  97. var cbuf = buffer.slice(),
  98. radixPos = $.inArray(opts.radixPoint, buffer);
  99. if (!reformatOnly) {
  100. cbuf.splice(pos, 0, "?"); //set position indicator
  101. }
  102. var bufVal = cbuf.join('');
  103. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  104. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  105. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  106. var radixSplit = bufVal.split(opts.radixPoint);
  107. bufVal = radixSplit[0];
  108. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  109. while (reg.test(bufVal)) {
  110. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  111. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  112. }
  113. if (radixSplit.length > 1)
  114. bufVal += opts.radixPoint + radixSplit[1];
  115. }
  116. buffer.length = bufVal.length; //align the length
  117. for (var i = 0, l = bufVal.length; i < l; i++) {
  118. buffer[i] = bufVal.charAt(i);
  119. }
  120. var newPos = $.inArray("?", buffer);
  121. if (!reformatOnly) buffer.splice(newPos, 1);
  122. return reformatOnly ? pos : newPos;
  123. },
  124. regex: {
  125. number: function (opts) {
  126. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  127. var digitExpression = isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}';
  128. var integerExpression = isNaN(opts.integerDigits) ? opts.integerDigits : '{1,' + opts.integerDigits + '}';
  129. var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
  130. var currentRegExp = "^" + signedExpression + "\\d" + integerExpression + "(" + escapedRadixPoint + "\\d" + digitExpression + ")?$";
  131. return new RegExp(currentRegExp);
  132. }
  133. },
  134. onKeyDown: function (e, buffer, opts) {
  135. var $input = $(this), input = this;
  136. if (e.keyCode == opts.keyCode.TAB) {
  137. var radixPosition = $.inArray(opts.radixPoint, buffer);
  138. if (radixPosition != -1) {
  139. var masksets = $input.data('_inputmask')['masksets'];
  140. var activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex'];
  141. for (var i = 1; i <= opts.digits && i < opts.getMaskLength(masksets[activeMasksetIndex]["_buffer"], opts.greedy, opts.repeat, buffer, opts) ; i++) {
  142. if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == "") buffer[radixPosition + i] = "0";
  143. }
  144. return { "refreshFromBuffer": true };
  145. }
  146. } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
  147. opts.postFormat(buffer, 0, true, opts);
  148. input._valueSet(buffer.join(''));
  149. return { "refreshFromBuffer": true };
  150. }
  151. },
  152. definitions: {
  153. '~': { //real number
  154. validator: function (chrs, buffer, pos, strict, opts) {
  155. var iopts = $.extend({}, opts, { digits: strict ? "*" : opts.digits });
  156. if (chrs == "") return false;
  157. if (!strict && pos <= 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs) && buffer.join('').length == 1) { //handle first char
  158. buffer[0] = "";
  159. return { "pos": 0 };
  160. }
  161. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  162. cbuf.splice(pos, 0, chrs);
  163. var bufferStr = cbuf.join('');
  164. //strip groupseparator
  165. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  166. bufferStr = bufferStr.replace(new RegExp(escapedGroupSeparator, "g"), '');
  167. if (strict && bufferStr.lastIndexOf(opts.radixPoint) == bufferStr.length - 1) {
  168. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  169. bufferStr = bufferStr.replace(new RegExp(escapedRadixPoint, "g"), '');
  170. }
  171. if (!strict && bufferStr == "") return false;
  172. var isValid = opts.regex.number(iopts).test(bufferStr);
  173. if (!isValid) {
  174. //let's help the regex a bit
  175. bufferStr += "0";
  176. isValid = opts.regex.number(iopts).test(bufferStr);
  177. if (!isValid) {
  178. //make a valid group
  179. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  180. for (var i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  181. bufferStr += "0";
  182. }
  183. isValid = opts.regex.number(iopts).test(bufferStr);
  184. if (!isValid && !strict) {
  185. if (chrs == opts.radixPoint) {
  186. isValid = opts.regex.number(iopts).test("0" + bufferStr + "0");
  187. if (isValid) {
  188. buffer[pos] = "0";
  189. pos++;
  190. return { "pos": pos };
  191. }
  192. }
  193. }
  194. }
  195. }
  196. if (isValid != false && !strict && chrs != opts.radixPoint) {
  197. var newPos = opts.postFormat(buffer, pos, (chrs == "-" || chrs == "+") ? true : false, opts);
  198. return { "pos": newPos, "refreshFromBuffer": true };
  199. }
  200. return isValid;
  201. },
  202. cardinality: 1,
  203. prevalidator: null
  204. }
  205. },
  206. insertMode: true,
  207. autoUnmask: false
  208. },
  209. 'integer': {
  210. regex: {
  211. number: function (opts) {
  212. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  213. var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
  214. return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)$");
  215. }
  216. },
  217. alias: "decimal"
  218. }
  219. });
  220. })(jQuery);