inputmask.numeric.extensions.js 33 KB

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