inputmask.numeric.extensions.js 23 KB

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