jquery.inputmask.numeric.extensions.js 17 KB

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