inputmask.numeric.extensions.js 24 KB

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