inputmask.numeric.extensions.js 23 KB

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