jquery.inputmask.numeric.extensions.js 17 KB

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