inputmask.numeric.extensions.js 32 KB

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