jquery.inputmask.numeric.extensions.js 15 KB

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