inputmask.numeric.extensions.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*!
  2. * inputmask.numeric.extensions.js
  3. * https://github.com/RobinHerbots/Inputmask
  4. * Copyright (c) 2010 - 2019 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 4.0.9
  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, digits, opts) {
  28. if (digits > 0) {
  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 <= 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. inputType: "text",
  132. inputmode: "numeric",
  133. preValidation: function(buffer, pos, c, isSelection, opts, maskset) {
  134. if (c === "-" || c === opts.negationSymbol.front) {
  135. if (opts.allowMinus !== true) return false;
  136. opts.isNegative = opts.isNegative === undefined ? true : !opts.isNegative;
  137. if (buffer.join("") === "") return true;
  138. return {
  139. caret: maskset.validPositions[pos] ? pos : undefined,
  140. dopost: true
  141. };
  142. }
  143. if (isSelection === false && c === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  144. var radixPos = $.inArray(opts.radixPoint, buffer);
  145. if (radixPos !== -1 && maskset.validPositions[radixPos] !== undefined) {
  146. if (opts.numericInput === true) {
  147. return pos === radixPos;
  148. }
  149. return {
  150. caret: radixPos + 1
  151. };
  152. }
  153. }
  154. return true;
  155. },
  156. postValidation: function(buffer, pos, currentResult, opts) {
  157. function buildPostMask(buffer, opts) {
  158. var postMask = "";
  159. postMask += "(" + opts.groupSeparator + "*{" + opts.groupSize + "}){*}";
  160. if (opts.radixPoint !== "") {
  161. var radixSplit = buffer.join("").split(opts.radixPoint);
  162. if (radixSplit[1]) {
  163. postMask += opts.radixPoint + "*{" + radixSplit[1].match(/^\d*\??\d*/)[0].length + "}";
  164. }
  165. }
  166. return postMask;
  167. }
  168. var suffix = opts.suffix.split(""), prefix = opts.prefix.split("");
  169. if (currentResult.pos === undefined && currentResult.caret !== undefined && currentResult.dopost !== true) return currentResult;
  170. var caretPos = currentResult.caret !== undefined ? currentResult.caret : currentResult.pos;
  171. var maskedValue = buffer.slice();
  172. if (opts.numericInput) {
  173. caretPos = maskedValue.length - caretPos - 1;
  174. maskedValue = maskedValue.reverse();
  175. }
  176. var charAtPos = maskedValue[caretPos];
  177. if (charAtPos === opts.groupSeparator) {
  178. caretPos += 1;
  179. charAtPos = maskedValue[caretPos];
  180. }
  181. if (caretPos === maskedValue.length - opts.suffix.length - 1 && charAtPos === opts.radixPoint) return currentResult;
  182. if (charAtPos !== undefined) {
  183. if (charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back) {
  184. maskedValue[caretPos] = "?";
  185. if (opts.prefix.length > 0 && caretPos >= (opts.isNegative === false ? 1 : 0) && caretPos < opts.prefix.length - 1 + (opts.isNegative === false ? 1 : 0)) {
  186. prefix[caretPos - (opts.isNegative === false ? 1 : 0)] = "?";
  187. } else if (opts.suffix.length > 0 && caretPos >= maskedValue.length - opts.suffix.length - (opts.isNegative === false ? 1 : 0)) {
  188. suffix[caretPos - (maskedValue.length - opts.suffix.length - (opts.isNegative === false ? 1 : 0))] = "?";
  189. }
  190. }
  191. }
  192. prefix = prefix.join("");
  193. suffix = suffix.join("");
  194. var processValue = maskedValue.join("").replace(prefix, "");
  195. processValue = processValue.replace(suffix, "");
  196. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  197. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  198. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  199. if (isNaN(opts.placeholder)) {
  200. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.placeholder), "g"), "");
  201. }
  202. if (processValue.length > 1 && processValue.indexOf(opts.radixPoint) !== 1) {
  203. if (charAtPos === "0") {
  204. processValue = processValue.replace(/^\?/g, "");
  205. }
  206. processValue = processValue.replace(/^0/g, "");
  207. }
  208. if (processValue.charAt(0) === opts.radixPoint && opts.radixPoint !== "" && opts.numericInput !== true) {
  209. processValue = "0" + processValue;
  210. }
  211. if (processValue !== "") {
  212. processValue = processValue.split("");
  213. if ((!opts.digitsOptional || opts.enforceDigitsOnBlur && currentResult.event === "blur") && isFinite(opts.digits)) {
  214. var radixPosition = $.inArray(opts.radixPoint, processValue);
  215. var rpb = $.inArray(opts.radixPoint, maskedValue);
  216. if (radixPosition === -1) {
  217. processValue.push(opts.radixPoint);
  218. radixPosition = processValue.length - 1;
  219. }
  220. for (var i = 1; i <= opts.digits; i++) {
  221. if ((!opts.digitsOptional || opts.enforceDigitsOnBlur && currentResult.event === "blur") && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  222. processValue[radixPosition + i] = currentResult.placeholder || opts.placeholder.charAt(0);
  223. } else if (rpb !== -1 && maskedValue[rpb + i] !== undefined) {
  224. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  225. }
  226. }
  227. }
  228. if (opts.autoGroup === true && opts.groupSeparator !== "" && (charAtPos !== opts.radixPoint || currentResult.pos !== undefined || currentResult.dopost)) {
  229. var addRadix = processValue[processValue.length - 1] === opts.radixPoint && currentResult.c === opts.radixPoint;
  230. processValue = Inputmask(buildPostMask(processValue, opts), {
  231. numericInput: true,
  232. jitMasking: true,
  233. definitions: {
  234. "*": {
  235. validator: "[0-9?]",
  236. cardinality: 1
  237. }
  238. }
  239. }).format(processValue.join(""));
  240. if (addRadix) processValue += opts.radixPoint;
  241. if (processValue.charAt(0) === opts.groupSeparator) {
  242. processValue.substr(1);
  243. }
  244. } else processValue = processValue.join("");
  245. }
  246. if (opts.isNegative && currentResult.event === "blur") {
  247. opts.isNegative = processValue !== "0";
  248. }
  249. processValue = prefix + processValue;
  250. processValue += suffix;
  251. if (opts.isNegative) {
  252. processValue = opts.negationSymbol.front + processValue;
  253. processValue += opts.negationSymbol.back;
  254. }
  255. processValue = processValue.split("");
  256. if (charAtPos !== undefined) {
  257. if (charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back) {
  258. caretPos = $.inArray("?", processValue);
  259. if (caretPos > -1) {
  260. processValue[caretPos] = charAtPos;
  261. } else caretPos = currentResult.caret || 0;
  262. } else if (charAtPos === opts.radixPoint || charAtPos === opts.negationSymbol.front || charAtPos === opts.negationSymbol.back) {
  263. var newCaretPos = $.inArray(charAtPos, processValue);
  264. if (newCaretPos !== -1) caretPos = newCaretPos;
  265. }
  266. }
  267. if (opts.numericInput) {
  268. caretPos = processValue.length - caretPos - 1;
  269. processValue = processValue.reverse();
  270. }
  271. var rslt = {
  272. caret: (charAtPos === undefined || currentResult.pos !== undefined) && caretPos !== undefined ? caretPos + (opts.numericInput ? -1 : 1) : caretPos,
  273. buffer: processValue,
  274. refreshFromBuffer: currentResult.dopost || buffer.join("") !== processValue.join("")
  275. };
  276. return rslt.refreshFromBuffer ? rslt : currentResult;
  277. },
  278. onBeforeWrite: function(e, buffer, caretPos, opts) {
  279. function parseMinMaxOptions(opts) {
  280. if (opts.parseMinMaxOptions === undefined) {
  281. if (opts.min !== null) {
  282. opts.min = opts.min.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  283. if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
  284. opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN;
  285. if (isNaN(opts.min)) opts.min = Number.MIN_VALUE;
  286. }
  287. if (opts.max !== null) {
  288. opts.max = opts.max.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  289. if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
  290. opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN;
  291. if (isNaN(opts.max)) opts.max = Number.MAX_VALUE;
  292. }
  293. opts.parseMinMaxOptions = "done";
  294. }
  295. }
  296. if (e) {
  297. switch (e.type) {
  298. case "keydown":
  299. return opts.postValidation(buffer, caretPos, {
  300. caret: caretPos,
  301. dopost: true
  302. }, opts);
  303. case "blur":
  304. case "checkval":
  305. var unmasked;
  306. parseMinMaxOptions(opts);
  307. if (opts.min !== null || opts.max !== null) {
  308. unmasked = opts.onUnMask(buffer.join(""), undefined, $.extend({}, opts, {
  309. unmaskAsNumber: true
  310. }));
  311. if (opts.min !== null && unmasked < opts.min) {
  312. opts.isNegative = opts.min < 0;
  313. return opts.postValidation(opts.min.toString().replace(".", opts.radixPoint).split(""), caretPos, {
  314. caret: caretPos,
  315. dopost: true,
  316. placeholder: "0"
  317. }, opts);
  318. } else if (opts.max !== null && unmasked > opts.max) {
  319. opts.isNegative = opts.max < 0;
  320. return opts.postValidation(opts.max.toString().replace(".", opts.radixPoint).split(""), caretPos, {
  321. caret: caretPos,
  322. dopost: true,
  323. placeholder: "0"
  324. }, opts);
  325. }
  326. }
  327. return opts.postValidation(buffer, caretPos, {
  328. caret: caretPos,
  329. placeholder: "0",
  330. event: "blur"
  331. }, opts);
  332. case "_checkval":
  333. return {
  334. caret: caretPos
  335. };
  336. default:
  337. break;
  338. }
  339. }
  340. },
  341. regex: {
  342. integerPart: function(opts, emptyCheck) {
  343. return emptyCheck ? new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]?") : new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "+]?\\d+");
  344. },
  345. integerNPart: function(opts) {
  346. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + Inputmask.escapeRegex(opts.placeholder.charAt(0)) + "]+");
  347. }
  348. },
  349. definitions: {
  350. "~": {
  351. validator: function(chrs, maskset, pos, strict, opts, isSelection) {
  352. var isValid, l;
  353. if (chrs === "k" || chrs === "m") {
  354. isValid = {
  355. insert: [],
  356. c: 0
  357. };
  358. for (var i = 0, l = chrs === "k" ? 2 : 5; i < l; i++) {
  359. isValid.insert.push({
  360. pos: pos + i,
  361. c: 0
  362. });
  363. }
  364. isValid.pos = pos + l;
  365. return isValid;
  366. }
  367. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  368. if (isValid === true) {
  369. if (opts.numericInput !== true && maskset.validPositions[pos] !== undefined && maskset.validPositions[pos].match.def === "~" && !isSelection) {
  370. var processValue = maskset.buffer.join("");
  371. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  372. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  373. var pvRadixSplit = processValue.split(opts.radixPoint);
  374. if (pvRadixSplit.length > 1) {
  375. pvRadixSplit[1] = pvRadixSplit[1].replace(/0/g, opts.placeholder.charAt(0));
  376. }
  377. if (pvRadixSplit[0] === "0") {
  378. pvRadixSplit[0] = pvRadixSplit[0].replace(/0/g, opts.placeholder.charAt(0));
  379. }
  380. processValue = pvRadixSplit[0] + opts.radixPoint + pvRadixSplit[1] || "";
  381. var bufferTemplate = maskset._buffer.join("");
  382. if (processValue === opts.radixPoint) {
  383. processValue = bufferTemplate;
  384. }
  385. while (processValue.match(Inputmask.escapeRegex(bufferTemplate) + "$") === null) {
  386. bufferTemplate = bufferTemplate.slice(1);
  387. }
  388. processValue = processValue.replace(bufferTemplate, "");
  389. processValue = processValue.split("");
  390. if (processValue[pos] === undefined) {
  391. isValid = {
  392. pos: pos,
  393. remove: pos
  394. };
  395. } else {
  396. isValid = {
  397. pos: pos
  398. };
  399. }
  400. }
  401. } else if (!strict && chrs === opts.radixPoint && maskset.validPositions[pos - 1] === undefined) {
  402. isValid = {
  403. insert: {
  404. pos: pos,
  405. c: 0
  406. },
  407. pos: pos + 1
  408. };
  409. }
  410. return isValid;
  411. },
  412. cardinality: 1
  413. },
  414. "+": {
  415. validator: function(chrs, maskset, pos, strict, opts) {
  416. return opts.allowMinus && (chrs === "-" || chrs === opts.negationSymbol.front);
  417. },
  418. cardinality: 1,
  419. placeholder: ""
  420. },
  421. "-": {
  422. validator: function(chrs, maskset, pos, strict, opts) {
  423. return opts.allowMinus && chrs === opts.negationSymbol.back;
  424. },
  425. cardinality: 1,
  426. placeholder: ""
  427. },
  428. ":": {
  429. validator: function(chrs, maskset, pos, strict, opts) {
  430. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  431. var isValid = new RegExp(radix).test(chrs);
  432. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  433. isValid = {
  434. caret: pos + 1
  435. };
  436. }
  437. return isValid;
  438. },
  439. cardinality: 1,
  440. placeholder: function(opts) {
  441. return opts.radixPoint;
  442. }
  443. }
  444. },
  445. onUnMask: function(maskedValue, unmaskedValue, opts) {
  446. if (unmaskedValue === "" && opts.nullable === true) {
  447. return unmaskedValue;
  448. }
  449. var processValue = maskedValue.replace(opts.prefix, "");
  450. processValue = processValue.replace(opts.suffix, "");
  451. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  452. if (opts.placeholder.charAt(0) !== "") {
  453. processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0");
  454. }
  455. if (opts.unmaskAsNumber) {
  456. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  457. processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  458. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  459. return Number(processValue);
  460. }
  461. return processValue;
  462. },
  463. isComplete: function(buffer, opts) {
  464. var maskedValue = (opts.numericInput ? buffer.slice().reverse() : buffer).join("");
  465. maskedValue = maskedValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  466. maskedValue = maskedValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  467. maskedValue = maskedValue.replace(opts.prefix, "");
  468. maskedValue = maskedValue.replace(opts.suffix, "");
  469. maskedValue = maskedValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"), "$1");
  470. if (opts.radixPoint === ",") maskedValue = maskedValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  471. return isFinite(maskedValue);
  472. },
  473. onBeforeMask: function(initialValue, opts) {
  474. opts.isNegative = undefined;
  475. var radixPoint = opts.radixPoint || ",";
  476. if ((typeof initialValue == "number" || opts.inputType === "number") && radixPoint !== "") {
  477. initialValue = initialValue.toString().replace(".", radixPoint);
  478. }
  479. var valueParts = initialValue.split(radixPoint), integerPart = valueParts[0].replace(/[^\-0-9]/g, ""), decimalPart = valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "";
  480. initialValue = integerPart + (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
  481. var digits = 0;
  482. if (radixPoint !== "") {
  483. digits = decimalPart.length;
  484. if (decimalPart !== "") {
  485. var digitsFactor = Math.pow(10, digits || 1);
  486. if (isFinite(opts.digits)) {
  487. digits = parseInt(opts.digits);
  488. digitsFactor = Math.pow(10, digits);
  489. }
  490. initialValue = initialValue.replace(Inputmask.escapeRegex(radixPoint), ".");
  491. if (isFinite(initialValue)) initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  492. initialValue = initialValue.toString().replace(".", radixPoint);
  493. }
  494. }
  495. if (opts.digits === 0 && initialValue.indexOf(Inputmask.escapeRegex(radixPoint)) !== -1) {
  496. initialValue = initialValue.substring(0, initialValue.indexOf(Inputmask.escapeRegex(radixPoint)));
  497. }
  498. return alignDigits(initialValue.toString().split(""), digits, opts).join("");
  499. },
  500. onKeyDown: function(e, buffer, caretPos, opts) {
  501. var $input = $(this);
  502. if (e.ctrlKey) {
  503. switch (e.keyCode) {
  504. case Inputmask.keyCode.UP:
  505. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  506. $input.trigger("setvalue");
  507. break;
  508. case Inputmask.keyCode.DOWN:
  509. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  510. $input.trigger("setvalue");
  511. break;
  512. }
  513. }
  514. }
  515. },
  516. currency: {
  517. prefix: "$ ",
  518. groupSeparator: ",",
  519. alias: "numeric",
  520. placeholder: "0",
  521. autoGroup: true,
  522. digits: 2,
  523. digitsOptional: false,
  524. clearMaskOnLostFocus: false
  525. },
  526. decimal: {
  527. alias: "numeric"
  528. },
  529. integer: {
  530. alias: "numeric",
  531. digits: 0,
  532. radixPoint: ""
  533. },
  534. percentage: {
  535. alias: "numeric",
  536. digits: 2,
  537. digitsOptional: true,
  538. radixPoint: ".",
  539. placeholder: "0",
  540. autoGroup: false,
  541. min: 0,
  542. max: 100,
  543. suffix: " %",
  544. allowMinus: false
  545. }
  546. });
  547. return Inputmask;
  548. });