inputmask.numeric.extensions.js 33 KB

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