inputmask.numeric.extensions.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. //strip placeholder at the end
  216. if (isNaN(opts.placeholder)) {
  217. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.placeholder), "g"), "");
  218. }
  219. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  220. if (processValue !== "" && isFinite(processValue)) {
  221. var floatValue = parseFloat(processValue),
  222. signedFloatValue = isNegative ? floatValue * -1 : floatValue;
  223. if (opts.min !== null && isFinite(opts.min) && signedFloatValue < parseFloat(opts.min)) {
  224. floatValue = Math.abs(opts.min);
  225. isNegative = opts.min < 0;
  226. maskedValue = undefined;
  227. }
  228. else if (opts.max !== null && isFinite(opts.max) && signedFloatValue > parseFloat(opts.max)) {
  229. floatValue = Math.abs(opts.max);
  230. isNegative = opts.max < 0;
  231. maskedValue = undefined;
  232. }
  233. processValue = floatValue.toString().replace(".", opts.radixPoint).split('');
  234. if (isFinite(opts.digits)) {
  235. var radixPosition = $.inArray(opts.radixPoint, processValue);
  236. var rpb = $.inArray(opts.radixPoint, maskedValue);
  237. if (radixPosition === -1) {
  238. processValue.push(opts.radixPoint);
  239. radixPosition = processValue.length - 1;
  240. }
  241. for (var i = 1; i <= opts.digits; i++) {
  242. if (!opts.digitsOptional && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  243. processValue[radixPosition + i] = "0";
  244. } else if (rpb !== -1 && maskedValue[rpb + i] !== undefined) {
  245. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  246. }
  247. }
  248. if (processValue[processValue.length - 1] === opts.radixPoint) {
  249. delete processValue[processValue.length - 1];
  250. }
  251. }
  252. if ((floatValue.toString() !== processValue && floatValue.toString() + "." !== processValue) || isNegative) {
  253. if (isNegative && (floatValue !== 0 || e.type !== "blur")) {
  254. processValue.unshift(opts.negationSymbol.front);
  255. processValue.push(opts.negationSymbol.back);
  256. }
  257. processValue = (opts.prefix + processValue.join("")).split("");
  258. if (opts.numericInput) processValue = processValue.reverse();
  259. rslt = opts.postFormat(processValue, opts.numericInput ? caretPos : caretPos - 1, opts);
  260. if (rslt.buffer) rslt.refreshFromBuffer = rslt.buffer.join("") !== buffer.join("");
  261. return rslt;
  262. }
  263. }
  264. }
  265. if (opts.autoGroup) {
  266. rslt = opts.postFormat(buffer, opts.numericInput ? caretPos : caretPos - 1, opts);
  267. rslt.caret = caretPos <= opts.prefix.length ? rslt.pos : rslt.pos + 1;
  268. return rslt;
  269. }
  270. },
  271. regex: {
  272. integerPart: function (opts) {
  273. return new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?\\d+");
  274. },
  275. integerNPart: function (opts) {
  276. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + Inputmask.escapeRegex(opts.placeholder.charAt(0)) + "]+");
  277. }
  278. },
  279. signHandler: function (chrs, maskset, pos, strict, opts) {
  280. if (!strict && (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+")) {
  281. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  282. if (matchRslt && matchRslt[0].length > 0) {
  283. if (maskset.buffer[matchRslt.index] === (chrs === "-" ? "+" : opts.negationSymbol.front)) {
  284. if (chrs === "-") {
  285. if (opts.negationSymbol.back !== "") {
  286. return {
  287. "pos": matchRslt.index,
  288. "c": opts.negationSymbol.front,
  289. "remove": matchRslt.index,
  290. "caret": pos,
  291. "insert": {
  292. "pos": maskset.buffer.length - opts.suffix.length - 1,
  293. "c": opts.negationSymbol.back
  294. }
  295. };
  296. } else {
  297. return {
  298. "pos": matchRslt.index,
  299. "c": opts.negationSymbol.front,
  300. "remove": matchRslt.index,
  301. "caret": pos
  302. };
  303. }
  304. } else {
  305. if (opts.negationSymbol.back !== "") {
  306. return {
  307. "pos": matchRslt.index,
  308. "c": "+",
  309. "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  310. "caret": pos
  311. };
  312. } else {
  313. return {
  314. "pos": matchRslt.index,
  315. "c": "+",
  316. "remove": matchRslt.index,
  317. "caret": pos
  318. };
  319. }
  320. }
  321. } else if (maskset.buffer[matchRslt.index] === (chrs === "-" ? opts.negationSymbol.front : "+")) {
  322. if (chrs === "-" && opts.negationSymbol.back !== "") {
  323. return {
  324. "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  325. "caret": pos - 1
  326. };
  327. } else {
  328. return {
  329. "remove": matchRslt.index,
  330. "caret": pos - 1
  331. };
  332. }
  333. } else {
  334. if (chrs === "-") {
  335. if (opts.negationSymbol.back !== "") {
  336. return {
  337. "pos": matchRslt.index,
  338. "c": opts.negationSymbol.front,
  339. "caret": pos + 1,
  340. "insert": {
  341. "pos": maskset.buffer.length - opts.suffix.length,
  342. "c": opts.negationSymbol.back
  343. }
  344. };
  345. } else {
  346. return {
  347. "pos": matchRslt.index,
  348. "c": opts.negationSymbol.front,
  349. "caret": pos + 1
  350. };
  351. }
  352. } else {
  353. return {
  354. "pos": matchRslt.index,
  355. "c": chrs,
  356. "caret": pos + 1
  357. };
  358. }
  359. }
  360. }
  361. }
  362. return false;
  363. },
  364. radixHandler: function (chrs, maskset, pos, strict, opts) {
  365. if (!strict && opts.numericInput !== true) {
  366. //if ($.inArray(chrs, [",", "."]) !== -1) chrs = opts.radixPoint;
  367. if (chrs === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  368. var radixPos = $.inArray(opts.radixPoint, maskset.buffer),
  369. integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  370. if (radixPos !== -1 && maskset.validPositions[radixPos]) {
  371. if (maskset.validPositions[radixPos - 1]) {
  372. return {
  373. "caret": radixPos + 1
  374. };
  375. } else {
  376. return {
  377. "pos": integerValue.index,
  378. c: integerValue[0],
  379. "caret": radixPos + 1
  380. };
  381. }
  382. } else if (!integerValue || (integerValue["0"] === "0" && (integerValue.index + 1) !== pos)) {
  383. maskset.buffer[integerValue ? integerValue.index : pos] = "0";
  384. return {
  385. "pos": (integerValue ? integerValue.index : pos) + 1,
  386. c: opts.radixPoint
  387. };
  388. }
  389. }
  390. }
  391. return false;
  392. },
  393. leadingZeroHandler: function (chrs, maskset, pos, strict, opts, isSelection) {
  394. if (!strict) {
  395. if (opts.numericInput === true) {
  396. var buffer = maskset.buffer.slice("").reverse();
  397. var bufferChar = buffer[opts.prefix.length];
  398. if (bufferChar === "0" && maskset.validPositions[pos - 1] === undefined) {
  399. return {
  400. "pos": pos,
  401. "remove": buffer.length - opts.prefix.length - 1
  402. };
  403. }
  404. } else {
  405. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer),
  406. matchRslt = maskset.buffer.slice(0, radixPosition !== -1 ? radixPosition : undefined).join("").match(opts.regex.integerNPart(opts));
  407. if (matchRslt && (radixPosition === -1 || pos <= radixPosition)) {
  408. var decimalPart = radixPosition === -1 ? 0 : parseInt(maskset.buffer.slice(radixPosition + 1).join(""));
  409. if (matchRslt["0"].indexOf(opts.placeholder !== "" ? opts.placeholder.charAt(0) : "0") === 0 &&
  410. (matchRslt.index + 1 === pos || (isSelection !== true && decimalPart === 0))) {
  411. maskset.buffer.splice(matchRslt.index, 1);
  412. return {
  413. "pos": matchRslt.index,
  414. "remove": matchRslt.index
  415. };
  416. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  417. return false;
  418. }
  419. }
  420. }
  421. }
  422. return true;
  423. },
  424. definitions: {
  425. "~": {
  426. validator: function (chrs, maskset, pos, strict, opts, isSelection) {
  427. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  428. if (!isValid) {
  429. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  430. if (!isValid) {
  431. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  432. if (isValid === true) {
  433. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts, isSelection);
  434. if (isValid === true) {
  435. //handle overwrite when fixed precision
  436. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  437. if (radixPosition !== -1 && (opts.digitsOptional === false || maskset.validPositions[pos]) && opts.numericInput !== true && pos > radixPosition && !strict) {
  438. isValid = {
  439. "pos": pos,
  440. "remove": pos
  441. };
  442. } else {
  443. isValid = {
  444. pos: pos
  445. };
  446. }
  447. }
  448. }
  449. }
  450. }
  451. return isValid;
  452. },
  453. cardinality: 1
  454. },
  455. "+": {
  456. validator: function (chrs, maskset, pos, strict, opts) {
  457. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  458. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  459. if (!strict && chrs === "-") {
  460. if (opts.negationSymbol.back !== "") {
  461. isValid = {
  462. "pos": pos,
  463. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  464. "caret": pos + 1,
  465. "insert": {
  466. "pos": maskset.buffer.length,
  467. "c": opts.negationSymbol.back
  468. }
  469. };
  470. } else {
  471. isValid = {
  472. "pos": pos,
  473. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  474. "caret": pos + 1
  475. };
  476. }
  477. } else {
  478. isValid = true;
  479. }
  480. }
  481. return isValid;
  482. },
  483. cardinality: 1,
  484. placeholder: ""
  485. },
  486. "-": {
  487. validator: function (chrs, maskset, pos, strict, opts) {
  488. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  489. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  490. isValid = true;
  491. }
  492. return isValid;
  493. },
  494. cardinality: 1,
  495. placeholder: ""
  496. },
  497. ":": {
  498. validator: function (chrs, maskset, pos, strict, opts) {
  499. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  500. if (!isValid) {
  501. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  502. isValid = new RegExp(radix).test(chrs);
  503. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  504. isValid = {
  505. "caret": pos + 1
  506. };
  507. }
  508. }
  509. return isValid ? {
  510. c: opts.radixPoint
  511. } : isValid;
  512. },
  513. cardinality: 1,
  514. placeholder: function (opts) {
  515. return opts.radixPoint;
  516. }
  517. }
  518. },
  519. onUnMask: function (maskedValue, unmaskedValue, opts) {
  520. var processValue = maskedValue.replace(opts.prefix, "");
  521. processValue = processValue.replace(opts.suffix, "");
  522. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  523. if (opts.unmaskAsNumber) {
  524. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  525. return Number(processValue);
  526. }
  527. return processValue;
  528. },
  529. isComplete: function (buffer, opts) {
  530. var maskedValue = buffer.join(""),
  531. bufClone = buffer.slice();
  532. //verify separator positions
  533. opts.postFormat(bufClone, 0, opts);
  534. if (bufClone.join("") !== maskedValue) return false;
  535. var processValue = maskedValue.replace(opts.prefix, "");
  536. processValue = processValue.replace(opts.suffix, "");
  537. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  538. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  539. return isFinite(processValue);
  540. },
  541. onBeforeMask: function (initialValue, opts) {
  542. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  543. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  544. } else {
  545. var kommaMatches = initialValue.match(/,/g);
  546. var dotMatches = initialValue.match(/\./g);
  547. if (dotMatches && kommaMatches) {
  548. if (dotMatches.length > kommaMatches.length) {
  549. initialValue = initialValue.replace(/\./g, "");
  550. initialValue = initialValue.replace(",", opts.radixPoint);
  551. } else if (kommaMatches.length > dotMatches.length) {
  552. initialValue = initialValue.replace(/,/g, "");
  553. initialValue = initialValue.replace(".", opts.radixPoint);
  554. } else { //equal
  555. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  556. }
  557. } else {
  558. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  559. }
  560. }
  561. if (opts.digits === 0) {
  562. if (initialValue.indexOf(".") !== -1) {
  563. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  564. } else if (initialValue.indexOf(",") !== -1) {
  565. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  566. }
  567. }
  568. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  569. var valueParts = initialValue.split(opts.radixPoint),
  570. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  571. if (parseInt(opts.digits) < decPart.toString().length) {
  572. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  573. //make the initialValue a valid javascript number for the parsefloat
  574. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  575. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  576. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  577. }
  578. }
  579. return initialValue.toString();
  580. },
  581. canClearPosition: function (maskset, position, lvp, strict, opts) {
  582. var positionInput = maskset.validPositions[position].input,
  583. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  584. position === lvp ||
  585. positionInput === opts.groupSeparator ||
  586. positionInput === opts.negationSymbol.front ||
  587. positionInput === opts.negationSymbol.back;
  588. return canClear;
  589. },
  590. onKeyDown: function (e, buffer, caretPos, opts) {
  591. var $input = $(this);
  592. if (e.ctrlKey) {
  593. switch (e.keyCode) {
  594. case Inputmask.keyCode.UP:
  595. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  596. $input.trigger("setvalue");
  597. break;
  598. case Inputmask.keyCode.DOWN:
  599. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  600. $input.trigger("setvalue");
  601. break;
  602. }
  603. }
  604. }
  605. },
  606. "currency": {
  607. prefix: "$ ",
  608. groupSeparator: ",",
  609. alias: "numeric",
  610. placeholder: "0",
  611. autoGroup: true,
  612. digits: 2,
  613. digitsOptional: false,
  614. clearMaskOnLostFocus: false
  615. },
  616. "decimal": {
  617. alias: "numeric"
  618. },
  619. "integer": {
  620. alias: "numeric",
  621. digits: 0,
  622. radixPoint: ""
  623. },
  624. "percentage": {
  625. alias: "numeric",
  626. digits: 2,
  627. radixPoint: ".",
  628. placeholder: "0",
  629. autoGroup: false,
  630. min: 0,
  631. max: 100,
  632. suffix: " %",
  633. allowPlus: false,
  634. allowMinus: false
  635. }
  636. });
  637. return Inputmask;
  638. }));