inputmask.numeric.extensions.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*!
  2. * inputmask.numeric.extensions.js
  3. * https://github.com/RobinHerbots/Inputmask
  4. * Copyright (c) 2010 - 2018 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 4.0.3-beta.0
  7. */
  8. (function(factory) {
  9. if (typeof define === "function" && define.amd) {
  10. define([ "./inputmask" ], factory);
  11. } else if (typeof exports === "object") {
  12. module.exports = factory(require("./inputmask"));
  13. } else {
  14. factory(window.Inputmask);
  15. }
  16. })(function(Inputmask) {
  17. var $ = Inputmask.dependencyLib;
  18. function autoEscape(txt, opts) {
  19. var escapedTxt = "";
  20. for (var i = 0; i < txt.length; i++) {
  21. if (Inputmask.prototype.definitions[txt.charAt(i)] || opts.definitions[txt.charAt(i)] || opts.optionalmarker.start === txt.charAt(i) || opts.optionalmarker.end === txt.charAt(i) || opts.quantifiermarker.start === txt.charAt(i) || opts.quantifiermarker.end === txt.charAt(i) || opts.groupmarker.start === txt.charAt(i) || opts.groupmarker.end === txt.charAt(i) || opts.alternatormarker === txt.charAt(i)) {
  22. escapedTxt += "\\" + txt.charAt(i);
  23. } else escapedTxt += txt.charAt(i);
  24. }
  25. return escapedTxt;
  26. }
  27. function alignDigits(buffer, opts) {
  28. if (opts.numericInput) {
  29. var radixPosition = $.inArray(opts.radixPoint, buffer);
  30. if (radixPosition === -1) {
  31. buffer.push(opts.radixPoint);
  32. radixPosition = buffer.length - 1;
  33. }
  34. for (var i = 1; i <= opts.digits; i++) {
  35. buffer[radixPosition + i] = buffer[radixPosition + i] || "0";
  36. }
  37. }
  38. return buffer;
  39. }
  40. Inputmask.extendAliases({
  41. numeric: {
  42. mask: function(opts) {
  43. if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
  44. opts.integerDigits = opts.repeat;
  45. }
  46. opts.repeat = 0;
  47. if (opts.groupSeparator === opts.radixPoint && opts.digits && opts.digits !== "0") {
  48. if (opts.radixPoint === ".") {
  49. opts.groupSeparator = ",";
  50. } else if (opts.radixPoint === ",") {
  51. opts.groupSeparator = ".";
  52. } else opts.groupSeparator = "";
  53. }
  54. if (opts.groupSeparator === " ") {
  55. opts.skipOptionalPartCharacter = undefined;
  56. }
  57. opts.autoGroup = opts.autoGroup && opts.groupSeparator !== "";
  58. if (opts.autoGroup) {
  59. if (typeof opts.groupSize == "string" && isFinite(opts.groupSize)) opts.groupSize = parseInt(opts.groupSize);
  60. if (isFinite(opts.integerDigits)) {
  61. var seps = Math.floor(opts.integerDigits / opts.groupSize);
  62. var mod = opts.integerDigits % opts.groupSize;
  63. opts.integerDigits = parseInt(opts.integerDigits) + (mod === 0 ? seps - 1 : seps);
  64. if (opts.integerDigits < 1) {
  65. opts.integerDigits = "*";
  66. }
  67. }
  68. }
  69. if (opts.placeholder.length > 1) {
  70. opts.placeholder = opts.placeholder.charAt(0);
  71. }
  72. if (opts.positionCaretOnClick === "radixFocus" && (opts.placeholder === "" && opts.integerOptional === false)) {
  73. opts.positionCaretOnClick = "lvp";
  74. }
  75. opts.definitions[";"] = opts.definitions["~"];
  76. opts.definitions[";"].definitionSymbol = "~";
  77. if (opts.numericInput === true) {
  78. opts.positionCaretOnClick = opts.positionCaretOnClick === "radixFocus" ? "lvp" : opts.positionCaretOnClick;
  79. opts.digitsOptional = false;
  80. if (isNaN(opts.digits)) opts.digits = 2;
  81. opts.decimalProtect = false;
  82. }
  83. var mask = "[+]";
  84. mask += autoEscape(opts.prefix, opts);
  85. if (opts.integerOptional === true) {
  86. mask += "~{1," + opts.integerDigits + "}";
  87. } else mask += "~{" + opts.integerDigits + "}";
  88. if (opts.digits !== undefined) {
  89. var radixDef = opts.decimalProtect ? ":" : opts.radixPoint;
  90. var dq = opts.digits.toString().split(",");
  91. if (isFinite(dq[0]) && dq[1] && isFinite(dq[1])) {
  92. mask += radixDef + ";{" + opts.digits + "}";
  93. } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
  94. if (opts.digitsOptional) {
  95. mask += "[" + radixDef + ";{1," + opts.digits + "}]";
  96. } else mask += radixDef + ";{" + opts.digits + "}";
  97. }
  98. }
  99. mask += autoEscape(opts.suffix, opts);
  100. mask += "[-]";
  101. opts.greedy = false;
  102. return mask;
  103. },
  104. placeholder: "",
  105. greedy: false,
  106. digits: "*",
  107. digitsOptional: true,
  108. enforceDigitsOnBlur: false,
  109. radixPoint: ".",
  110. positionCaretOnClick: "radixFocus",
  111. groupSize: 3,
  112. groupSeparator: "",
  113. autoGroup: false,
  114. allowMinus: true,
  115. negationSymbol: {
  116. front: "-",
  117. back: ""
  118. },
  119. integerDigits: "+",
  120. integerOptional: true,
  121. prefix: "",
  122. suffix: "",
  123. rightAlign: true,
  124. decimalProtect: true,
  125. min: null,
  126. max: null,
  127. step: 1,
  128. insertMode: true,
  129. autoUnmask: false,
  130. unmaskAsNumber: false,
  131. inputmode: "numeric",
  132. preValidation: function(buffer, pos, c, isSelection, opts, maskset) {
  133. if (c === "-" || c === opts.negationSymbol.front) {
  134. if (opts.allowMinus !== true) return false;
  135. opts.isNegative = opts.isNegative === undefined ? true : !opts.isNegative;
  136. if (buffer.join("") === "") return true;
  137. return {
  138. caret: maskset.validPositions[pos] ? pos : undefined,
  139. dopost: true
  140. };
  141. }
  142. if (isSelection === false && c === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  143. var radixPos = $.inArray(opts.radixPoint, buffer);
  144. if (radixPos !== -1 && maskset.validPositions[radixPos] !== undefined) {
  145. if (opts.numericInput === true) {
  146. return pos === radixPos;
  147. }
  148. return {
  149. caret: radixPos + 1
  150. };
  151. }
  152. }
  153. return true;
  154. },
  155. postValidation: function(buffer, pos, currentResult, opts) {
  156. function buildPostMask(buffer, opts) {
  157. var postMask = "";
  158. postMask += "(" + opts.groupSeparator + "*{" + opts.groupSize + "}){*}";
  159. if (opts.radixPoint !== "") {
  160. var radixSplit = buffer.join("").split(opts.radixPoint);
  161. if (radixSplit[1]) {
  162. postMask += opts.radixPoint + "*{" + radixSplit[1].match(/^\d*\??\d*/)[0].length + "}";
  163. }
  164. }
  165. return postMask;
  166. }
  167. var suffix = opts.suffix.split(""), prefix = opts.prefix.split("");
  168. if (currentResult.pos === undefined && currentResult.caret !== undefined && currentResult.dopost !== true) return currentResult;
  169. var caretPos = currentResult.caret !== undefined ? currentResult.caret : currentResult.pos;
  170. var maskedValue = buffer.slice();
  171. if (opts.numericInput) {
  172. caretPos = maskedValue.length - caretPos - 1;
  173. maskedValue = maskedValue.reverse();
  174. }
  175. var charAtPos = maskedValue[caretPos];
  176. if (charAtPos === opts.groupSeparator) {
  177. caretPos += 1;
  178. charAtPos = maskedValue[caretPos];
  179. }
  180. if (caretPos === maskedValue.length - opts.suffix.length - 1 && charAtPos === opts.radixPoint) return currentResult;
  181. if (charAtPos !== undefined) {
  182. if (charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back) {
  183. maskedValue[caretPos] = "?";
  184. if (opts.prefix.length > 0 && caretPos >= (opts.isNegative === false ? 1 : 0) && caretPos < opts.prefix.length - 1 + (opts.isNegative === false ? 1 : 0)) {
  185. prefix[caretPos - (opts.isNegative === false ? 1 : 0)] = "?";
  186. } else if (opts.suffix.length > 0 && caretPos >= maskedValue.length - opts.suffix.length - (opts.isNegative === false ? 1 : 0)) {
  187. suffix[caretPos - (maskedValue.length - opts.suffix.length - (opts.isNegative === false ? 1 : 0))] = "?";
  188. }
  189. }
  190. }
  191. prefix = prefix.join("");
  192. suffix = suffix.join("");
  193. var processValue = maskedValue.join("").replace(prefix, "");
  194. processValue = processValue.replace(suffix, "");
  195. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  196. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  197. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  198. if (isNaN(opts.placeholder)) {
  199. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.placeholder), "g"), "");
  200. }
  201. if (processValue.length > 1 && processValue.indexOf(opts.radixPoint) !== 1) {
  202. if (charAtPos === "0") {
  203. processValue = processValue.replace(/^\?/g, "");
  204. }
  205. processValue = processValue.replace(/^0/g, "");
  206. }
  207. if (processValue.charAt(0) === opts.radixPoint && opts.radixPoint !== "" && opts.numericInput !== true) {
  208. processValue = "0" + processValue;
  209. }
  210. if (processValue !== "") {
  211. processValue = processValue.split("");
  212. if ((!opts.digitsOptional || opts.enforceDigitsOnBlur && currentResult.event === "blur") && isFinite(opts.digits)) {
  213. var radixPosition = $.inArray(opts.radixPoint, processValue);
  214. var rpb = $.inArray(opts.radixPoint, maskedValue);
  215. if (radixPosition === -1) {
  216. processValue.push(opts.radixPoint);
  217. radixPosition = processValue.length - 1;
  218. }
  219. for (var i = 1; i <= opts.digits; i++) {
  220. if ((!opts.digitsOptional || opts.enforceDigitsOnBlur && currentResult.event === "blur") && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  221. processValue[radixPosition + i] = currentResult.placeholder || opts.placeholder.charAt(0);
  222. } else if (rpb !== -1 && maskedValue[rpb + i] !== undefined) {
  223. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  224. }
  225. }
  226. }
  227. if (opts.autoGroup === true && opts.groupSeparator !== "" && (charAtPos !== opts.radixPoint || currentResult.pos !== undefined || currentResult.dopost)) {
  228. var addRadix = processValue[processValue.length - 1] === opts.radixPoint && currentResult.c === opts.radixPoint;
  229. processValue = Inputmask(buildPostMask(processValue, opts), {
  230. numericInput: true,
  231. jitMasking: true,
  232. definitions: {
  233. "*": {
  234. validator: "[0-9?]",
  235. cardinality: 1
  236. }
  237. }
  238. }).format(processValue.join(""));
  239. if (addRadix) processValue += opts.radixPoint;
  240. if (processValue.charAt(0) === opts.groupSeparator) {
  241. processValue.substr(1);
  242. }
  243. } else processValue = processValue.join("");
  244. }
  245. if (opts.isNegative && currentResult.event === "blur") {
  246. opts.isNegative = processValue !== "0";
  247. }
  248. processValue = prefix + processValue;
  249. processValue += suffix;
  250. if (opts.isNegative) {
  251. processValue = opts.negationSymbol.front + processValue;
  252. processValue += opts.negationSymbol.back;
  253. }
  254. processValue = processValue.split("");
  255. if (charAtPos !== undefined) {
  256. if (charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back) {
  257. caretPos = $.inArray("?", processValue);
  258. if (caretPos > -1) {
  259. processValue[caretPos] = charAtPos;
  260. } else caretPos = currentResult.caret || 0;
  261. } else if (charAtPos === opts.radixPoint || charAtPos === opts.negationSymbol.front || charAtPos === opts.negationSymbol.back) {
  262. var newCaretPos = $.inArray(charAtPos, processValue);
  263. if (newCaretPos !== -1) caretPos = newCaretPos;
  264. }
  265. }
  266. if (opts.numericInput) {
  267. caretPos = processValue.length - caretPos - 1;
  268. processValue = processValue.reverse();
  269. }
  270. var rslt = {
  271. caret: (charAtPos === undefined || currentResult.pos !== undefined) && caretPos !== undefined ? caretPos + (opts.numericInput ? -1 : 1) : caretPos,
  272. buffer: processValue,
  273. refreshFromBuffer: currentResult.dopost || buffer.join("") !== processValue.join("")
  274. };
  275. return rslt.refreshFromBuffer ? rslt : currentResult;
  276. },
  277. onBeforeWrite: function(e, buffer, caretPos, opts) {
  278. function parseMinMaxOptions(opts) {
  279. if (opts.parseMinMaxOptions === undefined) {
  280. if (opts.min !== null) {
  281. opts.min = opts.min.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  282. if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
  283. opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN;
  284. if (isNaN(opts.min)) opts.min = Number.MIN_VALUE;
  285. }
  286. if (opts.max !== null) {
  287. opts.max = opts.max.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  288. if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
  289. opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN;
  290. if (isNaN(opts.max)) opts.max = Number.MAX_VALUE;
  291. }
  292. opts.parseMinMaxOptions = "done";
  293. }
  294. }
  295. if (e) {
  296. switch (e.type) {
  297. case "keydown":
  298. return opts.postValidation(buffer, caretPos, {
  299. caret: caretPos,
  300. dopost: true
  301. }, opts);
  302. case "blur":
  303. case "checkval":
  304. var unmasked;
  305. parseMinMaxOptions(opts);
  306. if (opts.min !== null || opts.max !== null) {
  307. unmasked = opts.onUnMask(buffer.join(""), undefined, $.extend({}, opts, {
  308. unmaskAsNumber: true
  309. }));
  310. if (opts.min !== null && unmasked < opts.min) {
  311. opts.isNegative = opts.min < 0;
  312. return opts.postValidation(opts.min.toString().replace(".", opts.radixPoint).split(""), caretPos, {
  313. caret: caretPos,
  314. dopost: true,
  315. placeholder: "0"
  316. }, opts);
  317. } else if (opts.max !== null && unmasked > opts.max) {
  318. opts.isNegative = opts.max < 0;
  319. return opts.postValidation(opts.max.toString().replace(".", opts.radixPoint).split(""), caretPos, {
  320. caret: caretPos,
  321. dopost: true,
  322. placeholder: "0"
  323. }, opts);
  324. }
  325. }
  326. return opts.postValidation(buffer, caretPos, {
  327. caret: caretPos,
  328. placeholder: "0",
  329. event: "blur"
  330. }, opts);
  331. case "_checkval":
  332. return {
  333. caret: caretPos
  334. };
  335. default:
  336. break;
  337. }
  338. }
  339. },
  340. regex: {
  341. integerPart: function(opts, emptyCheck) {
  342. return emptyCheck ? new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]?") : new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]?\\d+");
  343. },
  344. integerNPart: function(opts) {
  345. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + Inputmask.escapeRegex(opts.placeholder.charAt(0)) + "]+");
  346. }
  347. },
  348. definitions: {
  349. "~": {
  350. validator: function(chrs, maskset, pos, strict, opts, isSelection) {
  351. var isValid, l;
  352. if (chrs === "k" || chrs === "m") {
  353. isValid = {
  354. insert: [],
  355. c: 0
  356. };
  357. for (var i = 0, l = chrs === "k" ? 2 : 5; i < l; i++) {
  358. isValid.insert.push({
  359. pos: pos + i,
  360. c: 0
  361. });
  362. }
  363. isValid.pos = pos + l;
  364. return isValid;
  365. }
  366. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  367. if (isValid === true) {
  368. if (opts.numericInput !== true && maskset.validPositions[pos] !== undefined && maskset.validPositions[pos].match.def === "~" && !isSelection) {
  369. var processValue = maskset.buffer.join("");
  370. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  371. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  372. var pvRadixSplit = processValue.split(opts.radixPoint);
  373. if (pvRadixSplit.length > 1) {
  374. pvRadixSplit[1] = pvRadixSplit[1].replace(/0/g, opts.placeholder.charAt(0));
  375. }
  376. if (pvRadixSplit[0] === "0") {
  377. pvRadixSplit[0] = pvRadixSplit[0].replace(/0/g, opts.placeholder.charAt(0));
  378. }
  379. processValue = pvRadixSplit[0] + opts.radixPoint + pvRadixSplit[1] || "";
  380. var bufferTemplate = maskset._buffer.join("");
  381. if (processValue === opts.radixPoint) {
  382. processValue = bufferTemplate;
  383. }
  384. while (processValue.match(Inputmask.escapeRegex(bufferTemplate) + "$") === null) {
  385. bufferTemplate = bufferTemplate.slice(1);
  386. }
  387. processValue = processValue.replace(bufferTemplate, "");
  388. processValue = processValue.split("");
  389. if (processValue[pos] === undefined) {
  390. isValid = {
  391. pos: pos,
  392. remove: pos
  393. };
  394. } else {
  395. isValid = {
  396. pos: pos
  397. };
  398. }
  399. }
  400. } else if (!strict && chrs === opts.radixPoint && maskset.validPositions[pos - 1] === undefined) {
  401. isValid = {
  402. insert: {
  403. pos: pos,
  404. c: 0
  405. },
  406. pos: pos + 1
  407. };
  408. }
  409. return isValid;
  410. },
  411. cardinality: 1
  412. },
  413. "+": {
  414. validator: function(chrs, maskset, pos, strict, opts) {
  415. return opts.allowMinus && (chrs === "-" || chrs === opts.negationSymbol.front);
  416. },
  417. cardinality: 1,
  418. placeholder: ""
  419. },
  420. "-": {
  421. validator: function(chrs, maskset, pos, strict, opts) {
  422. return opts.allowMinus && chrs === opts.negationSymbol.back;
  423. },
  424. cardinality: 1,
  425. placeholder: ""
  426. },
  427. ":": {
  428. validator: function(chrs, maskset, pos, strict, opts) {
  429. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  430. var isValid = new RegExp(radix).test(chrs);
  431. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  432. isValid = {
  433. caret: pos + 1
  434. };
  435. }
  436. return isValid;
  437. },
  438. cardinality: 1,
  439. placeholder: function(opts) {
  440. return opts.radixPoint;
  441. }
  442. }
  443. },
  444. onUnMask: function(maskedValue, unmaskedValue, opts) {
  445. if (unmaskedValue === "" && opts.nullable === true) {
  446. return unmaskedValue;
  447. }
  448. var processValue = maskedValue.replace(opts.prefix, "");
  449. processValue = processValue.replace(opts.suffix, "");
  450. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  451. if (opts.placeholder.charAt(0) !== "") {
  452. processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0");
  453. }
  454. if (opts.unmaskAsNumber) {
  455. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  456. processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  457. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  458. return Number(processValue);
  459. }
  460. return processValue;
  461. },
  462. isComplete: function(buffer, opts) {
  463. var maskedValue = (opts.numericInput ? buffer.slice().reverse() : buffer).join("");
  464. maskedValue = maskedValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  465. maskedValue = maskedValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  466. maskedValue = maskedValue.replace(opts.prefix, "");
  467. maskedValue = maskedValue.replace(opts.suffix, "");
  468. maskedValue = maskedValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"), "$1");
  469. if (opts.radixPoint === ",") maskedValue = maskedValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  470. return isFinite(maskedValue);
  471. },
  472. onBeforeMask: function(initialValue, opts) {
  473. opts.isNegative = undefined;
  474. if (typeof initialValue == "number" && opts.radixPoint !== "") {
  475. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  476. }
  477. initialValue = initialValue.toString().charAt(initialValue.length - 1) === opts.radixPoint ? initialValue.toString().substr(0, initialValue.length - 1) : initialValue.toString();
  478. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  479. var vs = initialValue.split("."), groupSize = opts.groupSeparator !== "" ? parseInt(opts.groupSize) : 0;
  480. if (vs.length === 2 && (vs[0].length > groupSize || vs[1].length > groupSize || vs[0].length <= groupSize && vs[1].length < groupSize)) {
  481. initialValue = initialValue.replace(".", opts.radixPoint);
  482. }
  483. }
  484. var kommaMatches = initialValue.match(/,/g);
  485. var dotMatches = initialValue.match(/\./g);
  486. if (dotMatches && kommaMatches) {
  487. if (dotMatches.length > kommaMatches.length) {
  488. initialValue = initialValue.replace(/\./g, "");
  489. initialValue = initialValue.replace(",", opts.radixPoint);
  490. } else if (kommaMatches.length > dotMatches.length) {
  491. initialValue = initialValue.replace(/,/g, "");
  492. initialValue = initialValue.replace(".", opts.radixPoint);
  493. } else {
  494. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue.replace(/,/g, "");
  495. }
  496. } else {
  497. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  498. }
  499. if (opts.digits === 0) {
  500. if (initialValue.indexOf(".") !== -1) {
  501. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  502. } else if (initialValue.indexOf(",") !== -1) {
  503. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  504. }
  505. }
  506. if (opts.radixPoint !== "" && isFinite(opts.digits)) {
  507. if (initialValue.indexOf(opts.radixPoint) !== -1) {
  508. var valueParts = initialValue.split(opts.radixPoint), decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  509. if (parseInt(opts.digits) < decPart.toString().length) {
  510. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  511. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  512. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  513. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  514. }
  515. }
  516. }
  517. return alignDigits(initialValue.toString().split(""), opts).join("");
  518. },
  519. onKeyDown: function(e, buffer, caretPos, opts) {
  520. var $input = $(this);
  521. if (e.ctrlKey) {
  522. switch (e.keyCode) {
  523. case Inputmask.keyCode.UP:
  524. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  525. $input.trigger("setvalue");
  526. break;
  527. case Inputmask.keyCode.DOWN:
  528. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  529. $input.trigger("setvalue");
  530. break;
  531. }
  532. }
  533. }
  534. },
  535. currency: {
  536. prefix: "$ ",
  537. groupSeparator: ",",
  538. alias: "numeric",
  539. placeholder: "0",
  540. autoGroup: true,
  541. digits: 2,
  542. digitsOptional: false,
  543. clearMaskOnLostFocus: false
  544. },
  545. decimal: {
  546. alias: "numeric"
  547. },
  548. integer: {
  549. alias: "numeric",
  550. digits: 0,
  551. radixPoint: ""
  552. },
  553. percentage: {
  554. alias: "numeric",
  555. digits: 2,
  556. digitsOptional: true,
  557. radixPoint: ".",
  558. placeholder: "0",
  559. autoGroup: false,
  560. min: 0,
  561. max: 100,
  562. suffix: " %",
  563. allowMinus: false
  564. }
  565. });
  566. return Inputmask;
  567. });