inputmask.numeric.extensions.js 24 KB

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