inputmask.numeric.extensions.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/inputmask
  4. Copyright (c) Robin Herbots
  5. Licensed under the MIT license
  6. */
  7. import escapeRegex from "../escapeRegex";
  8. import Inputmask from "../inputmask";
  9. import { keys } from "../keycode";
  10. import { seekNext } from "../positioning";
  11. const $ = Inputmask.dependencyLib;
  12. function autoEscape(txt, opts) {
  13. let escapedTxt = "";
  14. for (let i = 0; i < txt.length; i++) {
  15. if (
  16. Inputmask.prototype.definitions[txt.charAt(i)] ||
  17. opts.definitions[txt.charAt(i)] ||
  18. opts.optionalmarker[0] === txt.charAt(i) ||
  19. opts.optionalmarker[1] === txt.charAt(i) ||
  20. opts.quantifiermarker[0] === txt.charAt(i) ||
  21. opts.quantifiermarker[1] === txt.charAt(i) ||
  22. opts.groupmarker[0] === txt.charAt(i) ||
  23. opts.groupmarker[1] === txt.charAt(i) ||
  24. opts.alternatormarker === txt.charAt(i)
  25. ) {
  26. escapedTxt += "\\" + txt.charAt(i);
  27. } else {
  28. escapedTxt += txt.charAt(i);
  29. }
  30. }
  31. return escapedTxt;
  32. }
  33. function alignDigits(buffer, digits, opts, force) {
  34. if (buffer.length > 0 && digits > 0 && (!opts.digitsOptional || force)) {
  35. var radixPosition = buffer.indexOf(opts.radixPoint),
  36. negationBack = false;
  37. if (opts.negationSymbol.back === buffer[buffer.length - 1]) {
  38. negationBack = true;
  39. buffer.length--;
  40. }
  41. if (radixPosition === -1) {
  42. buffer.push(opts.radixPoint);
  43. radixPosition = buffer.length - 1;
  44. }
  45. for (let i = 1; i <= digits; i++) {
  46. if (!isFinite(buffer[radixPosition + i])) {
  47. buffer[radixPosition + i] = "0";
  48. }
  49. }
  50. }
  51. if (negationBack) buffer.push(opts.negationSymbol.back);
  52. return buffer;
  53. }
  54. function findValidator(symbol, maskset) {
  55. let posNdx = 0;
  56. if (symbol === "+") {
  57. posNdx = seekNext.call(this, maskset.validPositions.length - 1);
  58. }
  59. for (let tstNdx in maskset.tests) {
  60. tstNdx = parseInt(tstNdx);
  61. if (tstNdx >= posNdx) {
  62. for (
  63. let ndx = 0, ndxl = maskset.tests[tstNdx].length;
  64. ndx < ndxl;
  65. ndx++
  66. ) {
  67. if (
  68. (maskset.validPositions[tstNdx] === undefined || symbol === "-") &&
  69. maskset.tests[tstNdx][ndx].match.def === symbol
  70. ) {
  71. return (
  72. tstNdx +
  73. (maskset.validPositions[tstNdx] !== undefined && symbol !== "-"
  74. ? 1
  75. : 0)
  76. );
  77. }
  78. }
  79. }
  80. }
  81. return posNdx;
  82. }
  83. function findValid(symbol, maskset) {
  84. let ret = -1;
  85. for (let ndx = 0, vpl = maskset.validPositions.length; ndx < vpl; ndx++) {
  86. const tst = maskset.validPositions[ndx];
  87. if (tst && tst.match.def === symbol) {
  88. ret = ndx;
  89. break;
  90. }
  91. }
  92. return ret;
  93. }
  94. function parseMinMaxOptions(opts) {
  95. if (opts.parseMinMaxOptions === undefined) {
  96. // convert min and max options
  97. if (opts.min !== null) {
  98. opts.min = opts.min
  99. .toString()
  100. .replace(new RegExp(escapeRegex(opts.groupSeparator), "g"), "");
  101. if (opts.radixPoint === ",")
  102. opts.min = opts.min.replace(opts.radixPoint, ".");
  103. opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN;
  104. if (isNaN(opts.min)) opts.min = Number.MIN_VALUE;
  105. }
  106. if (opts.max !== null) {
  107. opts.max = opts.max
  108. .toString()
  109. .replace(new RegExp(escapeRegex(opts.groupSeparator), "g"), "");
  110. if (opts.radixPoint === ",")
  111. opts.max = opts.max.replace(opts.radixPoint, ".");
  112. opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN;
  113. if (isNaN(opts.max)) opts.max = Number.MAX_VALUE;
  114. }
  115. opts.parseMinMaxOptions = "done";
  116. }
  117. }
  118. function genMask(opts) {
  119. opts.repeat = 0;
  120. // treat equal separator and radixpoint
  121. if (
  122. opts.groupSeparator === opts.radixPoint &&
  123. opts.digits &&
  124. opts.digits !== "0"
  125. ) {
  126. if (opts.radixPoint === ".") {
  127. opts.groupSeparator = ",";
  128. } else if (opts.radixPoint === ",") {
  129. opts.groupSeparator = ".";
  130. } else {
  131. opts.groupSeparator = "";
  132. }
  133. }
  134. // prevent conflict with default skipOptionalPartCharacter
  135. if (opts.groupSeparator === " ") {
  136. opts.skipOptionalPartCharacter = undefined;
  137. }
  138. // enforce placeholder to single
  139. if (opts.placeholder.length > 1) {
  140. opts.placeholder = opts.placeholder.charAt(0);
  141. }
  142. // only allow radixfocus when placeholder = 0
  143. if (opts.positionCaretOnClick === "radixFocus" && opts.placeholder === "") {
  144. opts.positionCaretOnClick = "lvp";
  145. }
  146. let decimalDef = "0",
  147. radixPointDef = opts.radixPoint;
  148. if (opts.numericInput === true && opts.__financeInput === undefined) {
  149. // finance people input style
  150. decimalDef = "1";
  151. opts.positionCaretOnClick =
  152. opts.positionCaretOnClick === "radixFocus"
  153. ? "lvp"
  154. : opts.positionCaretOnClick;
  155. opts.digitsOptional = false;
  156. if (isNaN(opts.digits)) opts.digits = 2;
  157. opts._radixDance = false;
  158. radixPointDef = opts.radixPoint === "," ? "?" : "!";
  159. if (
  160. opts.radixPoint !== "" &&
  161. opts.definitions[radixPointDef] === undefined
  162. ) {
  163. // update separator definition
  164. opts.definitions[radixPointDef] = {};
  165. opts.definitions[radixPointDef].validator = "[" + opts.radixPoint + "]";
  166. opts.definitions[radixPointDef].placeholder = opts.radixPoint;
  167. opts.definitions[radixPointDef].static = true;
  168. opts.definitions[radixPointDef].generated = true; // forced marker as generated input
  169. }
  170. } else {
  171. opts.__financeInput = false; // needed to keep original selection when remasking
  172. opts.numericInput = true;
  173. }
  174. let mask = "[+]",
  175. altMask;
  176. mask += autoEscape(opts.prefix, opts);
  177. if (opts.groupSeparator !== "") {
  178. if (opts.definitions[opts.groupSeparator] === undefined) {
  179. // update separator definition
  180. opts.definitions[opts.groupSeparator] = {};
  181. opts.definitions[opts.groupSeparator].validator =
  182. "[" + opts.groupSeparator + "]";
  183. opts.definitions[opts.groupSeparator].placeholder = opts.groupSeparator;
  184. opts.definitions[opts.groupSeparator].static = true;
  185. opts.definitions[opts.groupSeparator].generated = true; // forced marker as generated input
  186. }
  187. mask += opts._mask(opts);
  188. } else {
  189. mask += "9{+}";
  190. }
  191. if (opts.digits !== undefined && opts.digits !== 0) {
  192. const dq = opts.digits.toString().split(",");
  193. if (isFinite(dq[0]) && dq[1] && isFinite(dq[1])) {
  194. mask += radixPointDef + decimalDef + "{" + opts.digits + "}";
  195. } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
  196. if (opts.digitsOptional || opts.jitMasking) {
  197. altMask = mask + radixPointDef + decimalDef + "{0," + opts.digits + "}";
  198. // mask += "[" + opts.radixPoint + "]";
  199. opts.keepStatic = true;
  200. } else {
  201. mask += radixPointDef + decimalDef + "{" + opts.digits + "}";
  202. }
  203. }
  204. } else {
  205. opts.inputmode = "numeric";
  206. }
  207. mask += autoEscape(opts.suffix, opts);
  208. mask += "[-]";
  209. if (altMask) {
  210. mask = [altMask + autoEscape(opts.suffix, opts) + "[-]", mask];
  211. }
  212. opts.greedy = false; // enforce greedy false
  213. parseMinMaxOptions(opts);
  214. if (opts.radixPoint !== "" && opts.substituteRadixPoint)
  215. opts.substitutes[opts.radixPoint == "." ? "," : "."] = opts.radixPoint;
  216. // console.log(mask);
  217. return mask;
  218. }
  219. function hanndleRadixDance(pos, c, radixPos, maskset, opts) {
  220. if (opts._radixDance && opts.numericInput && c !== opts.negationSymbol.back) {
  221. if (
  222. pos <= radixPos &&
  223. (radixPos > 0 || c == opts.radixPoint) &&
  224. (maskset.validPositions[pos - 1] === undefined ||
  225. maskset.validPositions[pos - 1].input !== opts.negationSymbol.back)
  226. ) {
  227. pos -= 1;
  228. }
  229. }
  230. return pos;
  231. }
  232. function decimalValidator(chrs, maskset, pos, strict, opts) {
  233. const radixPos = maskset.buffer
  234. ? maskset.buffer.indexOf(opts.radixPoint)
  235. : -1,
  236. result =
  237. (radixPos !== -1 || (strict && opts.jitMasking)) &&
  238. new RegExp(opts.definitions["9"].validator).test(chrs);
  239. if (
  240. !strict &&
  241. opts._radixDance &&
  242. radixPos !== -1 &&
  243. result &&
  244. maskset.validPositions[radixPos] == undefined
  245. ) {
  246. return {
  247. insert: {
  248. pos: radixPos === pos ? radixPos + 1 : radixPos,
  249. c: opts.radixPoint
  250. },
  251. pos
  252. };
  253. }
  254. return result;
  255. }
  256. function checkForLeadingZeroes(buffer, opts) {
  257. // check leading zeros
  258. let numberMatches = new RegExp(
  259. "(^" +
  260. (opts.negationSymbol.front !== ""
  261. ? escapeRegex(opts.negationSymbol.front) + "?"
  262. : "") +
  263. escapeRegex(opts.prefix) +
  264. ")(.*)(" +
  265. escapeRegex(opts.suffix) +
  266. (opts.negationSymbol.back != ""
  267. ? escapeRegex(opts.negationSymbol.back) + "?"
  268. : "") +
  269. "$)"
  270. ).exec(buffer.slice().reverse().join("")),
  271. number = numberMatches ? numberMatches[2] : "",
  272. leadingzeroes = false;
  273. if (number) {
  274. number = number.split(opts.radixPoint.charAt(0))[0];
  275. leadingzeroes = new RegExp("^[0" + opts.groupSeparator + "]*").exec(number);
  276. }
  277. return leadingzeroes &&
  278. (leadingzeroes[0].length > 1 ||
  279. (leadingzeroes[0].length > 0 && leadingzeroes[0].length < number.length))
  280. ? leadingzeroes
  281. : false;
  282. }
  283. // number aliases
  284. Inputmask.extendAliases({
  285. numeric: {
  286. mask: genMask,
  287. _mask: function (opts) {
  288. return "(" + opts.groupSeparator + "999){+|1}";
  289. },
  290. digits: "*", // number of fractionalDigits
  291. digitsOptional: true,
  292. enforceDigitsOnBlur: false,
  293. radixPoint: ".",
  294. positionCaretOnClick: "radixFocus",
  295. _radixDance: true,
  296. groupSeparator: "",
  297. allowMinus: true,
  298. negationSymbol: {
  299. front: "-", // "("
  300. back: "" // ")"
  301. },
  302. prefix: "",
  303. suffix: "",
  304. min: null, // minimum value
  305. max: null, // maximum value
  306. SetMaxOnOverflow: false,
  307. step: 1,
  308. inputType: "text", // number ~ specify that values which are set are in textform (radix point is same as in the options) or in numberform (radixpoint = .)
  309. unmaskAsNumber: false,
  310. roundingFN: Math.round, // Math.floor , fn(x)
  311. inputmode: "decimal",
  312. shortcuts: { k: "1000", m: "1000000" },
  313. // global options
  314. placeholder: "0",
  315. greedy: false,
  316. rightAlign: true,
  317. insertMode: true,
  318. autoUnmask: false,
  319. skipOptionalPartCharacter: "",
  320. usePrototypeDefinitions: false,
  321. stripLeadingZeroes: true,
  322. substituteRadixPoint: true,
  323. definitions: {
  324. 0: {
  325. validator: decimalValidator
  326. },
  327. 1: {
  328. validator: decimalValidator,
  329. definitionSymbol: "9"
  330. },
  331. 9: {
  332. // \uFF11-\uFF19 #1606
  333. validator: "[0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9]",
  334. definitionSymbol: "*"
  335. },
  336. "+": {
  337. validator: function (chrs, maskset, pos, strict, opts) {
  338. return (
  339. opts.allowMinus &&
  340. (chrs === "-" || chrs === opts.negationSymbol.front)
  341. );
  342. }
  343. },
  344. "-": {
  345. validator: function (chrs, maskset, pos, strict, opts) {
  346. return opts.allowMinus && chrs === opts.negationSymbol.back;
  347. }
  348. }
  349. },
  350. preValidation: function (
  351. buffer,
  352. pos,
  353. c,
  354. isSelection,
  355. opts,
  356. maskset,
  357. caretPos,
  358. strict
  359. ) {
  360. const inputmask = this;
  361. if (opts.__financeInput !== false && c === opts.radixPoint) return false;
  362. const radixPos = buffer.indexOf(opts.radixPoint),
  363. initPos = pos;
  364. pos = hanndleRadixDance(pos, c, radixPos, maskset, opts);
  365. if (c === "-" || c === opts.negationSymbol.front) {
  366. if (opts.allowMinus !== true) return false;
  367. let isNegative = false,
  368. front = findValid("+", maskset),
  369. back = findValid("-", maskset);
  370. if (front !== -1) {
  371. isNegative = [front];
  372. if (back !== -1) isNegative.push(back);
  373. }
  374. return isNegative !== false
  375. ? {
  376. remove: isNegative,
  377. caret: initPos - opts.negationSymbol.back.length
  378. }
  379. : {
  380. insert: [
  381. {
  382. pos: findValidator.call(inputmask, "+", maskset),
  383. c: opts.negationSymbol.front,
  384. fromIsValid: true
  385. },
  386. {
  387. pos: findValidator.call(inputmask, "-", maskset),
  388. c: opts.negationSymbol.back,
  389. fromIsValid: undefined
  390. }
  391. ],
  392. caret: initPos + opts.negationSymbol.back.length
  393. };
  394. }
  395. if (c === opts.groupSeparator) {
  396. return { caret: initPos };
  397. }
  398. if (strict) return true;
  399. if (
  400. radixPos !== -1 &&
  401. opts._radixDance === true &&
  402. isSelection === false &&
  403. c === opts.radixPoint &&
  404. opts.digits !== undefined &&
  405. (isNaN(opts.digits) || parseInt(opts.digits) > 0) &&
  406. radixPos !== pos
  407. ) {
  408. const radixValidatorPos = findValidator.call(
  409. inputmask,
  410. opts.radixPoint,
  411. maskset
  412. );
  413. if (maskset.validPositions[radixValidatorPos]) {
  414. maskset.validPositions[radixValidatorPos].generatedInput =
  415. maskset.validPositions[radixValidatorPos].generated || false;
  416. }
  417. return {
  418. caret:
  419. opts._radixDance && pos === radixPos - 1 ? radixPos + 1 : radixPos
  420. };
  421. }
  422. if (opts.__financeInput === false) {
  423. if (isSelection) {
  424. if (opts.digitsOptional) {
  425. return { rewritePosition: caretPos.end };
  426. } else if (!opts.digitsOptional) {
  427. if (caretPos.begin > radixPos && caretPos.end <= radixPos) {
  428. if (c === opts.radixPoint) {
  429. return {
  430. insert: { pos: radixPos + 1, c: "0", fromIsValid: true },
  431. rewritePosition: radixPos
  432. };
  433. } else {
  434. return { rewritePosition: radixPos + 1 };
  435. }
  436. } else if (caretPos.begin < radixPos) {
  437. return { rewritePosition: caretPos.begin - 1 };
  438. }
  439. }
  440. } else if (
  441. !opts.showMaskOnHover &&
  442. !opts.showMaskOnFocus &&
  443. !opts.digitsOptional &&
  444. opts.digits > 0 &&
  445. this.__valueGet.call(this.el) === ""
  446. ) {
  447. return { rewritePosition: radixPos };
  448. }
  449. }
  450. return { rewritePosition: pos };
  451. },
  452. postValidation: function (
  453. buffer,
  454. pos,
  455. c,
  456. currentResult,
  457. opts,
  458. maskset,
  459. strict
  460. ) {
  461. if (currentResult === false) return currentResult;
  462. if (strict) return true;
  463. if (opts.min !== null || opts.max !== null) {
  464. const unmasked = opts.onUnMask(
  465. buffer.slice().reverse().join(""),
  466. undefined,
  467. $.extend({}, opts, {
  468. unmaskAsNumber: true
  469. })
  470. );
  471. if (
  472. opts.min !== null &&
  473. unmasked < opts.min &&
  474. (unmasked.toString().length > opts.min.toString().length ||
  475. unmasked < 0)
  476. ) {
  477. return false;
  478. // return {
  479. // refreshFromBuffer: true,
  480. // buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
  481. // };
  482. }
  483. if (opts.max !== null && unmasked > opts.max) {
  484. return opts.SetMaxOnOverflow
  485. ? {
  486. refreshFromBuffer: true,
  487. buffer: alignDigits(
  488. opts.max.toString().replace(".", opts.radixPoint).split(""),
  489. opts.digits,
  490. opts
  491. ).reverse()
  492. }
  493. : false;
  494. }
  495. }
  496. return currentResult;
  497. },
  498. onUnMask: function (maskedValue, unmaskedValue, opts) {
  499. if (unmaskedValue === "" && opts.nullable === true) {
  500. return unmaskedValue;
  501. }
  502. let processValue = maskedValue.replace(opts.prefix, "");
  503. processValue = processValue.replace(opts.suffix, "");
  504. processValue = processValue.replace(
  505. new RegExp(escapeRegex(opts.groupSeparator), "g"),
  506. ""
  507. );
  508. if (opts.placeholder.charAt(0) !== "") {
  509. processValue = processValue.replace(
  510. new RegExp(opts.placeholder.charAt(0), "g"),
  511. "0"
  512. );
  513. }
  514. if (opts.unmaskAsNumber) {
  515. if (
  516. opts.radixPoint !== "" &&
  517. processValue.indexOf(opts.radixPoint) !== -1
  518. )
  519. processValue = processValue.replace(
  520. escapeRegex.call(this, opts.radixPoint),
  521. "."
  522. );
  523. processValue = processValue.replace(
  524. new RegExp("^" + escapeRegex(opts.negationSymbol.front)),
  525. "-"
  526. );
  527. processValue = processValue.replace(
  528. new RegExp(escapeRegex(opts.negationSymbol.back) + "$"),
  529. ""
  530. );
  531. return Number(processValue);
  532. }
  533. return processValue;
  534. },
  535. isComplete: function (buffer, opts) {
  536. let maskedValue = (
  537. opts.numericInput ? buffer.slice().reverse() : buffer
  538. ).join("");
  539. maskedValue = maskedValue.replace(
  540. new RegExp("^" + escapeRegex(opts.negationSymbol.front)),
  541. "-"
  542. );
  543. maskedValue = maskedValue.replace(
  544. new RegExp(escapeRegex(opts.negationSymbol.back) + "$"),
  545. ""
  546. );
  547. maskedValue = maskedValue.replace(opts.prefix, "");
  548. maskedValue = maskedValue.replace(opts.suffix, "");
  549. maskedValue = maskedValue.replace(
  550. new RegExp(escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"),
  551. "$1"
  552. );
  553. if (opts.radixPoint === ",")
  554. maskedValue = maskedValue.replace(escapeRegex(opts.radixPoint), ".");
  555. return isFinite(maskedValue);
  556. },
  557. onBeforeMask: function (initialValue, opts) {
  558. initialValue = initialValue ?? "";
  559. const radixPoint = opts.radixPoint || ",";
  560. if (isFinite(opts.digits)) opts.digits = parseInt(opts.digits);
  561. if (
  562. (typeof initialValue === "number" || opts.inputType === "number") &&
  563. radixPoint !== ""
  564. ) {
  565. initialValue = initialValue.toString().replace(".", radixPoint);
  566. }
  567. const isNegative =
  568. initialValue.charAt(0) === "-" ||
  569. initialValue.charAt(0) === opts.negationSymbol.front,
  570. valueParts = initialValue.split(radixPoint),
  571. integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
  572. decimalPart =
  573. valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "",
  574. forceDigits = valueParts.length > 1;
  575. initialValue =
  576. integerPart +
  577. (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
  578. let digits = 0;
  579. if (radixPoint !== "") {
  580. digits = !opts.digitsOptional
  581. ? opts.digits
  582. : opts.digits < decimalPart.length
  583. ? opts.digits
  584. : decimalPart.length;
  585. if (decimalPart !== "" || !opts.digitsOptional) {
  586. const digitsFactor = Math.pow(10, digits || 1);
  587. // make the initialValue a valid javascript number for the parsefloat
  588. initialValue = initialValue.replace(escapeRegex(radixPoint), ".");
  589. if (!isNaN(parseFloat(initialValue))) {
  590. initialValue = (
  591. opts.roundingFN(parseFloat(initialValue) * digitsFactor) /
  592. digitsFactor
  593. ).toFixed(digits);
  594. }
  595. initialValue = initialValue.toString().replace(".", radixPoint);
  596. }
  597. }
  598. // this needs to be in a separate part and not directly in decimalPart to allow rounding
  599. if (opts.digits === 0 && initialValue.indexOf(radixPoint) !== -1) {
  600. initialValue = initialValue.substring(
  601. 0,
  602. initialValue.indexOf(radixPoint)
  603. );
  604. }
  605. if (opts.min !== null || opts.max !== null) {
  606. const numberValue = initialValue.toString().replace(radixPoint, ".");
  607. if (opts.min !== null && numberValue < opts.min) {
  608. initialValue = opts.min.toString().replace(".", radixPoint);
  609. } else if (opts.max !== null && numberValue > opts.max) {
  610. initialValue = opts.max.toString().replace(".", radixPoint);
  611. }
  612. }
  613. if (isNegative && initialValue.charAt(0) !== "-") {
  614. initialValue = "-" + initialValue;
  615. }
  616. return alignDigits(
  617. initialValue.toString().split(""),
  618. digits,
  619. opts,
  620. forceDigits
  621. ).join("");
  622. },
  623. onBeforeWrite: function (e, buffer, caretPos, opts) {
  624. function stripBuffer(buffer, stripRadix) {
  625. if (opts.__financeInput !== false || stripRadix) {
  626. var position = buffer.indexOf(opts.radixPoint);
  627. if (position !== -1) {
  628. buffer.splice(position, 1);
  629. }
  630. }
  631. if (opts.groupSeparator !== "") {
  632. while ((position = buffer.indexOf(opts.groupSeparator)) !== -1) {
  633. buffer.splice(position, 1);
  634. }
  635. }
  636. return buffer;
  637. }
  638. let result, leadingzeroes;
  639. if (
  640. opts.stripLeadingZeroes &&
  641. (leadingzeroes = checkForLeadingZeroes(buffer, opts))
  642. ) {
  643. const caretNdx =
  644. buffer
  645. .join("")
  646. .lastIndexOf(leadingzeroes[0].split("").reverse().join("")) -
  647. (leadingzeroes[0] == leadingzeroes.input ? 0 : 1),
  648. offset = leadingzeroes[0] == leadingzeroes.input ? 1 : 0;
  649. for (let i = leadingzeroes[0].length - offset; i > 0; i--) {
  650. this.maskset.validPositions.splice(caretNdx + i, 1);
  651. delete buffer[caretNdx + i];
  652. }
  653. }
  654. if (e) {
  655. switch (e.type) {
  656. case "blur":
  657. case "checkval":
  658. if (opts.min !== null) {
  659. const unmasked = opts.onUnMask(
  660. buffer.slice().reverse().join(""),
  661. undefined,
  662. $.extend({}, opts, {
  663. unmaskAsNumber: true
  664. })
  665. );
  666. if (opts.min !== null && unmasked < opts.min) {
  667. return {
  668. refreshFromBuffer: true,
  669. buffer: alignDigits(
  670. opts.min.toString().replace(".", opts.radixPoint).split(""),
  671. opts.digits,
  672. opts
  673. ).reverse()
  674. };
  675. }
  676. }
  677. if (buffer[buffer.length - 1] === opts.negationSymbol.front) {
  678. // strip negation symbol on blur when value is 0
  679. const nmbrMtchs = new RegExp(
  680. "(^" +
  681. (opts.negationSymbol.front != ""
  682. ? escapeRegex(opts.negationSymbol.front) + "?"
  683. : "") +
  684. escapeRegex(opts.prefix) +
  685. ")(.*)(" +
  686. escapeRegex(opts.suffix) +
  687. (opts.negationSymbol.back != ""
  688. ? escapeRegex(opts.negationSymbol.back) + "?"
  689. : "") +
  690. "$)"
  691. ).exec(stripBuffer(buffer.slice(), true).reverse().join("")),
  692. number = nmbrMtchs ? nmbrMtchs[2] : "";
  693. if (number == 0) {
  694. result = { refreshFromBuffer: true, buffer: [0] };
  695. }
  696. } else if (opts.radixPoint !== "") {
  697. // strip radixpoint on blur when it is the latest char
  698. const radixNDX = buffer.indexOf(opts.radixPoint);
  699. if (radixNDX === opts.suffix.length) {
  700. if (result && result.buffer) {
  701. result.buffer.splice(0, 1 + opts.suffix.length);
  702. } else {
  703. buffer.splice(0, 1 + opts.suffix.length);
  704. result = {
  705. refreshFromBuffer: true,
  706. buffer: stripBuffer(buffer)
  707. };
  708. }
  709. }
  710. }
  711. if (opts.enforceDigitsOnBlur) {
  712. result = result || {};
  713. const bffr =
  714. (result && result.buffer) || buffer.slice().reverse();
  715. result.refreshFromBuffer = true;
  716. result.buffer = alignDigits(
  717. bffr,
  718. opts.digits,
  719. opts,
  720. true
  721. ).reverse();
  722. }
  723. }
  724. }
  725. return result;
  726. },
  727. onKeyDown: function (e, buffer, caretPos, opts) {
  728. let $input = $(this),
  729. bffr;
  730. if (e.location != 3) {
  731. let pattern,
  732. c = e.key;
  733. if ((pattern = opts.shortcuts && opts.shortcuts[c])) {
  734. if (pattern.length > 1) {
  735. this.inputmask.__valueSet.call(
  736. this,
  737. parseFloat(this.inputmask.unmaskedvalue()) * parseInt(pattern)
  738. );
  739. $input.trigger("setvalue");
  740. return false;
  741. }
  742. }
  743. }
  744. if (e.ctrlKey) {
  745. switch (e.key) {
  746. case keys.ArrowUp:
  747. this.inputmask.__valueSet.call(
  748. this,
  749. parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step)
  750. );
  751. $input.trigger("setvalue");
  752. return false;
  753. case keys.ArrowDown:
  754. this.inputmask.__valueSet.call(
  755. this,
  756. parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step)
  757. );
  758. $input.trigger("setvalue");
  759. return false;
  760. }
  761. }
  762. if (
  763. !e.shiftKey &&
  764. (e.key === keys.Delete ||
  765. e.key === keys.Backspace ||
  766. e.key === keys.BACKSPACE_SAFARI) &&
  767. caretPos.begin !== buffer.length
  768. ) {
  769. if (
  770. buffer[e.key === keys.Delete ? caretPos.begin - 1 : caretPos.end] ===
  771. opts.negationSymbol.front
  772. ) {
  773. bffr = buffer.slice().reverse();
  774. if (opts.negationSymbol.front !== "") bffr.shift();
  775. if (opts.negationSymbol.back !== "") bffr.pop();
  776. $input.trigger("setvalue", [bffr.join(""), caretPos.begin]);
  777. return false;
  778. } else if (opts._radixDance === true) {
  779. const radixPos = buffer.indexOf(opts.radixPoint);
  780. if (!opts.digitsOptional) {
  781. if (
  782. radixPos !== -1 &&
  783. (caretPos.begin < radixPos ||
  784. caretPos.end < radixPos ||
  785. (e.key === keys.Delete &&
  786. (caretPos.begin === radixPos ||
  787. caretPos.begin - 1 === radixPos)))
  788. ) {
  789. let restoreCaretPos;
  790. if (caretPos.begin === caretPos.end) {
  791. // only adjust when not a selection
  792. if (e.key === keys.Backspace || e.key === keys.BACKSPACE_SAFARI)
  793. caretPos.begin++;
  794. else if (
  795. e.key === keys.Delete &&
  796. caretPos.begin - 1 === radixPos
  797. ) {
  798. restoreCaretPos = $.extend({}, caretPos);
  799. caretPos.begin--;
  800. caretPos.end--;
  801. }
  802. }
  803. bffr = buffer.slice().reverse();
  804. bffr.splice(
  805. bffr.length - caretPos.begin,
  806. caretPos.begin - caretPos.end + 1
  807. );
  808. // console.log(caretPos);
  809. bffr = alignDigits(bffr, opts.digits, opts).join("");
  810. if (restoreCaretPos) {
  811. caretPos = restoreCaretPos;
  812. }
  813. $input.trigger("setvalue", [
  814. bffr,
  815. caretPos.begin >= bffr.length ? radixPos + 1 : caretPos.begin
  816. ]);
  817. return false;
  818. }
  819. } else if (radixPos === 0) {
  820. bffr = buffer.slice().reverse();
  821. bffr.pop();
  822. $input.trigger("setvalue", [
  823. bffr.join(""),
  824. caretPos.begin >= bffr.length ? bffr.length : caretPos.begin
  825. ]);
  826. return false;
  827. }
  828. }
  829. }
  830. }
  831. },
  832. currency: {
  833. prefix: "", // "$ ",
  834. groupSeparator: ",",
  835. alias: "numeric",
  836. digits: 2,
  837. digitsOptional: false
  838. },
  839. decimal: {
  840. alias: "numeric"
  841. },
  842. integer: {
  843. alias: "numeric",
  844. inputmode: "numeric",
  845. digits: 0
  846. },
  847. percentage: {
  848. alias: "numeric",
  849. min: 0,
  850. max: 100,
  851. suffix: " %",
  852. digits: 0,
  853. allowMinus: false
  854. },
  855. indianns: {
  856. // indian numbering system
  857. alias: "numeric",
  858. _mask: function (opts) {
  859. return (
  860. "(" +
  861. opts.groupSeparator +
  862. "99){*|1}(" +
  863. opts.groupSeparator +
  864. "999){1|1}"
  865. );
  866. },
  867. groupSeparator: ",",
  868. radixPoint: ".",
  869. placeholder: "0",
  870. digits: 2,
  871. digitsOptional: false
  872. }
  873. });