inputmask.numeric.extensions.js 23 KB

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