jquery.inputmask.numeric.extensions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
  15. opts.integerDigits = opts.repeat;
  16. }
  17. opts.repeat = 0;
  18. opts.autoGroup = opts.autoGroup && opts.groupSeparator != "";
  19. if (opts.autoGroup && isFinite(opts.integerDigits)) {
  20. var seps = Math.floor(opts.integerDigits / opts.groupSize);
  21. var mod = opts.integerDigits % opts.groupSize;
  22. opts.integerDigits += mod == 0 ? seps - 1 : seps;
  23. }
  24. opts.definitions[":"].placeholder = opts.radixPoint;
  25. var mask = opts.prefix;
  26. mask += "[+]";
  27. mask += "~{1," + opts.integerDigits + "}";
  28. if (opts.digits != undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  29. if (opts.digitsOptional)
  30. mask += "[" + ":" + "~{" + opts.digits + "}]";
  31. else mask += ":" + "~{" + opts.digits + "}";
  32. }
  33. mask += opts.suffix;
  34. return mask;
  35. },
  36. placeholder: "",
  37. greedy: false,
  38. digits: "*", //number of fractionalDigits
  39. digitsOptional: true,
  40. groupSeparator: "",//",", // | "."
  41. radixPoint: ".",
  42. groupSize: 3,
  43. autoGroup: false,
  44. allowPlus: true,
  45. allowMinus: true,
  46. integerDigits: "+", //number of integerDigits
  47. prefix: "",
  48. suffix: "",
  49. skipRadixDance: false, //disable radixpoint caret positioning
  50. getLastValidPosition: function (maskset, closestTo, opts) {
  51. var lastValidPosition = -1, valids = maskset["validPositions"];
  52. for (var posNdx in valids) {
  53. var psNdx = parseInt(posNdx);
  54. if (psNdx > lastValidPosition) lastValidPosition = psNdx;
  55. }
  56. if (closestTo != undefined) {
  57. var buffer = maskset["buffer"];
  58. if (opts.skipRadixDance === false && opts.radixPoint != "" && $.inArray(opts.radixPoint, buffer) != -1)
  59. lastValidPosition = $.inArray(opts.radixPoint, buffer);
  60. }
  61. return lastValidPosition;
  62. },
  63. rightAlign: true,
  64. postFormat: function (buffer, pos, reformatOnly, opts) {
  65. var needsRefresh = false;
  66. if (opts.groupSeparator == "" || ($.inArray(opts.radixPoint, buffer) != -1 && pos > $.inArray(opts.radixPoint, buffer))) return { pos: pos };
  67. var cbuf = buffer.slice();
  68. if (!reformatOnly) {
  69. cbuf.splice(pos, 0, "?"); //set position indicator
  70. }
  71. var bufVal = cbuf.join('');
  72. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  73. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  74. needsRefresh = bufVal.indexOf(opts.groupSeparator) == 0;
  75. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  76. var radixSplit = bufVal.split(opts.radixPoint);
  77. bufVal = radixSplit[0];
  78. if (bufVal != (opts.prefix + "?0") && bufVal.length > (opts.groupSize + opts.prefix.length)) {
  79. needsRefresh = true;
  80. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  81. while (reg.test(bufVal)) {
  82. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  83. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  84. }
  85. }
  86. if (radixSplit.length > 1)
  87. bufVal += opts.radixPoint + radixSplit[1];
  88. }
  89. buffer.length = bufVal.length; //align the length
  90. for (var i = 0, l = bufVal.length; i < l; i++) {
  91. buffer[i] = bufVal.charAt(i);
  92. }
  93. var newPos = $.inArray("?", buffer);
  94. if (!reformatOnly) buffer.splice(newPos, 1);
  95. return { pos: reformatOnly ? pos : newPos, "refreshFromBuffer": needsRefresh };
  96. },
  97. onKeyDown: function (e, buffer, opts) {
  98. if (opts.autoGroup && (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE)) {
  99. return opts.postFormat(buffer, 0, true, opts);
  100. }
  101. },
  102. onKeyPress: function (e, buffer, opts) {
  103. var k = (e.which || e.charCode || e.keyCode);
  104. if (k == 46 && e.shiftKey == false && opts.radixPoint == ",") k = 44;
  105. if (opts.autoGroup && String.fromCharCode(k) == opts.radixPoint) {
  106. var refresh = opts.postFormat(buffer, 0, true, opts);
  107. refresh.caret = $.inArray(opts.radixPoint, buffer) + 1;
  108. return refresh;
  109. }
  110. },
  111. regex: {
  112. integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
  113. },
  114. negationhandler: function (chrs, buffer, pos, strict, opts) {
  115. if (!strict && opts.allowMinus && chrs === "-") {
  116. var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
  117. if (matchRslt.length > 0) {
  118. if (buffer[matchRslt.index] == "+") {
  119. return { "pos": matchRslt.index, "c": "-", "remove": matchRslt.index, "caret": pos };
  120. } else if (buffer[matchRslt.index] == "-") {
  121. return { "remove": matchRslt.index, "caret": pos - 1 };
  122. } else {
  123. return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
  124. }
  125. }
  126. }
  127. return false;
  128. },
  129. definitions: {
  130. '~': {
  131. validator: function (chrs, maskset, pos, strict, opts) {
  132. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  133. if (!isValid) {
  134. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  135. if (isValid === true) isValid = { pos: pos };
  136. if (isValid != false && !strict) {
  137. //handle 0 for integerpart
  138. var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  139. if (matchRslt) {
  140. if (matchRslt["0"][0].indexOf("0") == 0 && pos >= opts.prefix.length) {
  141. if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
  142. maskset.buffer.splice(matchRslt.index, 1);
  143. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  144. $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
  145. } else if (pos > matchRslt.index && pos <= radixPosition) {
  146. maskset.buffer.splice(matchRslt.index, 1);
  147. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  148. $.extend(isValid, { "pos": pos, "remove": matchRslt.index });
  149. }
  150. } else if (chrs == "0" && pos <= matchRslt.index) {
  151. return false;
  152. }
  153. }
  154. //handle overwrite when fixed precision
  155. if (opts.digitsOptional === false && pos > radixPosition) {
  156. return { "pos": pos, "remove": pos };
  157. }
  158. }
  159. if (isValid != false && !strict && chrs != opts.radixPoint && opts.autoGroup === true) {
  160. isValid = $.extend(isValid, opts.postFormat(maskset.buffer, pos, false, opts));
  161. }
  162. }
  163. return isValid;
  164. },
  165. cardinality: 1,
  166. prevalidator: null
  167. },
  168. '+': {
  169. validator: function (chrs, maskset, pos, strict, opts) {
  170. var signed = "[";
  171. if (opts.allowMinus === true) signed += "-";
  172. if (opts.allowPlus === true) signed += "\+";
  173. signed += "]";
  174. var isValid = new RegExp(signed).test(chrs);
  175. return isValid;
  176. },
  177. cardinality: 1,
  178. prevalidator: null
  179. },
  180. ':': {
  181. validator: function (chrs, maskset, pos, strict, opts) {
  182. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  183. if (!isValid) {
  184. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  185. isValid = new RegExp(radix).test(chrs);
  186. }
  187. return isValid;
  188. },
  189. cardinality: 1,
  190. prevalidator: null,
  191. placeholder: "" //radixpoint will be set in the mask function
  192. }
  193. },
  194. insertMode: true,
  195. autoUnmask: false,
  196. onUnMask: function (maskedValue, unmaskedValue, opts) {
  197. var processValue = maskedValue.replace(opts.prefix, "");
  198. processValue = processValue.replace(opts.suffix, "");
  199. processValue = processValue.replace(new RegExp(opts.groupSeparator, "g"), "");
  200. processValue = processValue.replace(opts.radixPoint, ".");
  201. return Number(processValue);
  202. },
  203. isComplete: function (buffer, opts) {
  204. var maskedValue = buffer.join('');
  205. var processValue = maskedValue.replace(opts.prefix, "");
  206. processValue = processValue.replace(opts.suffix, "");
  207. processValue = processValue.replace(new RegExp(opts.groupSeparator, "g"), "");
  208. processValue = processValue.replace(opts.radixPoint, ".");
  209. return isFinite(processValue);
  210. },
  211. onBeforeMask: function (initialValue, opts) {
  212. return isFinite(initialValue) ? initialValue.toString().replace(".", opts.radixPoint) : initialValue;
  213. }
  214. },
  215. 'decimal': {
  216. alias: "numeric"
  217. },
  218. 'integer': {
  219. alias: "numeric",
  220. digits: "0"
  221. }
  222. });
  223. })(jQuery);