inputmask.numeric.extensions.js 33 KB

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