inputmask.numeric.extensions.js 33 KB

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