jquery.inputmask.numeric.extensions.js 18 KB

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