jquery.inputmask.numeric.extensions.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.decimalProtect ? ":" : opts.radixPoint) + "~{" + opts.digits + "}]";
  31. else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + "~{" + 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. rightAlign: true,
  50. decimalProtect: false, //only decimals allowed after entering the radixpoint
  51. postFormat: function (buffer, pos, reformatOnly, opts) { //this needs to be removed // this is crap
  52. var needsRefresh = false, charAtPos = buffer[pos];
  53. if (opts.groupSeparator == "" ||
  54. ($.inArray(opts.radixPoint, buffer) != -1 && pos >= $.inArray(opts.radixPoint, buffer)) ||
  55. new RegExp('[-\+]').test(charAtPos)
  56. ) return { pos: pos };
  57. var cbuf = buffer.slice();
  58. if (charAtPos == opts.groupSeparator) {
  59. cbuf.splice(pos--, 1);
  60. charAtPos = cbuf[pos];
  61. }
  62. if (reformatOnly) cbuf[pos] = "?"; else cbuf.splice(pos, 0, "?"); //set position indicator
  63. var bufVal = cbuf.join('');
  64. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  65. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  66. needsRefresh = bufVal.indexOf(opts.groupSeparator) == 0;
  67. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
  68. var radixSplit = bufVal.split(opts.radixPoint);
  69. bufVal = radixSplit[0];
  70. if (bufVal != (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
  71. needsRefresh = true;
  72. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  73. while (reg.test(bufVal)) {
  74. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  75. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  76. }
  77. }
  78. if (radixSplit.length > 1)
  79. bufVal += opts.radixPoint + radixSplit[1];
  80. }
  81. buffer.length = bufVal.length; //align the length
  82. for (var i = 0, l = bufVal.length; i < l; i++) {
  83. buffer[i] = bufVal.charAt(i);
  84. }
  85. var newPos = $.inArray("?", buffer);
  86. if (reformatOnly) buffer[newPos] = charAtPos; else buffer.splice(newPos, 1);
  87. return { pos: newPos, "refreshFromBuffer": needsRefresh };
  88. },
  89. onKeyDown: function (e, buffer, caretPos, opts) {
  90. if (e.keyCode == opts.keyCode.TAB && opts.placeholder.charAt(0) != "0") {
  91. var radixPosition = $.inArray(opts.radixPoint, buffer);
  92. if (radixPosition != -1 && isFinite(opts.digits)) {
  93. for (var i = 1; i <= opts.digits; i++) {
  94. if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == opts.placeholder.charAt(0)) buffer[radixPosition + i] = "0";
  95. }
  96. return { "refreshFromBuffer": { start: ++radixPosition, end: radixPosition + opts.digits } };
  97. }
  98. } else if (opts.autoGroup && (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE)) {
  99. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  100. rslt.caret = rslt.pos + 1;
  101. return rslt;
  102. }
  103. },
  104. onKeyPress: function (e, buffer, caretPos, opts) {
  105. if (opts.autoGroup /*&& String.fromCharCode(k) == opts.radixPoint*/) {
  106. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  107. rslt.caret = rslt.pos + 1;
  108. return rslt;
  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. radixhandler: function (chrs, maskset, pos, strict, opts) {
  130. if (!strict && chrs === opts.radixPoint) {
  131. var radixPos = $.inArray(opts.radixPoint, maskset.buffer.join('')), integerValue = maskset.buffer.join('').match(opts.regex.integerPart(opts));
  132. if (radixPos != -1) {
  133. if (maskset["validPositions"][radixPos - 1])
  134. return { "caret": radixPos + 1 };
  135. else return { "pos": integerValue.index, c: integerValue[0], "caret": radixPos + 1 };
  136. }
  137. }
  138. return false;
  139. },
  140. leadingZeroHandler: function (chrs, maskset, pos, strict, opts) {
  141. var matchRslt = maskset.buffer.join('').match(opts.regex.integerPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  142. if (matchRslt && !strict && (radixPosition == -1 || matchRslt.index < radixPosition)) {
  143. if (matchRslt["0"].indexOf("0") == 0 && pos >= opts.prefix.length) {
  144. if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
  145. maskset.buffer.splice(matchRslt.index, 1);
  146. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  147. return { "pos": pos, "remove": matchRslt.index };
  148. } else if (pos > matchRslt.index && pos <= radixPosition) {
  149. maskset.buffer.splice(matchRslt.index, 1);
  150. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  151. return { "pos": pos, "remove": matchRslt.index };
  152. }
  153. } else if (chrs == "0" && pos <= matchRslt.index) {
  154. return false;
  155. }
  156. }
  157. return true;
  158. },
  159. definitions: {
  160. '~': {
  161. validator: function (chrs, maskset, pos, strict, opts) {
  162. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  163. if (!isValid) {
  164. isValid = opts.radixhandler(chrs, maskset, pos, strict, opts);
  165. if (!isValid) {
  166. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  167. if (isValid === true) {
  168. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  169. if (isValid === true) {
  170. //handle overwrite when fixed precision
  171. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  172. if (opts.digitsOptional === false && pos > radixPosition && !strict) {
  173. return { "pos": pos, "remove": pos };
  174. } else return { pos: pos };
  175. }
  176. }
  177. }
  178. }
  179. return isValid;
  180. },
  181. cardinality: 1,
  182. prevalidator: null
  183. },
  184. '+': {
  185. validator: function (chrs, maskset, pos, strict, opts) {
  186. var signed = "[";
  187. if (opts.allowMinus === true) signed += "-";
  188. if (opts.allowPlus === true) signed += "\+";
  189. signed += "]";
  190. return new RegExp(signed).test(chrs);
  191. },
  192. cardinality: 1,
  193. prevalidator: null
  194. },
  195. ':': {
  196. validator: function (chrs, maskset, pos, strict, opts) {
  197. var isValid = opts.negationhandler(chrs, maskset.buffer, pos, strict, opts);
  198. if (!isValid) {
  199. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  200. isValid = new RegExp(radix).test(chrs);
  201. if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
  202. isValid = { "pos": pos, "remove": pos };
  203. }
  204. }
  205. return isValid;
  206. },
  207. cardinality: 1,
  208. prevalidator: null,
  209. placeholder: "" //radixpoint will be set in the mask function
  210. }
  211. },
  212. insertMode: true,
  213. autoUnmask: false,
  214. onUnMask: function (maskedValue, unmaskedValue, opts) {
  215. var processValue = maskedValue.replace(opts.prefix, "");
  216. processValue = processValue.replace(opts.suffix, "");
  217. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  218. //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  219. return processValue;
  220. },
  221. isComplete: function (buffer, opts) {
  222. var maskedValue = buffer.join(''), bufClone = buffer.slice();
  223. //verify separator positions
  224. opts.postFormat(bufClone, 0, true, opts);
  225. if (bufClone.join('') != maskedValue) return false;
  226. var processValue = maskedValue.replace(opts.prefix, "");
  227. processValue = processValue.replace(opts.suffix, "");
  228. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  229. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  230. return isFinite(processValue);
  231. },
  232. onBeforeMask: function (initialValue, opts) {
  233. if (isFinite(initialValue)) {
  234. return initialValue.toString().replace(".", opts.radixPoint);
  235. } else {
  236. var kommaMatches = initialValue.match(/,/g);
  237. var dotMatches = initialValue.match(/\./g);
  238. if (dotMatches && kommaMatches) {
  239. if (dotMatches.length > kommaMatches.length) {
  240. initialValue = initialValue.replace(/\./g, "");
  241. initialValue = initialValue.replace(",", opts.radixPoint);
  242. } else if (kommaMatches.length > dotMatches.length) {
  243. initialValue = initialValue.replace(/,/g, "");
  244. initialValue = initialValue.replace(".", opts.radixPoint);
  245. }
  246. } else {
  247. initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  248. }
  249. return initialValue;
  250. }
  251. }
  252. },
  253. 'decimal': {
  254. alias: "numeric"
  255. },
  256. 'integer': {
  257. alias: "numeric",
  258. digits: "0"
  259. }
  260. });
  261. return $.fn.inputmask;
  262. })(jQuery);