inputmask.numeric.extensions.js 23 KB

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