inputmask.numeric.extensions.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. escapedTxt += opts.definitions[txt.charAt(i)] ? "\\" + txt.charAt(i) : txt.charAt(i);
  27. }
  28. return escapedTxt;
  29. }
  30. if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
  31. opts.integerDigits = opts.repeat;
  32. }
  33. opts.repeat = 0;
  34. if (opts.groupSeparator === opts.radixPoint) { //treat equal separator and radixpoint
  35. if (opts.radixPoint === ".") {
  36. opts.groupSeparator = ",";
  37. } else if (opts.radixPoint === ",") {
  38. opts.groupSeparator = ".";
  39. } else opts.groupSeparator = "";
  40. }
  41. if (opts.groupSeparator === " ") { //prevent conflict with default skipOptionalPartCharacter
  42. opts.skipOptionalPartCharacter = undefined;
  43. }
  44. opts.autoGroup = opts.autoGroup && opts.groupSeparator !== "";
  45. if (opts.autoGroup) {
  46. if (typeof opts.groupSize == "string" && isFinite(opts.groupSize)) opts.groupSize = parseInt(opts.groupSize);
  47. if (isFinite(opts.integerDigits)) {
  48. var seps = Math.floor(opts.integerDigits / opts.groupSize);
  49. var mod = opts.integerDigits % opts.groupSize;
  50. opts.integerDigits = parseInt(opts.integerDigits) + (mod === 0 ? seps - 1 : seps);
  51. if (opts.integerDigits < 1) {
  52. opts.integerDigits = "*";
  53. }
  54. }
  55. }
  56. //enforce placeholder to single
  57. if (opts.placeholder.length > 1) {
  58. opts.placeholder = opts.placeholder.charAt(0);
  59. }
  60. //only allow radixfocus when placeholder = 0
  61. opts.radixFocus = opts.radixFocus && opts.placeholder !== "" && opts.integerOptional === true;
  62. opts.definitions[";"] = opts.definitions["~"]; //clone integer def for decimals
  63. opts.definitions[";"].definitionSymbol = "~";
  64. if (opts.numericInput === true) { //finance people input style
  65. opts.radixFocus = false;
  66. opts.digitsOptional = false;
  67. if (isNaN(opts.digits)) opts.digits = 2;
  68. opts.decimalProtect = false;
  69. }
  70. var mask = autoEscape(opts.prefix);
  71. mask += "[+]";
  72. if (opts.integerOptional === true) {
  73. mask += "~{1," + opts.integerDigits + "}";
  74. } else mask += "~{" + opts.integerDigits + "}";
  75. if (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  76. if (opts.decimalProtect) opts.radixPointDefinitionSymbol = ":";
  77. if (opts.digitsOptional) {
  78. mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{1," + opts.digits + "}]";
  79. } else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}";
  80. }
  81. if (opts.negationSymbol.back !== "") {
  82. mask += "[-]";
  83. }
  84. mask += autoEscape(opts.suffix);
  85. opts.greedy = false; //enforce greedy false
  86. return mask;
  87. },
  88. placeholder: "",
  89. greedy: false,
  90. digits: "*", //number of fractionalDigits
  91. digitsOptional: true,
  92. radixPoint: ".",
  93. radixFocus: true,
  94. groupSize: 3,
  95. groupSeparator: "",
  96. autoGroup: false,
  97. allowPlus: true,
  98. allowMinus: true,
  99. negationSymbol: {
  100. front: "-", //"("
  101. back: "" //")"
  102. },
  103. integerDigits: "+", //number of integerDigits
  104. integerOptional: true,
  105. prefix: "",
  106. suffix: "",
  107. rightAlign: true,
  108. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  109. min: null, //minimum value
  110. max: null, //maximum value
  111. step: 1,
  112. insertMode: true,
  113. autoUnmask: false,
  114. unmaskAsNumber: false,
  115. postFormat: function (buffer, pos, opts) { //this needs to be removed // this is crap
  116. // console.log(buffer);
  117. if (opts.numericInput === true) {
  118. buffer = buffer.reverse();
  119. if (isFinite(pos)) {
  120. pos = buffer.join("").length - pos - 1;
  121. }
  122. }
  123. var suffixStripped = false,
  124. i, l;
  125. if (buffer.length >= opts.suffix.length && buffer.join("").indexOf(opts.suffix) === (buffer.length - opts.suffix.length)) {
  126. buffer.length = buffer.length - opts.suffix.length; //strip suffix
  127. suffixStripped = true;
  128. }
  129. //position overflow corrections
  130. pos = pos >= buffer.length ? buffer.length - 1 : (pos < opts.prefix.length ? opts.prefix.length : pos);
  131. var needsRefresh = false,
  132. charAtPos = buffer[pos];
  133. var cbuf = buffer.slice();
  134. if (charAtPos === opts.groupSeparator) {
  135. cbuf.splice(pos--, 1);
  136. charAtPos = cbuf[pos];
  137. }
  138. if (charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back) cbuf[pos] = "?";
  139. var bufVal = cbuf.join(""),
  140. bufValOrigin = bufVal;
  141. if (bufVal.length > 0 && opts.autoGroup || bufVal.indexOf(opts.groupSeparator) !== -1) {
  142. var escapedGroupSeparator = Inputmask.escapeRegex(opts.groupSeparator);
  143. needsRefresh = bufVal.indexOf(opts.groupSeparator) === 0;
  144. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  145. var radixSplit = bufVal.split(opts.radixPoint);
  146. bufVal = opts.radixPoint === "" ? bufVal : radixSplit[0];
  147. if (bufVal !== (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
  148. //needsRefresh = true;
  149. var reg = new RegExp("([-\+]?[\\d\?]+)([\\d\?]{" + opts.groupSize + "})");
  150. while (reg.test(bufVal) && opts.groupSeparator !== "") {
  151. bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2");
  152. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  153. }
  154. }
  155. if (opts.radixPoint !== "" && radixSplit.length > 1) {
  156. bufVal += opts.radixPoint + radixSplit[1];
  157. }
  158. }
  159. needsRefresh = bufValOrigin !== bufVal;
  160. buffer.length = bufVal.length; //align the length
  161. for (i = 0, l = bufVal.length; i < l; i++) {
  162. buffer[i] = bufVal.charAt(i);
  163. }
  164. var newPos = $.inArray("?", buffer);
  165. if (newPos === -1) newPos = $.inArray(charAtPos, buffer);
  166. buffer[newPos] = charAtPos;
  167. if (!needsRefresh && suffixStripped) {
  168. for (i = 0, l = opts.suffix.length; i < l; i++) {
  169. buffer.push(opts.suffix.charAt(i));
  170. }
  171. }
  172. // console.log("formatted " + buffer + " refresh " + needsRefresh);
  173. newPos = (opts.numericInput && isFinite(pos)) ? buffer.join("").length - newPos - 1 : newPos;
  174. if (opts.numericInput) {
  175. buffer = buffer.reverse();
  176. if ($.inArray(opts.radixPoint, buffer) < newPos && (buffer.join("").length - opts.suffix.length) !== newPos) {
  177. newPos = newPos - 1;
  178. }
  179. }
  180. return {
  181. pos: newPos,
  182. "refreshFromBuffer": needsRefresh,
  183. "buffer": buffer
  184. };
  185. },
  186. onBeforeWrite: function (e, buffer, caretPos, opts) {
  187. var rslt;
  188. if (e && (e.type === "blur" || e.type === "checkval" || e.type === "keydown")) {
  189. var maskedValue = opts.numericInput ? buffer.slice().reverse().join("") : buffer.join(""),
  190. processValue = maskedValue.replace(opts.prefix, ""),
  191. minmaxed = false;
  192. processValue = processValue.replace(opts.suffix, "");
  193. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  194. if (opts.radixPoint === ",") processValue = processValue.replace(opts.radixPoint, ".");
  195. //handle negation symbol
  196. var isNegative = processValue.match(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"));
  197. isNegative = isNegative !== null && isNegative.length === 1;
  198. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  199. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  200. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  201. if (processValue !== "" && isFinite(processValue)) {
  202. var floatValue = parseFloat(processValue),
  203. signedFloatValue = isNegative ? floatValue * -1 : floatValue;
  204. if (opts.min !== null && isFinite(opts.min)) {
  205. if (signedFloatValue < parseFloat(opts.min)) {
  206. floatValue = Math.abs(opts.min);
  207. isNegative = opts.min < 0;
  208. minmaxed = true;
  209. }
  210. }
  211. if (!minmaxed && opts.max !== null && isFinite(opts.max)) {
  212. if (signedFloatValue > parseFloat(opts.max)) {
  213. floatValue = Math.abs(opts.max);
  214. isNegative = opts.max < 0;
  215. minmaxed = true;
  216. }
  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. prevalidator: null
  439. },
  440. "+": {
  441. validator: function (chrs, maskset, pos, strict, opts) {
  442. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  443. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  444. if (!strict && chrs === "-") {
  445. if (opts.negationSymbol.back !== "") {
  446. isValid = {
  447. "pos": pos,
  448. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  449. "caret": pos + 1,
  450. "insert": {
  451. "pos": maskset.buffer.length,
  452. "c": opts.negationSymbol.back
  453. }
  454. };
  455. } else {
  456. isValid = {
  457. "pos": pos,
  458. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  459. "caret": pos + 1
  460. };
  461. }
  462. } else {
  463. isValid = true;
  464. }
  465. }
  466. return isValid;
  467. },
  468. cardinality: 1,
  469. prevalidator: null,
  470. placeholder: ""
  471. },
  472. "-": {
  473. validator: function (chrs, maskset, pos, strict, opts) {
  474. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  475. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  476. isValid = true;
  477. }
  478. return isValid;
  479. },
  480. cardinality: 1,
  481. prevalidator: null,
  482. placeholder: ""
  483. },
  484. ":": {
  485. validator: function (chrs, maskset, pos, strict, opts) {
  486. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  487. if (!isValid) {
  488. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + ",\\." + "]";
  489. isValid = new RegExp(radix).test(chrs);
  490. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  491. isValid = {
  492. "caret": pos + 1
  493. };
  494. }
  495. }
  496. return isValid ? {
  497. c: opts.radixPoint
  498. } : isValid;
  499. },
  500. cardinality: 1,
  501. prevalidator: null,
  502. placeholder: function (opts) {
  503. return opts.radixPoint;
  504. }
  505. }
  506. },
  507. onUnMask: function (maskedValue, unmaskedValue, opts) {
  508. var processValue = maskedValue.replace(opts.prefix, "");
  509. processValue = processValue.replace(opts.suffix, "");
  510. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  511. if (opts.unmaskAsNumber) {
  512. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  513. return Number(processValue);
  514. }
  515. return processValue;
  516. },
  517. isComplete: function (buffer, opts) {
  518. var maskedValue = buffer.join(""),
  519. bufClone = buffer.slice();
  520. //verify separator positions
  521. opts.postFormat(bufClone, 0, opts);
  522. if (bufClone.join("") !== maskedValue) return false;
  523. var processValue = maskedValue.replace(opts.prefix, "");
  524. processValue = processValue.replace(opts.suffix, "");
  525. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  526. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  527. return isFinite(processValue);
  528. },
  529. onBeforeMask: function (initialValue, opts) {
  530. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  531. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  532. } else {
  533. var kommaMatches = initialValue.match(/,/g);
  534. var dotMatches = initialValue.match(/\./g);
  535. if (dotMatches && kommaMatches) {
  536. if (dotMatches.length > kommaMatches.length) {
  537. initialValue = initialValue.replace(/\./g, "");
  538. initialValue = initialValue.replace(",", opts.radixPoint);
  539. } else if (kommaMatches.length > dotMatches.length) {
  540. initialValue = initialValue.replace(/,/g, "");
  541. initialValue = initialValue.replace(".", opts.radixPoint);
  542. } else { //equal
  543. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  544. }
  545. } else {
  546. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  547. }
  548. }
  549. if (opts.digits === 0) {
  550. if (initialValue.indexOf(".") !== -1) {
  551. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  552. } else if (initialValue.indexOf(",") !== -1) {
  553. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  554. }
  555. }
  556. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  557. var valueParts = initialValue.split(opts.radixPoint),
  558. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  559. if (parseInt(opts.digits) < decPart.toString().length) {
  560. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  561. //make the initialValue a valid javascript number for the parsefloat
  562. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  563. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  564. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  565. }
  566. }
  567. return initialValue.toString();
  568. },
  569. canClearPosition: function (maskset, position, lvp, strict, opts) {
  570. var positionInput = maskset.validPositions[position].input,
  571. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  572. position === lvp ||
  573. positionInput === opts.groupSeparator ||
  574. positionInput === opts.negationSymbol.front ||
  575. positionInput === opts.negationSymbol.back;
  576. return canClear;
  577. },
  578. onKeyDown: function (e, buffer, caretPos, opts) {
  579. var $input = $(this);
  580. if (e.ctrlKey) {
  581. switch (e.keyCode) {
  582. case Inputmask.keyCode.UP:
  583. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  584. $input.trigger("setvalue");
  585. break;
  586. case Inputmask.keyCode.DOWN:
  587. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  588. $input.trigger("setvalue");
  589. break;
  590. }
  591. }
  592. }
  593. },
  594. "currency": {
  595. prefix: "$ ",
  596. groupSeparator: ",",
  597. alias: "numeric",
  598. placeholder: "0",
  599. autoGroup: true,
  600. digits: 2,
  601. digitsOptional: false,
  602. clearMaskOnLostFocus: false
  603. },
  604. "decimal": {
  605. alias: "numeric"
  606. },
  607. "integer": {
  608. alias: "numeric",
  609. digits: 0,
  610. radixPoint: ""
  611. },
  612. "percentage": {
  613. alias: "numeric",
  614. digits: 2,
  615. radixPoint: ".",
  616. placeholder: "0",
  617. autoGroup: false,
  618. min: 0,
  619. max: 100,
  620. suffix: " %",
  621. allowPlus: false,
  622. allowMinus: false
  623. }
  624. });
  625. return Inputmask;
  626. }));