inputmask.numeric.extensions.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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(["./dependencyLibs/inputmask.dependencyLib", "./inputmask"], factory);
  12. } else if (typeof exports === "object") {
  13. module.exports = factory(require("./dependencyLibs/inputmask.dependencyLib"), require("./inputmask"));
  14. } else {
  15. factory(window.dependencyLib || jQuery, window.Inputmask);
  16. }
  17. }
  18. (function ($, Inputmask, undefined) {
  19. function autoEscape(txt, opts) {
  20. var escapedTxt = "";
  21. for (var i = 0; i < txt.length; i++) {
  22. if (opts.definitions[txt.charAt(i)] ||
  23. opts.optionalmarker.start === txt.charAt(i) ||
  24. opts.optionalmarker.end === txt.charAt(i) ||
  25. opts.quantifiermarker.start === txt.charAt(i) ||
  26. opts.quantifiermarker.end === txt.charAt(i) ||
  27. opts.groupmarker.start === txt.charAt(i) ||
  28. opts.groupmarker.end === txt.charAt(i) ||
  29. opts.alternatormarker === txt.charAt(i))
  30. escapedTxt += "\\" + txt.charAt(i)
  31. else escapedTxt += txt.charAt(i);
  32. }
  33. return escapedTxt;
  34. }
  35. //number aliases
  36. Inputmask.extendAliases({
  37. "numeric": {
  38. mask: function (opts) {
  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. if (opts.positionCaretOnClick === "radixFocus" && (opts.placeholder === "" && opts.integerOptional === false)) {
  71. opts.positionCaretOnClick = "lvp";
  72. }
  73. opts.definitions[";"] = opts.definitions["~"]; //clone integer def for decimals
  74. opts.definitions[";"].definitionSymbol = "~";
  75. if (opts.numericInput === true) { //finance people input style
  76. opts.positionCaretOnClick = opts.positionCaretOnClick === "radixFocus" ? "lvp" : opts.positionCaretOnClick;
  77. opts.digitsOptional = false;
  78. if (isNaN(opts.digits)) opts.digits = 2;
  79. opts.decimalProtect = false;
  80. }
  81. var mask = "[+]";
  82. mask += autoEscape(opts.prefix, opts);
  83. if (opts.integerOptional === true) {
  84. mask += "~{1," + opts.integerDigits + "}";
  85. } else mask += "~{" + opts.integerDigits + "}";
  86. if (opts.digits !== undefined) {
  87. opts.radixPointDefinitionSymbol = opts.decimalProtect ? ":" : opts.radixPoint;
  88. var dq = opts.digits.toString().split(",");
  89. if (isFinite(dq[0] && dq[1] && isFinite(dq[1]))) {
  90. mask += opts.radixPointDefinitionSymbol + ";{" + opts.digits + "}";
  91. } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
  92. if (opts.digitsOptional) {
  93. mask += "[" + opts.radixPointDefinitionSymbol + ";{1," + opts.digits + "}]";
  94. } else mask += opts.radixPointDefinitionSymbol + ";{" + opts.digits + "}";
  95. }
  96. }
  97. mask += autoEscape(opts.suffix, opts);
  98. mask += "[-]";
  99. opts.greedy = false; //enforce greedy false
  100. //convert min and max options
  101. if (opts.min !== null) {
  102. opts.min = opts.min.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  103. if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
  104. }
  105. if (opts.max !== null) {
  106. opts.max = opts.max.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  107. if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
  108. }
  109. return mask;
  110. },
  111. placeholder: "",
  112. greedy: false,
  113. digits: "*", //number of fractionalDigits
  114. digitsOptional: true,
  115. radixPoint: ".",
  116. positionCaretOnClick: "radixFocus",
  117. groupSize: 3,
  118. groupSeparator: "",
  119. autoGroup: false,
  120. allowMinus: true,
  121. negationSymbol: {
  122. front: "-", //"("
  123. back: "" //")"
  124. },
  125. integerDigits: "+", //number of integerDigits
  126. integerOptional: true,
  127. prefix: "",
  128. suffix: "",
  129. rightAlign: true,
  130. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  131. min: null, //minimum value
  132. max: null, //maximum value
  133. step: 1,
  134. insertMode: true,
  135. autoUnmask: false,
  136. unmaskAsNumber: false,
  137. inputmode: "numeric",
  138. preValidation: function (buffer, pos, c, opts) {
  139. if (c === "-" || c == opts.negationSymbol.front) {
  140. if (opts.allowMinus !== true) return false;
  141. opts.isNegative = opts.isNegative === undefined ? true : !opts.isNegative;
  142. return {caretPos: pos};
  143. }
  144. if (opts.numericInput !== true && c === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  145. var radixPos = $.inArray(opts.radixPoint, buffer);
  146. if (radixPos !== -1) {
  147. return {"caret": radixPos + 1};
  148. }
  149. }
  150. return true;
  151. },
  152. postValidation: function (buffer, currentResult, opts) {
  153. function buildPostMask(buffer, opts) {
  154. //define base for formatter
  155. var postMask = "";
  156. postMask += "(" + opts.groupSeparator + "*{" + opts.groupSize + "}){*}";
  157. if (opts.radixPoint !== "") {
  158. var radixSplit = buffer.join("").split(opts.radixPoint);
  159. if (radixSplit[1]) {
  160. postMask += opts.radixPoint + "*{" + radixSplit[1].match(/^\d*\??\d*/)[0].length + "}";
  161. }
  162. }
  163. return postMask;
  164. }
  165. var suffix = opts.suffix.split(""), prefix = opts.prefix.split("");
  166. if (currentResult.pos == undefined && currentResult.caret !== undefined) return currentResult;
  167. var caretPos = currentResult.caretPos || currentResult.pos;
  168. var maskedValue = buffer.slice();
  169. if (opts.numericInput) {
  170. caretPos = maskedValue.length - caretPos;
  171. maskedValue = maskedValue.reverse();
  172. }
  173. //mark caretPos
  174. var charAtPos = maskedValue[caretPos];
  175. if (caretPos == maskedValue.length - 1 && charAtPos === opts.radixPoint) return currentResult;
  176. if (charAtPos !== undefined) {
  177. if (charAtPos !== opts.radixPoint &&
  178. charAtPos !== opts.negationSymbol.front &&
  179. charAtPos !== opts.negationSymbol.back
  180. ) {
  181. maskedValue[caretPos] = "?";
  182. if (caretPos >= (opts.isNegative ? 0 : 1) && caretPos < opts.prefix.length + (opts.isNegative ? 0 : 1)) {
  183. prefix[caretPos - (opts.isNegative ? 0 : 1)] = "?";
  184. } else if (caretPos >= (maskedValue.length - opts.suffix.length) - (opts.isNegative ? 0 : 1)) {
  185. suffix[caretPos - (maskedValue.length - opts.suffix.length - (opts.isNegative ? 0 : 1))] = "?";
  186. }
  187. }
  188. }
  189. //make numeric
  190. prefix = prefix.join("");
  191. suffix = suffix.join("");
  192. var processValue = maskedValue.join("").replace(prefix, "");
  193. processValue = processValue.replace(suffix, "");
  194. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  195. //strip negation symbol
  196. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  197. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  198. //strip placeholder at the end
  199. if (isNaN(opts.placeholder)) {
  200. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.placeholder), "g"), "");
  201. }
  202. //strip leading zeroes
  203. processValue = processValue.replace(/^0/g, "");
  204. if (processValue[0] === opts.radixPoint) processValue = "0" + processValue;
  205. if (processValue !== "") {
  206. processValue = processValue.split("");
  207. //handle digits
  208. if (isFinite(opts.digits)) {
  209. var radixPosition = $.inArray(opts.radixPoint, processValue);
  210. var rpb = $.inArray(opts.radixPoint, maskedValue);
  211. if (radixPosition === -1) {
  212. processValue.push(opts.radixPoint);
  213. radixPosition = processValue.length - 1;
  214. }
  215. for (var i = 1; i <= opts.digits; i++) {
  216. if (!opts.digitsOptional && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  217. processValue[radixPosition + i] = "0";
  218. } else if (rpb !== -1 && maskedValue[rpb + i] !== undefined) {
  219. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  220. }
  221. }
  222. }
  223. if (opts.autoGroup === true && opts.groupSeparator !== "") {
  224. processValue = Inputmask(buildPostMask(processValue, opts), {
  225. numericInput: true, jitMasking: true,
  226. definitions: {
  227. "*": {
  228. validator: "[0-9?]",
  229. cardinality: 1
  230. }
  231. }
  232. }).format(processValue.join(""));
  233. } else processValue = processValue.join("");
  234. processValue = prefix + processValue;
  235. processValue += suffix;
  236. if (opts.isNegative) {
  237. processValue = opts.negationSymbol.front + processValue;
  238. processValue += opts.negationSymbol.back;
  239. }
  240. processValue = processValue.split("");
  241. //unmark position
  242. if (charAtPos !== undefined) {
  243. if (charAtPos !== opts.radixPoint && charAtPos !== opts.negationSymbol.front && charAtPos !== opts.negationSymbol.back) {
  244. caretPos = $.inArray("?", processValue);
  245. processValue[caretPos] = charAtPos;
  246. } else if (charAtPos === opts.radixPoint &&
  247. charAtPos === opts.negationSymbol.front &&
  248. charAtPos === opts.negationSymbol.back) {
  249. caretPos = $.inArray(charAtPos, processValue);
  250. }
  251. }
  252. if (opts.numericInput) {
  253. caretPos = processValue.length - caretPos;
  254. processValue = processValue.reverse();
  255. }
  256. var rslt = {
  257. caret: currentResult.caret || (currentResult.pos ? caretPos + 1 : caretPos),
  258. buffer: processValue,
  259. refreshFromBuffer: processValue.join("") != buffer.join("")
  260. };
  261. console.log(JSON.stringify(rslt));
  262. return rslt;
  263. }
  264. return currentResult;
  265. },
  266. onBeforeWrite: function (e, buffer, caretPos, opts) {
  267. if (e) {
  268. if (e.type === "keydown") {
  269. return opts.postValidation(buffer, {caretPos: caretPos}, opts);
  270. }
  271. if (e.type === "blur") {
  272. // var valc = processValue.split("")[caretPos] = charAtPos;
  273. // var floatValue = parseFloat(valc),
  274. // signedFloatValue = opts.isNegative ? floatValue * -1 : floatValue;
  275. //
  276. // if (opts.min !== null && isFinite(opts.min) && signedFloatValue < parseFloat(opts.min)) {
  277. // floatValue = Math.abs(opts.min);
  278. // opts.isNegative = opts.min < 0;
  279. // maskedValue = undefined;
  280. // }
  281. // else if (opts.max !== null && isFinite(opts.max) && signedFloatValue > parseFloat(opts.max)) {
  282. // floatValue = Math.abs(opts.max);
  283. // opts.isNegative = opts.max < 0;
  284. // maskedValue = undefined;
  285. // }
  286. //strip radixpoint at the end
  287. // if (processValue[processValue.length - 1] === opts.radixPoint) {
  288. // delete processValue[processValue.length - 1];
  289. // }
  290. }
  291. }
  292. },
  293. regex: {
  294. integerPart: function (opts, emptyCheck) {
  295. return emptyCheck ? new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?") : new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?\\d+");
  296. }
  297. ,
  298. integerNPart: function (opts) {
  299. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + Inputmask.escapeRegex(opts.placeholder.charAt(0)) + "]+");
  300. }
  301. }
  302. ,
  303. definitions: {
  304. "~": {
  305. validator: function (chrs, maskset, pos, strict, opts, isSelection) {
  306. var isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  307. if (isValid === true && opts.numericInput !== true) {
  308. //handle overwrite when fixed precision
  309. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  310. if (radixPosition !== -1 && (opts.digitsOptional === false || maskset.validPositions[pos]) && opts.numericInput !== true && pos > radixPosition && !strict) {
  311. isValid = {
  312. "pos": pos,
  313. "remove": pos
  314. };
  315. } else {
  316. isValid = {
  317. pos: pos
  318. };
  319. }
  320. }
  321. return isValid;
  322. },
  323. cardinality: 1
  324. },
  325. "+": {
  326. validator: function (chrs, maskset, pos, strict, opts) {
  327. return (strict && opts.allowMinus && chrs === opts.negationSymbol.front);
  328. },
  329. cardinality: 1,
  330. placeholder: ""
  331. },
  332. "-": {
  333. validator: function (chrs, maskset, pos, strict, opts) {
  334. return (strict && opts.allowMinus && chrs === opts.negationSymbol.back);
  335. },
  336. cardinality: 1,
  337. placeholder: ""
  338. },
  339. ":": {
  340. validator: function (chrs, maskset, pos, strict, opts) {
  341. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  342. isValid = new RegExp(radix).test(chrs);
  343. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  344. isValid = {
  345. "caret": pos + 1
  346. };
  347. }
  348. return isValid;
  349. },
  350. cardinality: 1,
  351. placeholder: function (opts) {
  352. return opts.radixPoint;
  353. }
  354. }
  355. }
  356. ,
  357. onUnMask: function (maskedValue, unmaskedValue, opts) {
  358. if (unmaskedValue === "" && opts.nullable === true) {
  359. return unmaskedValue;
  360. }
  361. var processValue = maskedValue.replace(opts.prefix, "");
  362. processValue = processValue.replace(opts.suffix, "");
  363. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  364. if (opts.unmaskAsNumber) {
  365. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  366. return Number(processValue);
  367. }
  368. return processValue;
  369. }
  370. ,
  371. isComplete: function (buffer, opts) {
  372. var maskedValue = buffer.join(""),
  373. bufClone = buffer.slice();
  374. //verify separator positions
  375. if (bufClone.join("") !== maskedValue) return false;
  376. var processValue = maskedValue.replace(opts.prefix, "");
  377. processValue = processValue.replace(opts.suffix, "");
  378. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  379. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  380. return isFinite(processValue);
  381. }
  382. ,
  383. onBeforeMask: function (initialValue, opts) {
  384. initialValue = initialValue.toString();
  385. if (opts.numericInput === true) {
  386. initialValue = initialValue.split("").reverse().join("");
  387. }
  388. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  389. var vs = initialValue.split("."),
  390. groupSize = opts.groupSeparator !== "" ? parseInt(opts.groupSize) : 0;
  391. if (vs.length === 2 && (vs[0].length > groupSize || vs[1].length > groupSize))
  392. initialValue = initialValue.replace(".", opts.radixPoint);
  393. }
  394. var kommaMatches = initialValue.match(/,/g);
  395. var dotMatches = initialValue.match(/\./g);
  396. if (dotMatches && kommaMatches) {
  397. if (dotMatches.length > kommaMatches.length) {
  398. initialValue = initialValue.replace(/\./g, "");
  399. initialValue = initialValue.replace(",", opts.radixPoint);
  400. } else if (kommaMatches.length > dotMatches.length) {
  401. initialValue = initialValue.replace(/,/g, "");
  402. initialValue = initialValue.replace(".", opts.radixPoint);
  403. } else { //equal
  404. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  405. }
  406. } else {
  407. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  408. }
  409. if (opts.digits === 0) {
  410. if (initialValue.indexOf(".") !== -1) {
  411. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  412. } else if (initialValue.indexOf(",") !== -1) {
  413. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  414. }
  415. }
  416. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  417. var valueParts = initialValue.split(opts.radixPoint),
  418. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  419. if (parseInt(opts.digits) < decPart.toString().length) {
  420. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  421. //make the initialValue a valid javascript number for the parsefloat
  422. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  423. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  424. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  425. }
  426. }
  427. if (opts.numericInput === true) {
  428. initialValue = initialValue.split("").reverse().join("");
  429. }
  430. return initialValue;
  431. }
  432. ,
  433. canClearPosition: function (maskset, position, lvp, strict, opts) {
  434. var positionInput = maskset.validPositions[position].input,
  435. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  436. position === lvp ||
  437. positionInput === opts.groupSeparator ||
  438. positionInput === opts.negationSymbol.front ||
  439. positionInput === opts.negationSymbol.back;
  440. return canClear;
  441. }
  442. ,
  443. onKeyDown: function (e, buffer, caretPos, opts) {
  444. var $input = $(this);
  445. if (e.ctrlKey) {
  446. switch (e.keyCode) {
  447. case Inputmask.keyCode.UP:
  448. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  449. $input.trigger("setvalue");
  450. break;
  451. case Inputmask.keyCode.DOWN:
  452. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  453. $input.trigger("setvalue");
  454. break;
  455. }
  456. }
  457. }
  458. },
  459. "currency": {
  460. prefix: "$ ",
  461. groupSeparator: ",",
  462. alias: "numeric",
  463. placeholder: "0",
  464. autoGroup: true,
  465. digits: 2,
  466. digitsOptional: false,
  467. clearMaskOnLostFocus: false
  468. },
  469. "decimal": {
  470. alias: "numeric"
  471. },
  472. "integer": {
  473. alias: "numeric",
  474. digits: 0,
  475. radixPoint: ""
  476. },
  477. "percentage": {
  478. alias: "numeric",
  479. digits: 2,
  480. radixPoint: ".",
  481. placeholder: "0",
  482. autoGroup: false,
  483. min: 0,
  484. max: 100,
  485. suffix: " %",
  486. allowMinus: false
  487. }
  488. })
  489. ;
  490. return Inputmask;
  491. }));