inputmask.numeric.extensions.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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, l;
  378. if (chrs === "k" || chrs === "m") {
  379. isValid = {
  380. insert: [],
  381. c: 0
  382. };
  383. for (var i = 0, l = chrs === "k" ? 2 : 5; i < l; i++) {
  384. isValid.insert.push({pos: pos + i, c: 0});
  385. }
  386. isValid.pos = pos + l;
  387. return isValid;
  388. }
  389. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  390. if (isValid === true) {
  391. if (opts.numericInput !== true && maskset.validPositions[pos] !== undefined && maskset.validPositions[pos].match.def === "~" && !isSelection) {
  392. var processValue = maskset.buffer.join("");
  393. //strip negation symbol
  394. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  395. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  396. //filter 0 after radixpoint
  397. var pvRadixSplit = processValue.split(opts.radixPoint);
  398. if (pvRadixSplit.length > 1) {
  399. pvRadixSplit[1] = pvRadixSplit[1].replace(/0/g, opts.placeholder.charAt(0));
  400. }
  401. //filter 0 before radixpoint
  402. if (pvRadixSplit[0] === "0") {
  403. pvRadixSplit[0] = pvRadixSplit[0].replace(/0/g, opts.placeholder.charAt(0));
  404. }
  405. processValue = pvRadixSplit[0] + opts.radixPoint + pvRadixSplit[1] || "";
  406. var bufferTemplate = maskset._buffer.join(""); //getBuffer().slice(lvp).join('');
  407. if (processValue === opts.radixPoint) {
  408. processValue = bufferTemplate;
  409. }
  410. while (processValue.match(Inputmask.escapeRegex(bufferTemplate) + "$") === null) {
  411. bufferTemplate = bufferTemplate.slice(1);
  412. }
  413. // if (processValue !== opts.radixPoint) {
  414. processValue = processValue.replace(bufferTemplate, "");
  415. // }
  416. processValue = processValue.split("");
  417. if (processValue[pos] === undefined) {
  418. isValid = {
  419. "pos": pos,
  420. "remove": pos
  421. };
  422. } else {
  423. isValid = {
  424. pos: pos
  425. };
  426. }
  427. }
  428. } else if (!strict && chrs === opts.radixPoint && maskset.validPositions[pos - 1] === undefined) {
  429. isValid = {
  430. insert: {
  431. pos: pos,
  432. c: 0
  433. },
  434. pos: pos + 1
  435. }
  436. }
  437. return isValid;
  438. },
  439. cardinality: 1
  440. },
  441. "+": {
  442. validator: function (chrs, maskset, pos, strict, opts) {
  443. return (opts.allowMinus && (chrs === "-" || chrs === opts.negationSymbol.front));
  444. },
  445. cardinality: 1,
  446. placeholder: ""
  447. },
  448. "-": {
  449. validator: function (chrs, maskset, pos, strict, opts) {
  450. return (opts.allowMinus && chrs === opts.negationSymbol.back);
  451. },
  452. cardinality: 1,
  453. placeholder: ""
  454. },
  455. ":": {
  456. validator: function (chrs, maskset, pos, strict, opts) {
  457. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  458. var isValid = new RegExp(radix).test(chrs);
  459. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  460. isValid = {
  461. "caret": pos + 1
  462. };
  463. }
  464. return isValid;
  465. },
  466. cardinality: 1,
  467. placeholder: function (opts) {
  468. return opts.radixPoint;
  469. }
  470. }
  471. },
  472. onUnMask: function (maskedValue, unmaskedValue, opts) {
  473. if (unmaskedValue === "" && opts.nullable === true) {
  474. return unmaskedValue;
  475. }
  476. var processValue = maskedValue.replace(opts.prefix, "");
  477. processValue = processValue.replace(opts.suffix, "");
  478. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  479. if (opts.placeholder.charAt(0) !== "") {
  480. processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0");
  481. }
  482. if (opts.unmaskAsNumber) {
  483. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  484. processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  485. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  486. return Number(processValue);
  487. }
  488. return processValue;
  489. },
  490. isComplete: function (buffer, opts) {
  491. var maskedValue = buffer.join(""),
  492. bufClone = buffer.slice();
  493. //verify separator positions
  494. if (bufClone.join("") !== maskedValue) return false;
  495. var processValue = maskedValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  496. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  497. processValue = processValue.replace(opts.prefix, "");
  498. processValue = processValue.replace(opts.suffix, "");
  499. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"), "$1");
  500. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  501. return isFinite(processValue);
  502. },
  503. onBeforeMask: function (initialValue, opts) {
  504. opts.isNegative = undefined;
  505. if (typeof initialValue == "number" && opts.radixPoint !== "") {
  506. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  507. }
  508. initialValue = initialValue.toString().charAt(initialValue.length - 1) === opts.radixPoint ?
  509. initialValue.toString().substr(0, initialValue.length - 1) : initialValue.toString();
  510. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  511. var vs = initialValue.split("."),
  512. groupSize = opts.groupSeparator !== "" ? parseInt(opts.groupSize) : 0;
  513. if (vs.length === 2 && (vs[0].length > groupSize || vs[1].length > groupSize || (vs[0].length <= groupSize && vs[1].length < groupSize))) {
  514. initialValue = initialValue.replace(".", opts.radixPoint);
  515. }
  516. }
  517. var kommaMatches = initialValue.match(/,/g);
  518. var dotMatches = initialValue.match(/\./g);
  519. if (dotMatches && kommaMatches) {
  520. if (dotMatches.length > kommaMatches.length) {
  521. initialValue = initialValue.replace(/\./g, "");
  522. initialValue = initialValue.replace(",", opts.radixPoint);
  523. } else if (kommaMatches.length > dotMatches.length) {
  524. initialValue = initialValue.replace(/,/g, "");
  525. initialValue = initialValue.replace(".", opts.radixPoint);
  526. } else { //equal
  527. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue.replace(/,/g, "");
  528. }
  529. } else {
  530. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  531. }
  532. if (opts.digits === 0) {
  533. if (initialValue.indexOf(".") !== -1) {
  534. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  535. } else if (initialValue.indexOf(",") !== -1) {
  536. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  537. }
  538. }
  539. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  540. var valueParts = initialValue.split(opts.radixPoint),
  541. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  542. if (parseInt(opts.digits) < decPart.toString().length) {
  543. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  544. //make the initialValue a valid javascript number for the parsefloat
  545. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  546. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  547. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  548. }
  549. }
  550. // if (opts.numericInput === true) {
  551. // initialValue = initialValue.split("").reverse().join("");
  552. // }
  553. return initialValue;
  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. }));