jquery.inputmask.numeric.extensions.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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[matchRslt.index] == "-0" && (matchRsltDigits == undefined || matchRsltDigits[matchRsltDigits.index].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[matchRslt.index].length > 0 && (matchRslt[matchRslt.index] !== "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 || matchRslt.index < radixPosition)) {
  171. if (matchRslt["0"] == "0" && pos >= opts.prefix.length) {
  172. if (radixPosition == -1 || (pos <= radixPosition && maskset["validPositions"][radixPosition] == undefined)) {
  173. maskset.buffer.splice(matchRslt.index, 1);
  174. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  175. return { "pos": pos, "remove": matchRslt.index };
  176. } else if (pos > matchRslt.index && pos <= radixPosition) {
  177. maskset.buffer.splice(matchRslt.index, 1);
  178. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  179. return { "pos": pos, "remove": matchRslt.index };
  180. } if (maskset["validPositions"][radixPosition] == undefined) {
  181. maskset["buffer"][pos] = chrs;
  182. return { "refreshFromBuffer": true };
  183. }
  184. } else if (chrs == "0" && pos <= matchRslt.index) {
  185. return false;
  186. }
  187. }
  188. return true;
  189. },
  190. definitions: {
  191. '~': {
  192. validator: function (chrs, maskset, pos, strict, opts) {
  193. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  194. if (!isValid) {
  195. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  196. if (!isValid) {
  197. isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  198. if (isValid === true) {
  199. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  200. if (isValid === true) {
  201. //handle overwrite when fixed precision
  202. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  203. if (opts.digitsOptional === false && pos > radixPosition && !strict) {
  204. isValid = { "pos": pos, "remove": pos };
  205. } else isValid = { pos: pos };
  206. }
  207. }
  208. }
  209. }
  210. return isValid;
  211. },
  212. cardinality: 1,
  213. prevalidator: null
  214. },
  215. '+': {
  216. validator: function (chrs, maskset, pos, strict, opts) {
  217. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  218. if (!isValid) {
  219. isValid = (opts.allowMinus && chrs == "-") || (opts.allowPlus && chrs == "+");
  220. }
  221. return isValid;
  222. },
  223. cardinality: 1,
  224. prevalidator: null,
  225. placeholder: ''
  226. },
  227. ':': {
  228. validator: function (chrs, maskset, pos, strict, opts) {
  229. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  230. if (!isValid) {
  231. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  232. isValid = new RegExp(radix).test(chrs);
  233. if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
  234. isValid = { "caret": pos + 1 };
  235. }
  236. }
  237. return isValid;
  238. },
  239. cardinality: 1,
  240. prevalidator: null,
  241. placeholder: function (opts) { return opts.radixPoint; }
  242. }
  243. },
  244. insertMode: true,
  245. autoUnmask: false,
  246. onUnMask: function (maskedValue, unmaskedValue, opts) {
  247. var processValue = maskedValue.replace(opts.prefix, "");
  248. processValue = processValue.replace(opts.suffix, "");
  249. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  250. //processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  251. return processValue;
  252. },
  253. isComplete: function (buffer, opts) {
  254. var maskedValue = buffer.join(''), bufClone = buffer.slice();
  255. //verify separator positions
  256. opts.postFormat(bufClone, 0, true, opts);
  257. if (bufClone.join('') != maskedValue) return false;
  258. var processValue = maskedValue.replace(opts.prefix, "");
  259. processValue = processValue.replace(opts.suffix, "");
  260. processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  261. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  262. return isFinite(processValue);
  263. },
  264. onBeforeMask: function (initialValue, opts) {
  265. if (opts.radixPoint != "" && isFinite(initialValue)) {
  266. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  267. } else {
  268. var kommaMatches = initialValue.match(/,/g);
  269. var dotMatches = initialValue.match(/\./g);
  270. if (dotMatches && kommaMatches) {
  271. if (dotMatches.length > kommaMatches.length) {
  272. initialValue = initialValue.replace(/\./g, "");
  273. initialValue = initialValue.replace(",", opts.radixPoint);
  274. } else if (kommaMatches.length > dotMatches.length) {
  275. initialValue = initialValue.replace(/,/g, "");
  276. initialValue = initialValue.replace(".", opts.radixPoint);
  277. } else { //equal
  278. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  279. }
  280. } else {
  281. initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  282. }
  283. }
  284. if (opts.digits == 0) {
  285. if (initialValue.indexOf(".") != -1) {
  286. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  287. } else if (initialValue.indexOf(",") != -1) {
  288. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  289. }
  290. }
  291. return initialValue;
  292. }
  293. },
  294. 'currency': {
  295. prefix: "$ ",
  296. groupSeparator: ",",
  297. alias: "numeric",
  298. placeholder: "0",
  299. autoGroup: true,
  300. digits: 2,
  301. digitsOptional: false,
  302. clearMaskOnLostFocus: false
  303. },
  304. 'decimal': {
  305. alias: "numeric"
  306. },
  307. 'integer': {
  308. alias: "numeric",
  309. digits: "0",
  310. radixPoint: ""
  311. }
  312. });
  313. return $.fn.inputmask;
  314. })(jQuery);