inputmask.numeric.extensions.js 33 KB

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