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 (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) {
  209. processValue = "0" + processValue;
  210. }
  211. if (processValue !== "") {
  212. processValue = processValue.split("");
  213. //handle digits
  214. if (!opts.digitsOptional && isFinite(opts.digits)) {
  215. var radixPosition = $.inArray(opts.radixPoint, processValue);
  216. var rpb = $.inArray(opts.radixPoint, maskedValue);
  217. if (radixPosition === -1) {
  218. processValue.push(opts.radixPoint);
  219. radixPosition = processValue.length - 1;
  220. }
  221. for (var i = 1; i <= opts.digits; i++) {
  222. if (!opts.digitsOptional && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  223. processValue[radixPosition + i] = currentResult.placeholder || opts.placeholder.charAt(0);
  224. } else if (rpb !== -1 && maskedValue[rpb + i] !== undefined) {
  225. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  226. }
  227. }
  228. }
  229. if (opts.autoGroup === true && opts.groupSeparator !== "" && (charAtPos !== opts.radixPoint || currentResult.pos !== undefined || currentResult.dopost)) {
  230. processValue = Inputmask(buildPostMask(processValue, opts), {
  231. numericInput: true, jitMasking: true,
  232. definitions: {
  233. "*": {
  234. validator: "[0-9?]",
  235. cardinality: 1
  236. }
  237. }
  238. }).format(processValue.join(""));
  239. if (processValue.charAt(0) === opts.groupSeparator) {
  240. processValue.substr(1);
  241. }
  242. } else processValue = processValue.join("");
  243. }
  244. if (opts.isNegative && currentResult.event === "blur") {
  245. opts.isNegative = processValue !== "0"
  246. }
  247. processValue = prefix + processValue;
  248. processValue += suffix;
  249. if (opts.isNegative) {
  250. processValue = opts.negationSymbol.front + processValue;
  251. processValue += opts.negationSymbol.back;
  252. }
  253. processValue = processValue.split("");
  254. //unmark position
  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 ||
  262. charAtPos === opts.negationSymbol.front ||
  263. charAtPos === opts.negationSymbol.back) {
  264. var newCaretPos = $.inArray(charAtPos, processValue);
  265. if (newCaretPos !== -1) caretPos = newCaretPos;
  266. // else charAtPos = undefined;
  267. }
  268. }
  269. if (opts.numericInput) {
  270. caretPos = processValue.length - caretPos - 1;
  271. processValue = processValue.reverse();
  272. }
  273. var rslt = {
  274. caret: (charAtPos === undefined || currentResult.pos !== undefined) ? caretPos + (opts.numericInput ? -1 : 1) : caretPos,
  275. buffer: processValue,
  276. refreshFromBuffer: currentResult.dopost || buffer.join("") !== processValue.join("")
  277. };
  278. // console.log(JSON.stringify(rslt));
  279. return rslt.refreshFromBuffer ? rslt : currentResult;
  280. },
  281. onBeforeWrite: function (e, buffer, caretPos, opts) {
  282. function parseMinMaxOptions(opts) {
  283. if (opts.parseMinMaxOptions === undefined) {
  284. //convert min and max options
  285. if (opts.min !== null) {
  286. opts.min = opts.min.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  287. if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
  288. opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN;
  289. if (isNaN(opts.min)) opts.min = Number.MIN_VALUE;
  290. }
  291. if (opts.max !== null) {
  292. opts.max = opts.max.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  293. if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
  294. opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN;
  295. if (isNaN(opts.max)) opts.max = Number.MAX_VALUE;
  296. }
  297. opts.parseMinMaxOptions = "done";
  298. // console.log(opts.min + " " + opts.max);
  299. }
  300. }
  301. if (e) {
  302. switch (e.type) {
  303. case "keydown":
  304. return opts.postValidation(buffer, {caret: caretPos, dopost: true}, opts);
  305. case "blur":
  306. case "checkval":
  307. var unmasked;
  308. parseMinMaxOptions(opts);
  309. if (opts.min !== null || opts.max !== null) {
  310. unmasked = opts.onUnMask(buffer.join(""), undefined, $.extend({}, opts, {unmaskAsNumber: true}));
  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(""), { //TODO needs fix for MIN_VALUE & MAX_VALUE
  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(""), { //TODO needs fix for MIN_VALUE & MAX_VALUE
  321. caret: caretPos,
  322. dopost: true,
  323. placeholder: "0"
  324. }, opts);
  325. }
  326. }
  327. return opts.postValidation(buffer, {
  328. caret: caretPos,
  329. dopost: true,
  330. placeholder: "0",
  331. event: "blur"
  332. }, opts);
  333. case "_checkval":
  334. return {caret: caretPos};
  335. default:
  336. //strip radixpoint at the end
  337. // if (buffer[buffer.length - 1] === opts.radixPoint) {
  338. // delete buffer[buffer.length - 1];
  339. // return opts.postValidation(buffer, {
  340. // caret: caretPos,
  341. // dopost: true,
  342. // placeholder: "0"
  343. // }, opts);
  344. // }
  345. break;
  346. }
  347. }
  348. },
  349. regex: {
  350. integerPart: function (opts, emptyCheck) {
  351. return emptyCheck ? new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?") : new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?\\d+");
  352. }
  353. ,
  354. integerNPart: function (opts) {
  355. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + Inputmask.escapeRegex(opts.placeholder.charAt(0)) + "]+");
  356. }
  357. }
  358. ,
  359. definitions: {
  360. "~": {
  361. validator: function (chrs, maskset, pos, strict, opts, isSelection) {
  362. var isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  363. if (isValid === true) {
  364. if (opts.numericInput !== true && maskset.validPositions[pos] !== undefined && maskset.validPositions[pos].match.def === "~" && !isSelection) {
  365. var processValue = maskset.buffer.join("");
  366. //strip negation symbol
  367. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  368. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  369. //filter 0
  370. processValue = processValue.replace(/0/g, opts.placeholder.charAt(0));
  371. var bufferTemplate = maskset._buffer.join(""); //getBuffer().slice(lvp).join('');
  372. if (processValue === opts.radixPoint) {
  373. processValue = bufferTemplate;
  374. }
  375. while (processValue.match(Inputmask.escapeRegex(bufferTemplate) + "$") === null) {
  376. bufferTemplate = bufferTemplate.slice(1);
  377. }
  378. processValue = processValue.replace(bufferTemplate, "");
  379. processValue = processValue.split("");
  380. if (processValue[pos] === undefined) {
  381. isValid = {
  382. "pos": pos,
  383. "remove": pos
  384. };
  385. } else {
  386. isValid = {
  387. pos: pos
  388. };
  389. }
  390. }
  391. } else if (!strict && chrs === opts.radixPoint && maskset.validPositions[pos - 1] === undefined) {
  392. maskset.buffer[pos] = "0";
  393. isValid = {
  394. "pos": pos + 1
  395. }
  396. }
  397. return isValid;
  398. },
  399. cardinality: 1
  400. },
  401. "+": {
  402. validator: function (chrs, maskset, pos, strict, opts) {
  403. return (opts.allowMinus && (chrs === "-" || chrs === opts.negationSymbol.front));
  404. },
  405. cardinality: 1,
  406. placeholder: ""
  407. },
  408. "-": {
  409. validator: function (chrs, maskset, pos, strict, opts) {
  410. return (opts.allowMinus && chrs === opts.negationSymbol.back);
  411. },
  412. cardinality: 1,
  413. placeholder: ""
  414. },
  415. ":": {
  416. validator: function (chrs, maskset, pos, strict, opts) {
  417. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  418. var isValid = new RegExp(radix).test(chrs);
  419. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  420. isValid = {
  421. "caret": pos + 1
  422. };
  423. }
  424. return isValid;
  425. },
  426. cardinality: 1,
  427. placeholder: function (opts) {
  428. return opts.radixPoint;
  429. }
  430. }
  431. },
  432. onUnMask: function (maskedValue, unmaskedValue, opts) {
  433. if (unmaskedValue === "" && opts.nullable === true) {
  434. return unmaskedValue;
  435. }
  436. var processValue = maskedValue.replace(opts.prefix, "");
  437. processValue = processValue.replace(opts.suffix, "");
  438. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  439. if (opts.placeholder.charAt(0) !== "")
  440. processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0");
  441. if (opts.unmaskAsNumber) {
  442. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  443. return Number(processValue);
  444. }
  445. return processValue;
  446. }
  447. ,
  448. isComplete: function (buffer, opts) {
  449. var maskedValue = buffer.join(""),
  450. bufClone = buffer.slice();
  451. //verify separator positions
  452. if (bufClone.join("") !== maskedValue) return false;
  453. var processValue = maskedValue.replace(opts.prefix, "");
  454. processValue = processValue.replace(opts.suffix, "");
  455. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  456. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  457. return isFinite(processValue);
  458. },
  459. onBeforeMask: function (initialValue, opts) {
  460. opts.isNegative = undefined;
  461. initialValue = initialValue.toString().charAt(initialValue.length - 1) === opts.radixPoint
  462. ? initialValue.toString().substr(0, initialValue.length - 1) : initialValue.toString();
  463. // if (opts.numericInput === true) {
  464. // initialValue = initialValue.split("").reverse().join("");
  465. // }
  466. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  467. var vs = initialValue.split("."),
  468. groupSize = opts.groupSeparator !== "" ? parseInt(opts.groupSize) : 0;
  469. if (vs.length === 2 && (vs[0].length > groupSize || vs[1].length > groupSize || (vs[0].length <= groupSize && vs[1].length < groupSize)))
  470. initialValue = initialValue.replace(".", opts.radixPoint);
  471. }
  472. var kommaMatches = initialValue.match(/,/g);
  473. var dotMatches = initialValue.match(/\./g);
  474. if (dotMatches && kommaMatches) {
  475. if (dotMatches.length > kommaMatches.length) {
  476. initialValue = initialValue.replace(/\./g, "");
  477. initialValue = initialValue.replace(",", opts.radixPoint);
  478. } else if (kommaMatches.length > dotMatches.length) {
  479. initialValue = initialValue.replace(/,/g, "");
  480. initialValue = initialValue.replace(".", opts.radixPoint);
  481. } else { //equal
  482. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  483. }
  484. } else {
  485. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  486. }
  487. if (opts.digits === 0) {
  488. if (initialValue.indexOf(".") !== -1) {
  489. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  490. } else if (initialValue.indexOf(",") !== -1) {
  491. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  492. }
  493. }
  494. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  495. var valueParts = initialValue.split(opts.radixPoint),
  496. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  497. if (parseInt(opts.digits) < decPart.toString().length) {
  498. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  499. //make the initialValue a valid javascript number for the parsefloat
  500. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  501. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  502. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  503. }
  504. }
  505. // if (opts.numericInput === true) {
  506. // initialValue = initialValue.split("").reverse().join("");
  507. // }
  508. return initialValue;
  509. }
  510. ,
  511. canClearPosition: function (maskset, position, lvp, strict, opts) {
  512. var vp = maskset.validPositions[position],
  513. canClear =
  514. vp.input !== opts.radixPoint ||
  515. (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false) ||
  516. (vp.input === opts.radixPoint && maskset.validPositions[position + 1] && maskset.validPositions[position + 1].match.fn === null) ||
  517. isFinite(vp.input) ||
  518. position === lvp ||
  519. vp.input === opts.groupSeparator ||
  520. vp.input === opts.negationSymbol.front ||
  521. vp.input === opts.negationSymbol.back;
  522. if (canClear && (vp.match.nativeDef == "+" || vp.match.nativeDef == "-")) {
  523. opts.isNegative = false;
  524. }
  525. return canClear;
  526. }
  527. ,
  528. onKeyDown: function (e, buffer, caretPos, opts) {
  529. //TODO FIXME
  530. var $input = $(this);
  531. if (e.ctrlKey) {
  532. switch (e.keyCode) {
  533. case Inputmask.keyCode.UP:
  534. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  535. $input.trigger("setvalue");
  536. break;
  537. case Inputmask.keyCode.DOWN:
  538. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  539. $input.trigger("setvalue");
  540. break;
  541. }
  542. }
  543. }
  544. },
  545. "currency": {
  546. prefix: "$ ",
  547. groupSeparator: ",",
  548. alias: "numeric",
  549. placeholder: "0",
  550. autoGroup: true,
  551. digits: 2,
  552. digitsOptional: false,
  553. clearMaskOnLostFocus: false
  554. }
  555. ,
  556. "decimal": {
  557. alias: "numeric"
  558. }
  559. ,
  560. "integer": {
  561. alias: "numeric",
  562. digits: 0,
  563. radixPoint: ""
  564. }
  565. ,
  566. "percentage": {
  567. alias: "numeric",
  568. digits: 2,
  569. digitsOptional: true,
  570. radixPoint: ".",
  571. placeholder: "0",
  572. autoGroup: false,
  573. min: 0,
  574. max: 100,
  575. suffix: " %",
  576. allowMinus: false
  577. }
  578. })
  579. ;
  580. return Inputmask;
  581. }));