inputmask.numeric.extensions.js 31 KB

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