jquery.inputmask.numeric.extensions.js 18 KB

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