jquery.inputmask.numeric.extensions.js 15 KB

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