inputmask.numeric.extensions.js 23 KB

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