inputmask.numeric.extensions.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. if (e && (e.type === "blur" || e.type === "checkval" || e.type === "keydown")) {
  188. var maskedValue = opts.numericInput ? buffer.slice().reverse().join("") : buffer.join(""),
  189. processValue = maskedValue.replace(opts.prefix, ""),
  190. minmaxed = false;
  191. processValue = processValue.replace(opts.suffix, "");
  192. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  193. if (opts.radixPoint === ",") processValue = processValue.replace(opts.radixPoint, ".");
  194. //handle negation symbol
  195. var isNegative = processValue.match(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g")),
  196. isNegative = isNegative !== null && isNegative.length === 1;
  197. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  198. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  199. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  200. if (isFinite(processValue)) {
  201. var floatValue = parseFloat(processValue),
  202. signedFloatValue = isNegative ? floatValue * -1 : floatValue;
  203. if (opts.min !== null && isFinite(opts.min)) {
  204. if (signedFloatValue < parseFloat(opts.min)) {
  205. floatValue = Math.abs(opts.min);
  206. isNegative = opts.min < 0;
  207. minmaxed = true;
  208. }
  209. }
  210. if (!minmaxed && opts.max !== null && isFinite(opts.max)) {
  211. if (signedFloatValue > parseFloat(opts.max)) {
  212. floatValue = Math.abs(opts.max);
  213. isNegative = opts.max < 0;
  214. minmaxed = true;
  215. }
  216. }
  217. processValue = floatValue.toString().replace(".", opts.radixPoint).split('');
  218. if (isFinite(opts.digits)) {
  219. var radixPosition = $.inArray(opts.radixPoint, processValue);
  220. var rpb = $.inArray(opts.radixPoint, maskedValue);
  221. if (radixPosition === -1) {
  222. processValue.push(opts.radixPoint);
  223. radixPosition = processValue.length - 1;
  224. }
  225. for (var i = 1; i <= opts.digits; i++) {
  226. if (!opts.digitsOptional && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  227. processValue[radixPosition + i] = "0";
  228. } else if (rpb != -1 && maskedValue[rpb + i] != undefined) {
  229. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  230. }
  231. }
  232. if (processValue[processValue.length - 1] == opts.radixPoint) {
  233. delete processValue[processValue.length - 1];
  234. }
  235. }
  236. if ((floatValue.toString() !== processValue && floatValue.toString() + "." !== processValue) || isNegative) {
  237. if (isNegative && (floatValue !== 0 || e.type !== "blur")) {
  238. processValue.unshift(opts.negationSymbol.front);
  239. processValue.push(opts.negationSymbol.back);
  240. }
  241. processValue = (opts.prefix + processValue.join('')).split("");
  242. if (opts.numericInput) processValue = processValue.reverse();
  243. var rslt = opts.postFormat(processValue, opts.numericInput ? caretPos : caretPos - 1, opts);
  244. if (rslt.buffer)
  245. rslt.refreshFromBuffer = rslt.buffer.join('') != buffer.join('');
  246. return rslt;
  247. }
  248. }
  249. }
  250. if (opts.autoGroup) {
  251. var 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) + "]+");
  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 matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)),
  388. radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  389. if (matchRslt && !strict && (radixPosition === -1 || pos <= radixPosition)) {
  390. if (matchRslt["0"].indexOf("0") === 0) {
  391. if (pos < opts.prefix.length) pos = matchRslt.index; //position
  392. var _radixPosition = $.inArray(opts.radixPoint, maskset._buffer);
  393. var digitsMatch = maskset._buffer && maskset.buffer.slice(radixPosition).join("") === maskset._buffer.slice(_radixPosition).join("") || parseInt(maskset.buffer.slice(radixPosition + 1).join("")) === 0;
  394. var integerMatch = maskset._buffer && maskset.buffer.slice(matchRslt.index, radixPosition).join("") === maskset._buffer.slice(opts.prefix.length, _radixPosition).join("") || maskset.buffer.slice(matchRslt.index, radixPosition).join("") === "0";
  395. if (radixPosition === -1 || digitsMatch && integerMatch) {
  396. maskset.buffer.splice(matchRslt.index, 1);
  397. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  398. return {
  399. "pos": pos,
  400. "remove": matchRslt.index
  401. };
  402. } else if (matchRslt.index + 1 === pos || chrs === "0") {
  403. maskset.buffer.splice(matchRslt.index, 1);
  404. pos = matchRslt.index;
  405. return {
  406. "pos": pos,
  407. "remove": matchRslt.index
  408. };
  409. }
  410. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  411. return false;
  412. }
  413. }
  414. }
  415. return true;
  416. },
  417. definitions: {
  418. "~": {
  419. validator: function(chrs, maskset, pos, strict, opts) {
  420. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  421. if (!isValid) {
  422. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  423. if (!isValid) {
  424. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  425. if (isValid === true) {
  426. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  427. if (isValid === true) {
  428. //handle overwrite when fixed precision
  429. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  430. if (radixPosition !== -1 && opts.digitsOptional === false && opts.numericInput !== true && pos > radixPosition && !strict) {
  431. isValid = {
  432. "pos": pos,
  433. "remove": pos
  434. };
  435. } else {
  436. isValid = {
  437. pos: pos
  438. };
  439. }
  440. }
  441. }
  442. }
  443. }
  444. return isValid;
  445. },
  446. cardinality: 1,
  447. prevalidator: null
  448. },
  449. "+": {
  450. validator: function(chrs, maskset, pos, strict, opts) {
  451. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  452. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  453. if (chrs === "-") {
  454. if (opts.negationSymbol.back !== "") {
  455. isValid = {
  456. "pos": pos,
  457. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  458. "caret": pos + 1,
  459. "insert": {
  460. "pos": maskset.buffer.length,
  461. "c": opts.negationSymbol.back
  462. }
  463. };
  464. } else {
  465. isValid = {
  466. "pos": pos,
  467. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  468. "caret": pos + 1
  469. };
  470. }
  471. } else {
  472. isValid = true;
  473. }
  474. }
  475. return isValid;
  476. },
  477. cardinality: 1,
  478. prevalidator: null,
  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 && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  485. isValid = true;
  486. }
  487. return isValid;
  488. },
  489. cardinality: 1,
  490. prevalidator: null,
  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. prevalidator: null,
  511. placeholder: function(opts) {
  512. return opts.radixPoint;
  513. }
  514. }
  515. },
  516. onUnMask: function(maskedValue, unmaskedValue, opts) {
  517. var processValue = maskedValue.replace(opts.prefix, "");
  518. processValue = processValue.replace(opts.suffix, "");
  519. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  520. if (opts.unmaskAsNumber) {
  521. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  522. return Number(processValue);
  523. }
  524. return processValue;
  525. },
  526. isComplete: function(buffer, opts) {
  527. var maskedValue = buffer.join(""),
  528. bufClone = buffer.slice();
  529. //verify separator positions
  530. opts.postFormat(bufClone, 0, opts);
  531. if (bufClone.join("") !== maskedValue) return false;
  532. var processValue = maskedValue.replace(opts.prefix, "");
  533. processValue = processValue.replace(opts.suffix, "");
  534. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  535. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  536. return isFinite(processValue);
  537. },
  538. onBeforeMask: function(initialValue, opts) {
  539. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  540. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  541. } else {
  542. var kommaMatches = initialValue.match(/,/g);
  543. var dotMatches = initialValue.match(/\./g);
  544. if (dotMatches && kommaMatches) {
  545. if (dotMatches.length > kommaMatches.length) {
  546. initialValue = initialValue.replace(/\./g, "");
  547. initialValue = initialValue.replace(",", opts.radixPoint);
  548. } else if (kommaMatches.length > dotMatches.length) {
  549. initialValue = initialValue.replace(/,/g, "");
  550. initialValue = initialValue.replace(".", opts.radixPoint);
  551. } else { //equal
  552. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  553. }
  554. } else {
  555. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  556. }
  557. }
  558. if (opts.digits === 0) {
  559. if (initialValue.indexOf(".") !== -1) {
  560. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  561. } else if (initialValue.indexOf(",") !== -1) {
  562. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  563. }
  564. }
  565. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  566. var valueParts = initialValue.split(opts.radixPoint),
  567. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  568. if (parseInt(opts.digits) < decPart.toString().length) {
  569. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  570. //make the initialValue a valid javascript number for the parsefloat
  571. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  572. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  573. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  574. }
  575. }
  576. return initialValue.toString();
  577. },
  578. canClearPosition: function(maskset, position, lvp, strict, opts) {
  579. var positionInput = maskset.validPositions[position].input,
  580. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  581. position === lvp ||
  582. positionInput === opts.groupSeparator ||
  583. positionInput === opts.negationSymbol.front ||
  584. positionInput === opts.negationSymbol.back;
  585. return canClear;
  586. },
  587. onKeyDown: function(e, buffer, caretPos, opts) {
  588. var $input = $(this);
  589. if (e.ctrlKey) {
  590. switch (e.keyCode) {
  591. case Inputmask.keyCode.UP:
  592. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  593. $input.trigger("setvalue");
  594. break;
  595. case Inputmask.keyCode.DOWN:
  596. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  597. $input.trigger("setvalue");
  598. break;
  599. }
  600. }
  601. }
  602. },
  603. "currency": {
  604. prefix: "$ ",
  605. groupSeparator: ",",
  606. alias: "numeric",
  607. placeholder: "0",
  608. autoGroup: true,
  609. digits: 2,
  610. digitsOptional: false,
  611. clearMaskOnLostFocus: false
  612. },
  613. "decimal": {
  614. alias: "numeric"
  615. },
  616. "integer": {
  617. alias: "numeric",
  618. digits: 0,
  619. radixPoint: ""
  620. },
  621. "percentage": {
  622. alias: "numeric",
  623. digits: 2,
  624. radixPoint: ".",
  625. placeholder: "0",
  626. autoGroup: false,
  627. min: 0,
  628. max: 100,
  629. suffix: " %",
  630. allowPlus: false,
  631. allowMinus: false
  632. }
  633. });
  634. return Inputmask;
  635. }));