inputmask.numeric.extensions.js 32 KB

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