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. 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.digitsOptional) {
  77. mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{1," + opts.digits + "}]";
  78. } else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}";
  79. }
  80. if (opts.negationSymbol.back !== "") {
  81. mask += "[-]";
  82. }
  83. mask += autoEscape(opts.suffix);
  84. opts.greedy = false; //enforce greedy false
  85. return mask;
  86. },
  87. placeholder: "",
  88. greedy: false,
  89. digits: "*", //number of fractionalDigits
  90. digitsOptional: true,
  91. radixPoint: ".",
  92. radixFocus: true,
  93. groupSize: 3,
  94. groupSeparator: "",
  95. autoGroup: false,
  96. allowPlus: true,
  97. allowMinus: true,
  98. negationSymbol: {
  99. front: "-", //"("
  100. back: "" //")"
  101. },
  102. integerDigits: "+", //number of integerDigits
  103. integerOptional: true,
  104. prefix: "",
  105. suffix: "",
  106. rightAlign: true,
  107. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  108. min: null, //minimum value
  109. max: null, //maximum value
  110. step: 1,
  111. insertMode: true,
  112. autoUnmask: false,
  113. unmaskAsNumber: false,
  114. postFormat: function(buffer, pos, opts) { //this needs to be removed // this is crap
  115. // console.log(buffer);
  116. if (opts.numericInput === true) {
  117. buffer = buffer.reverse();
  118. if (isFinite(pos)) {
  119. pos = buffer.join("").length - pos - 1;
  120. }
  121. }
  122. var suffixStripped = false,
  123. i, l;
  124. if (buffer.length >= opts.suffix.length && buffer.join("").indexOf(opts.suffix) === (buffer.length - opts.suffix.length)) {
  125. buffer.length = buffer.length - opts.suffix.length; //strip suffix
  126. suffixStripped = true;
  127. }
  128. //position overflow corrections
  129. pos = pos >= buffer.length ? buffer.length - 1 : (pos < opts.prefix.length ? opts.prefix.length : pos);
  130. var needsRefresh = false,
  131. charAtPos = buffer[pos];
  132. var cbuf = buffer.slice();
  133. if (charAtPos === opts.groupSeparator) {
  134. cbuf.splice(pos--, 1);
  135. charAtPos = cbuf[pos];
  136. }
  137. if (charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back) cbuf[pos] = "?";
  138. var bufVal = cbuf.join(""),
  139. bufValOrigin = bufVal;
  140. if (bufVal.length > 0 && opts.autoGroup || bufVal.indexOf(opts.groupSeparator) !== -1) {
  141. var escapedGroupSeparator = Inputmask.escapeRegex(opts.groupSeparator);
  142. needsRefresh = bufVal.indexOf(opts.groupSeparator) === 0;
  143. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  144. var radixSplit = bufVal.split(opts.radixPoint);
  145. bufVal = opts.radixPoint === "" ? bufVal : radixSplit[0];
  146. if (bufVal !== (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
  147. //needsRefresh = true;
  148. var reg = new RegExp("([-\+]?[\\d\?]+)([\\d\?]{" + opts.groupSize + "})");
  149. while (reg.test(bufVal) && opts.groupSeparator !== "") {
  150. bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2");
  151. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  152. }
  153. }
  154. if (opts.radixPoint !== "" && radixSplit.length > 1) {
  155. bufVal += opts.radixPoint + radixSplit[1];
  156. }
  157. }
  158. needsRefresh = bufValOrigin !== bufVal;
  159. buffer.length = bufVal.length; //align the length
  160. for (i = 0, l = bufVal.length; i < l; i++) {
  161. buffer[i] = bufVal.charAt(i);
  162. }
  163. var newPos = $.inArray("?", buffer);
  164. if (newPos === -1) newPos = $.inArray(charAtPos, buffer);
  165. buffer[newPos] = charAtPos;
  166. if (!needsRefresh && suffixStripped) {
  167. for (i = 0, l = opts.suffix.length; i < l; i++) {
  168. buffer.push(opts.suffix.charAt(i));
  169. }
  170. }
  171. // console.log("formatted " + buffer + " refresh " + needsRefresh);
  172. newPos = (opts.numericInput && isFinite(pos)) ? buffer.join("").length - newPos - 1 : newPos;
  173. if (opts.numericInput) {
  174. buffer = buffer.reverse();
  175. if ($.inArray(opts.radixPoint, buffer) < newPos && (buffer.join("").length - opts.suffix.length) !== newPos) {
  176. newPos = newPos - 1;
  177. }
  178. }
  179. return {
  180. pos: newPos,
  181. "refreshFromBuffer": needsRefresh,
  182. "buffer": buffer
  183. };
  184. },
  185. onBeforeWrite: function(e, buffer, caretPos, opts) {
  186. if (e && (e.type === "blur" || e.type === "checkval" || e.type === "keydown")) {
  187. var maskedValue = opts.numericInput ? buffer.slice().reverse().join("") : buffer.join(""),
  188. processValue = maskedValue.replace(opts.prefix, ""),
  189. minmaxed = false;
  190. processValue = processValue.replace(opts.suffix, "");
  191. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  192. if (opts.radixPoint === ",") processValue = processValue.replace(opts.radixPoint, ".");
  193. //handle negation symbol
  194. var isNegative = processValue.match(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g")),
  195. isNegative = isNegative !== null && isNegative.length === 1;
  196. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  197. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  198. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  199. if (isFinite(processValue)) {
  200. var floatValue = parseFloat(processValue),
  201. signedFloatValue = isNegative ? floatValue * -1 : floatValue;
  202. if (opts.min !== null && isFinite(opts.min)) {
  203. if (signedFloatValue < parseFloat(opts.min)) {
  204. floatValue = Math.abs(opts.min);
  205. isNegative = opts.min < 0;
  206. minmaxed = true;
  207. }
  208. }
  209. if (!minmaxed && opts.max !== null && isFinite(opts.max)) {
  210. if (signedFloatValue > parseFloat(opts.max)) {
  211. floatValue = Math.abs(opts.max);
  212. isNegative = opts.max < 0;
  213. minmaxed = true;
  214. }
  215. }
  216. processValue = floatValue.toString().replace(".", opts.radixPoint).split('');
  217. if (isFinite(opts.digits)) {
  218. var radixPosition = $.inArray(opts.radixPoint, processValue);
  219. var rpb = $.inArray(opts.radixPoint, maskedValue);
  220. if (radixPosition === -1) {
  221. processValue.push(opts.radixPoint);
  222. radixPosition = processValue.length - 1;
  223. }
  224. for (var i = 1; i <= opts.digits; i++) {
  225. if (!opts.digitsOptional && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  226. processValue[radixPosition + i] = "0";
  227. } else if (rpb != -1 && maskedValue[rpb + i] != undefined) {
  228. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  229. }
  230. }
  231. if (processValue[processValue.length - 1] == opts.radixPoint) {
  232. delete processValue[processValue.length - 1];
  233. }
  234. }
  235. if ((floatValue.toString() !== processValue && floatValue.toString() + "." !== processValue) || isNegative) {
  236. if (isNegative && (floatValue !== 0 || e.type !== "blur")) {
  237. processValue.unshift(opts.negationSymbol.front);
  238. processValue.push(opts.negationSymbol.back);
  239. }
  240. processValue = (opts.prefix + processValue.join('')).split("");
  241. if (opts.numericInput) processValue = processValue.reverse();
  242. var rslt = opts.postFormat(processValue, opts.numericInput ? caretPos : caretPos - 1, opts);
  243. if (rslt.buffer)
  244. rslt.refreshFromBuffer = rslt.buffer.join('') != buffer.join('');
  245. return rslt;
  246. }
  247. }
  248. }
  249. if (opts.autoGroup) {
  250. var rslt = opts.postFormat(buffer, opts.numericInput ? caretPos : caretPos - 1, opts);
  251. rslt.caret = caretPos <= opts.prefix.length ? rslt.pos : rslt.pos + 1;
  252. return rslt;
  253. }
  254. },
  255. regex: {
  256. integerPart: function(opts) {
  257. return new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?\\d+");
  258. },
  259. integerNPart: function(opts) {
  260. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + "]+");
  261. }
  262. },
  263. signHandler: function(chrs, maskset, pos, strict, opts) {
  264. if (!strict && (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+")) {
  265. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  266. if (matchRslt && matchRslt[0].length > 0) {
  267. if (maskset.buffer[matchRslt.index] === (chrs === "-" ? "+" : opts.negationSymbol.front)) {
  268. if (chrs === "-") {
  269. if (opts.negationSymbol.back !== "") {
  270. return {
  271. "pos": matchRslt.index,
  272. "c": opts.negationSymbol.front,
  273. "remove": matchRslt.index,
  274. "caret": pos,
  275. "insert": {
  276. "pos": maskset.buffer.length - opts.suffix.length - 1,
  277. "c": opts.negationSymbol.back
  278. }
  279. };
  280. } else {
  281. return {
  282. "pos": matchRslt.index,
  283. "c": opts.negationSymbol.front,
  284. "remove": matchRslt.index,
  285. "caret": pos
  286. };
  287. }
  288. } else {
  289. if (opts.negationSymbol.back !== "") {
  290. return {
  291. "pos": matchRslt.index,
  292. "c": "+",
  293. "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  294. "caret": pos
  295. };
  296. } else {
  297. return {
  298. "pos": matchRslt.index,
  299. "c": "+",
  300. "remove": matchRslt.index,
  301. "caret": pos
  302. };
  303. }
  304. }
  305. } else if (maskset.buffer[matchRslt.index] === (chrs === "-" ? opts.negationSymbol.front : "+")) {
  306. if (chrs === "-" && opts.negationSymbol.back !== "") {
  307. return {
  308. "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  309. "caret": pos - 1
  310. };
  311. } else {
  312. return {
  313. "remove": matchRslt.index,
  314. "caret": pos - 1
  315. };
  316. }
  317. } else {
  318. if (chrs === "-") {
  319. if (opts.negationSymbol.back !== "") {
  320. return {
  321. "pos": matchRslt.index,
  322. "c": opts.negationSymbol.front,
  323. "caret": pos + 1,
  324. "insert": {
  325. "pos": maskset.buffer.length - opts.suffix.length,
  326. "c": opts.negationSymbol.back
  327. }
  328. };
  329. } else {
  330. return {
  331. "pos": matchRslt.index,
  332. "c": opts.negationSymbol.front,
  333. "caret": pos + 1
  334. };
  335. }
  336. } else {
  337. return {
  338. "pos": matchRslt.index,
  339. "c": chrs,
  340. "caret": pos + 1
  341. };
  342. }
  343. }
  344. }
  345. }
  346. return false;
  347. },
  348. radixHandler: function(chrs, maskset, pos, strict, opts) {
  349. if (!strict) {
  350. if ($.inArray(chrs, [",", "."]) !== -1) chrs = opts.radixPoint;
  351. if (chrs === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  352. var radixPos = $.inArray(opts.radixPoint, maskset.buffer),
  353. integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  354. if (radixPos !== -1 && maskset.validPositions[radixPos]) {
  355. if (maskset.validPositions[radixPos - 1]) {
  356. return {
  357. "caret": radixPos + 1
  358. };
  359. } else {
  360. return {
  361. "pos": integerValue.index,
  362. c: integerValue[0],
  363. "caret": radixPos + 1
  364. };
  365. }
  366. } else if (!integerValue || (integerValue["0"] === "0" && (integerValue.index + 1) !== pos)) {
  367. maskset.buffer[integerValue ? integerValue.index : pos] = "0";
  368. return {
  369. "pos": (integerValue ? integerValue.index : pos) + 1,
  370. c: opts.radixPoint
  371. };
  372. }
  373. }
  374. }
  375. return false;
  376. },
  377. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  378. if (opts.numericInput === true) {
  379. if (maskset.buffer[maskset.buffer.length - opts.prefix.length - 1] === "0") {
  380. return {
  381. "pos": pos,
  382. "remove": maskset.buffer.length - opts.prefix.length - 1
  383. };
  384. }
  385. } else {
  386. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)),
  387. radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  388. if (matchRslt && !strict && (radixPosition === -1 || pos <= radixPosition)) {
  389. if (matchRslt["0"].indexOf("0") === 0) {
  390. if (pos < opts.prefix.length) pos = matchRslt.index; //position
  391. var _radixPosition = $.inArray(opts.radixPoint, maskset._buffer);
  392. var digitsMatch = maskset._buffer && maskset.buffer.slice(radixPosition).join("") === maskset._buffer.slice(_radixPosition).join("") || parseInt(maskset.buffer.slice(radixPosition + 1).join("")) === 0;
  393. 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";
  394. if (radixPosition === -1 || digitsMatch && integerMatch) {
  395. maskset.buffer.splice(matchRslt.index, 1);
  396. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  397. return {
  398. "pos": pos,
  399. "remove": matchRslt.index
  400. };
  401. } else if (matchRslt.index + 1 === pos || chrs === "0") {
  402. maskset.buffer.splice(matchRslt.index, 1);
  403. pos = matchRslt.index;
  404. return {
  405. "pos": pos,
  406. "remove": matchRslt.index
  407. };
  408. }
  409. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  410. return false;
  411. }
  412. }
  413. }
  414. return true;
  415. },
  416. definitions: {
  417. "~": {
  418. validator: function(chrs, maskset, pos, strict, opts) {
  419. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  420. if (!isValid) {
  421. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  422. if (!isValid) {
  423. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  424. if (isValid === true) {
  425. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  426. if (isValid === true) {
  427. //handle overwrite when fixed precision
  428. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  429. if (radixPosition !== -1 && opts.digitsOptional === false && opts.numericInput !== true && pos > radixPosition && !strict) {
  430. isValid = {
  431. "pos": pos,
  432. "remove": pos
  433. };
  434. } else {
  435. isValid = {
  436. pos: pos
  437. };
  438. }
  439. }
  440. }
  441. }
  442. }
  443. return isValid;
  444. },
  445. cardinality: 1,
  446. prevalidator: null
  447. },
  448. "+": {
  449. validator: function(chrs, maskset, pos, strict, opts) {
  450. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  451. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  452. if (chrs === "-") {
  453. if (opts.negationSymbol.back !== "") {
  454. isValid = {
  455. "pos": pos,
  456. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  457. "caret": pos + 1,
  458. "insert": {
  459. "pos": maskset.buffer.length,
  460. "c": opts.negationSymbol.back
  461. }
  462. };
  463. } else {
  464. isValid = {
  465. "pos": pos,
  466. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  467. "caret": pos + 1
  468. };
  469. }
  470. } else {
  471. isValid = true;
  472. }
  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 && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  484. isValid = true;
  485. }
  486. return isValid;
  487. },
  488. cardinality: 1,
  489. prevalidator: null,
  490. placeholder: ""
  491. },
  492. ":": {
  493. validator: function(chrs, maskset, pos, strict, opts) {
  494. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  495. if (!isValid) {
  496. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + ",\\." + "]";
  497. isValid = new RegExp(radix).test(chrs);
  498. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  499. isValid = {
  500. "caret": pos + 1
  501. };
  502. }
  503. }
  504. return isValid ? {
  505. c: opts.radixPoint
  506. } : isValid;
  507. },
  508. cardinality: 1,
  509. prevalidator: null,
  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. }));