inputmask.numeric.extensions.js 23 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. 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) {
  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 (opts.numericInput === true) {
  380. if (maskset.buffer[maskset.buffer.length - opts.prefix.length - 1] === "0") {
  381. return {
  382. "pos": pos,
  383. "remove": maskset.buffer.length - opts.prefix.length - 1
  384. };
  385. }
  386. } else {
  387. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer),
  388. matchRslt = maskset.buffer.slice(0, radixPosition !== -1 ? radixPosition : undefined).join("").match(opts.regex.integerNPart(opts));
  389. if (matchRslt && !strict && (radixPosition === -1 || pos <= radixPosition)) {
  390. if (matchRslt["0"].indexOf(opts.placeholder !== "" ? opts.placeholder.charAt(0) : "0") === 0 && matchRslt.index + 1 === pos) {
  391. maskset.buffer.splice(matchRslt.index, 1);
  392. pos = matchRslt.index;
  393. return {
  394. "pos": pos,
  395. "remove": matchRslt.index
  396. };
  397. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  398. return false;
  399. }
  400. }
  401. }
  402. return true;
  403. },
  404. definitions: {
  405. "~": {
  406. validator: function (chrs, maskset, pos, strict, opts) {
  407. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  408. if (!isValid) {
  409. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  410. if (!isValid) {
  411. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  412. if (isValid === true) {
  413. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  414. if (isValid === true) {
  415. //handle overwrite when fixed precision
  416. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  417. if (radixPosition !== -1 && (opts.digitsOptional === false || maskset.validPositions[pos]) && opts.numericInput !== true && pos > radixPosition && !strict) {
  418. isValid = {
  419. "pos": pos,
  420. "remove": pos
  421. };
  422. } else {
  423. isValid = {
  424. pos: pos
  425. };
  426. }
  427. }
  428. }
  429. }
  430. }
  431. return isValid;
  432. },
  433. cardinality: 1,
  434. prevalidator: null
  435. },
  436. "+": {
  437. validator: function (chrs, maskset, pos, strict, opts) {
  438. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  439. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  440. if (chrs === "-") {
  441. if (opts.negationSymbol.back !== "") {
  442. isValid = {
  443. "pos": pos,
  444. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  445. "caret": pos + 1,
  446. "insert": {
  447. "pos": maskset.buffer.length,
  448. "c": opts.negationSymbol.back
  449. }
  450. };
  451. } else {
  452. isValid = {
  453. "pos": pos,
  454. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  455. "caret": pos + 1
  456. };
  457. }
  458. } else {
  459. isValid = true;
  460. }
  461. }
  462. return isValid;
  463. },
  464. cardinality: 1,
  465. prevalidator: null,
  466. placeholder: ""
  467. },
  468. "-": {
  469. validator: function (chrs, maskset, pos, strict, opts) {
  470. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  471. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  472. isValid = true;
  473. }
  474. return isValid;
  475. },
  476. cardinality: 1,
  477. prevalidator: null,
  478. placeholder: ""
  479. },
  480. ":": {
  481. validator: function (chrs, maskset, pos, strict, opts) {
  482. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  483. if (!isValid) {
  484. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + ",\\." + "]";
  485. isValid = new RegExp(radix).test(chrs);
  486. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  487. isValid = {
  488. "caret": pos + 1
  489. };
  490. }
  491. }
  492. return isValid ? {
  493. c: opts.radixPoint
  494. } : isValid;
  495. },
  496. cardinality: 1,
  497. prevalidator: null,
  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. }));