inputmask.numeric.extensions.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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, back];
  372. }
  373. return isNegative !== false
  374. ? {
  375. remove: isNegative,
  376. caret: initPos - opts.negationSymbol.back.length
  377. }
  378. : {
  379. insert: [
  380. {
  381. pos: findValidator.call(inputmask, "+", maskset),
  382. c: opts.negationSymbol.front,
  383. fromIsValid: true
  384. },
  385. {
  386. pos: findValidator.call(inputmask, "-", maskset),
  387. c: opts.negationSymbol.back,
  388. fromIsValid: undefined
  389. }
  390. ],
  391. caret: initPos + opts.negationSymbol.back.length
  392. };
  393. }
  394. if (c === opts.groupSeparator) {
  395. return { caret: initPos };
  396. }
  397. if (strict) return true;
  398. if (
  399. radixPos !== -1 &&
  400. opts._radixDance === true &&
  401. isSelection === false &&
  402. c === opts.radixPoint &&
  403. opts.digits !== undefined &&
  404. (isNaN(opts.digits) || parseInt(opts.digits) > 0) &&
  405. radixPos !== pos
  406. ) {
  407. const radixValidatorPos = findValidator.call(
  408. inputmask,
  409. opts.radixPoint,
  410. maskset
  411. );
  412. if (maskset.validPositions[radixValidatorPos]) {
  413. maskset.validPositions[radixValidatorPos].generatedInput =
  414. maskset.validPositions[radixValidatorPos].generated || false;
  415. }
  416. return {
  417. caret:
  418. opts._radixDance && pos === radixPos - 1 ? radixPos + 1 : radixPos
  419. };
  420. }
  421. if (opts.__financeInput === false) {
  422. if (isSelection) {
  423. if (opts.digitsOptional) {
  424. return { rewritePosition: caretPos.end };
  425. } else if (!opts.digitsOptional) {
  426. if (caretPos.begin > radixPos && caretPos.end <= radixPos) {
  427. if (c === opts.radixPoint) {
  428. return {
  429. insert: { pos: radixPos + 1, c: "0", fromIsValid: true },
  430. rewritePosition: radixPos
  431. };
  432. } else {
  433. return { rewritePosition: radixPos + 1 };
  434. }
  435. } else if (caretPos.begin < radixPos) {
  436. return { rewritePosition: caretPos.begin - 1 };
  437. }
  438. }
  439. } else if (
  440. !opts.showMaskOnHover &&
  441. !opts.showMaskOnFocus &&
  442. !opts.digitsOptional &&
  443. opts.digits > 0 &&
  444. this.__valueGet.call(this.el) === ""
  445. ) {
  446. return { rewritePosition: radixPos };
  447. }
  448. }
  449. return { rewritePosition: pos };
  450. },
  451. postValidation: function (
  452. buffer,
  453. pos,
  454. c,
  455. currentResult,
  456. opts,
  457. maskset,
  458. strict
  459. ) {
  460. if (currentResult === false) return currentResult;
  461. if (strict) return true;
  462. if (opts.min !== null || opts.max !== null) {
  463. const unmasked = opts.onUnMask(
  464. buffer.slice().reverse().join(""),
  465. undefined,
  466. $.extend({}, opts, {
  467. unmaskAsNumber: true
  468. })
  469. );
  470. if (
  471. opts.min !== null &&
  472. unmasked < opts.min &&
  473. (unmasked.toString().length > opts.min.toString().length ||
  474. unmasked < 0)
  475. ) {
  476. return false;
  477. // return {
  478. // refreshFromBuffer: true,
  479. // buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
  480. // };
  481. }
  482. if (opts.max !== null && unmasked > opts.max) {
  483. return opts.SetMaxOnOverflow
  484. ? {
  485. refreshFromBuffer: true,
  486. buffer: alignDigits(
  487. opts.max.toString().replace(".", opts.radixPoint).split(""),
  488. opts.digits,
  489. opts
  490. ).reverse()
  491. }
  492. : false;
  493. }
  494. }
  495. return currentResult;
  496. },
  497. onUnMask: function (maskedValue, unmaskedValue, opts) {
  498. if (unmaskedValue === "" && opts.nullable === true) {
  499. return unmaskedValue;
  500. }
  501. let processValue = maskedValue.replace(opts.prefix, "");
  502. processValue = processValue.replace(opts.suffix, "");
  503. processValue = processValue.replace(
  504. new RegExp(escapeRegex(opts.groupSeparator), "g"),
  505. ""
  506. );
  507. if (opts.placeholder.charAt(0) !== "") {
  508. processValue = processValue.replace(
  509. new RegExp(opts.placeholder.charAt(0), "g"),
  510. "0"
  511. );
  512. }
  513. if (opts.unmaskAsNumber) {
  514. if (
  515. opts.radixPoint !== "" &&
  516. processValue.indexOf(opts.radixPoint) !== -1
  517. )
  518. processValue = processValue.replace(
  519. escapeRegex.call(this, opts.radixPoint),
  520. "."
  521. );
  522. processValue = processValue.replace(
  523. new RegExp("^" + escapeRegex(opts.negationSymbol.front)),
  524. "-"
  525. );
  526. processValue = processValue.replace(
  527. new RegExp(escapeRegex(opts.negationSymbol.back) + "$"),
  528. ""
  529. );
  530. return Number(processValue);
  531. }
  532. return processValue;
  533. },
  534. isComplete: function (buffer, opts) {
  535. let maskedValue = (
  536. opts.numericInput ? buffer.slice().reverse() : buffer
  537. ).join("");
  538. maskedValue = maskedValue.replace(
  539. new RegExp("^" + escapeRegex(opts.negationSymbol.front)),
  540. "-"
  541. );
  542. maskedValue = maskedValue.replace(
  543. new RegExp(escapeRegex(opts.negationSymbol.back) + "$"),
  544. ""
  545. );
  546. maskedValue = maskedValue.replace(opts.prefix, "");
  547. maskedValue = maskedValue.replace(opts.suffix, "");
  548. maskedValue = maskedValue.replace(
  549. new RegExp(escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"),
  550. "$1"
  551. );
  552. if (opts.radixPoint === ",")
  553. maskedValue = maskedValue.replace(escapeRegex(opts.radixPoint), ".");
  554. return isFinite(maskedValue);
  555. },
  556. onBeforeMask: function (initialValue, opts) {
  557. initialValue = initialValue ?? "";
  558. const radixPoint = opts.radixPoint || ",";
  559. if (isFinite(opts.digits)) opts.digits = parseInt(opts.digits);
  560. if (
  561. (typeof initialValue === "number" || opts.inputType === "number") &&
  562. radixPoint !== ""
  563. ) {
  564. initialValue = initialValue.toString().replace(".", radixPoint);
  565. }
  566. const isNegative =
  567. initialValue.charAt(0) === "-" ||
  568. initialValue.charAt(0) === opts.negationSymbol.front,
  569. valueParts = initialValue.split(radixPoint),
  570. integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
  571. decimalPart =
  572. valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "",
  573. forceDigits = valueParts.length > 1;
  574. initialValue =
  575. integerPart +
  576. (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
  577. let digits = 0;
  578. if (radixPoint !== "") {
  579. digits = !opts.digitsOptional
  580. ? opts.digits
  581. : opts.digits < decimalPart.length
  582. ? opts.digits
  583. : decimalPart.length;
  584. if (decimalPart !== "" || !opts.digitsOptional) {
  585. const digitsFactor = Math.pow(10, digits || 1);
  586. // make the initialValue a valid javascript number for the parsefloat
  587. initialValue = initialValue.replace(escapeRegex(radixPoint), ".");
  588. if (!isNaN(parseFloat(initialValue))) {
  589. initialValue = (
  590. opts.roundingFN(parseFloat(initialValue) * digitsFactor) /
  591. digitsFactor
  592. ).toFixed(digits);
  593. }
  594. initialValue = initialValue.toString().replace(".", radixPoint);
  595. }
  596. }
  597. // this needs to be in a separate part and not directly in decimalPart to allow rounding
  598. if (opts.digits === 0 && initialValue.indexOf(radixPoint) !== -1) {
  599. initialValue = initialValue.substring(
  600. 0,
  601. initialValue.indexOf(radixPoint)
  602. );
  603. }
  604. if (opts.min !== null || opts.max !== null) {
  605. const numberValue = initialValue.toString().replace(radixPoint, ".");
  606. if (opts.min !== null && numberValue < opts.min) {
  607. initialValue = opts.min.toString().replace(".", radixPoint);
  608. } else if (opts.max !== null && numberValue > opts.max) {
  609. initialValue = opts.max.toString().replace(".", radixPoint);
  610. }
  611. }
  612. if (isNegative && initialValue.charAt(0) !== "-") {
  613. initialValue = "-" + initialValue;
  614. }
  615. return alignDigits(
  616. initialValue.toString().split(""),
  617. digits,
  618. opts,
  619. forceDigits
  620. ).join("");
  621. },
  622. onBeforeWrite: function (e, buffer, caretPos, opts) {
  623. function stripBuffer(buffer, stripRadix) {
  624. if (opts.__financeInput !== false || stripRadix) {
  625. var position = buffer.indexOf(opts.radixPoint);
  626. if (position !== -1) {
  627. buffer.splice(position, 1);
  628. }
  629. }
  630. if (opts.groupSeparator !== "") {
  631. while ((position = buffer.indexOf(opts.groupSeparator)) !== -1) {
  632. buffer.splice(position, 1);
  633. }
  634. }
  635. return buffer;
  636. }
  637. let result, leadingzeroes;
  638. if (
  639. opts.stripLeadingZeroes &&
  640. (leadingzeroes = checkForLeadingZeroes(buffer, opts))
  641. ) {
  642. const caretNdx =
  643. buffer
  644. .join("")
  645. .lastIndexOf(leadingzeroes[0].split("").reverse().join("")) -
  646. (leadingzeroes[0] == leadingzeroes.input ? 0 : 1),
  647. offset = leadingzeroes[0] == leadingzeroes.input ? 1 : 0;
  648. for (let i = leadingzeroes[0].length - offset; i > 0; i--) {
  649. this.maskset.validPositions.splice(caretNdx + i, 1);
  650. delete buffer[caretNdx + i];
  651. }
  652. }
  653. if (e) {
  654. switch (e.type) {
  655. case "blur":
  656. case "checkval":
  657. if (opts.min !== null) {
  658. const unmasked = opts.onUnMask(
  659. buffer.slice().reverse().join(""),
  660. undefined,
  661. $.extend({}, opts, {
  662. unmaskAsNumber: true
  663. })
  664. );
  665. if (opts.min !== null && unmasked < opts.min) {
  666. return {
  667. refreshFromBuffer: true,
  668. buffer: alignDigits(
  669. opts.min.toString().replace(".", opts.radixPoint).split(""),
  670. opts.digits,
  671. opts
  672. ).reverse()
  673. };
  674. }
  675. }
  676. if (buffer[buffer.length - 1] === opts.negationSymbol.front) {
  677. // strip negation symbol on blur when value is 0
  678. const nmbrMtchs = new RegExp(
  679. "(^" +
  680. (opts.negationSymbol.front != ""
  681. ? escapeRegex(opts.negationSymbol.front) + "?"
  682. : "") +
  683. escapeRegex(opts.prefix) +
  684. ")(.*)(" +
  685. escapeRegex(opts.suffix) +
  686. (opts.negationSymbol.back != ""
  687. ? escapeRegex(opts.negationSymbol.back) + "?"
  688. : "") +
  689. "$)"
  690. ).exec(stripBuffer(buffer.slice(), true).reverse().join("")),
  691. number = nmbrMtchs ? nmbrMtchs[2] : "";
  692. if (number == 0) {
  693. result = { refreshFromBuffer: true, buffer: [0] };
  694. }
  695. } else if (opts.radixPoint !== "") {
  696. // strip radixpoint on blur when it is the latest char
  697. const radixNDX = buffer.indexOf(opts.radixPoint);
  698. if (radixNDX === opts.suffix.length) {
  699. if (result && result.buffer) {
  700. result.buffer.splice(0, 1 + opts.suffix.length);
  701. } else {
  702. buffer.splice(0, 1 + opts.suffix.length);
  703. result = {
  704. refreshFromBuffer: true,
  705. buffer: stripBuffer(buffer)
  706. };
  707. }
  708. }
  709. }
  710. if (opts.enforceDigitsOnBlur) {
  711. result = result || {};
  712. const bffr =
  713. (result && result.buffer) || buffer.slice().reverse();
  714. result.refreshFromBuffer = true;
  715. result.buffer = alignDigits(
  716. bffr,
  717. opts.digits,
  718. opts,
  719. true
  720. ).reverse();
  721. }
  722. }
  723. }
  724. return result;
  725. },
  726. onKeyDown: function (e, buffer, caretPos, opts) {
  727. let $input = $(this),
  728. bffr;
  729. if (e.location != 3) {
  730. let pattern,
  731. c = e.key;
  732. if ((pattern = opts.shortcuts && opts.shortcuts[c])) {
  733. if (pattern.length > 1) {
  734. this.inputmask.__valueSet.call(
  735. this,
  736. parseFloat(this.inputmask.unmaskedvalue()) * parseInt(pattern)
  737. );
  738. $input.trigger("setvalue");
  739. return false;
  740. }
  741. }
  742. }
  743. if (e.ctrlKey) {
  744. switch (e.key) {
  745. case keys.ArrowUp:
  746. this.inputmask.__valueSet.call(
  747. this,
  748. parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step)
  749. );
  750. $input.trigger("setvalue");
  751. return false;
  752. case keys.ArrowDown:
  753. this.inputmask.__valueSet.call(
  754. this,
  755. parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step)
  756. );
  757. $input.trigger("setvalue");
  758. return false;
  759. }
  760. }
  761. if (
  762. !e.shiftKey &&
  763. (e.key === keys.Delete ||
  764. e.key === keys.Backspace ||
  765. e.key === keys.BACKSPACE_SAFARI) &&
  766. caretPos.begin !== buffer.length
  767. ) {
  768. if (
  769. buffer[e.key === keys.Delete ? caretPos.begin - 1 : caretPos.end] ===
  770. opts.negationSymbol.front
  771. ) {
  772. bffr = buffer.slice().reverse();
  773. if (opts.negationSymbol.front !== "") bffr.shift();
  774. if (opts.negationSymbol.back !== "") bffr.pop();
  775. $input.trigger("setvalue", [bffr.join(""), caretPos.begin]);
  776. return false;
  777. } else if (opts._radixDance === true) {
  778. const radixPos = buffer.indexOf(opts.radixPoint);
  779. if (!opts.digitsOptional) {
  780. if (
  781. radixPos !== -1 &&
  782. (caretPos.begin < radixPos ||
  783. caretPos.end < radixPos ||
  784. (e.key === keys.Delete &&
  785. (caretPos.begin === radixPos ||
  786. caretPos.begin - 1 === radixPos)))
  787. ) {
  788. let restoreCaretPos;
  789. if (caretPos.begin === caretPos.end) {
  790. // only adjust when not a selection
  791. if (e.key === keys.Backspace || e.key === keys.BACKSPACE_SAFARI)
  792. caretPos.begin++;
  793. else if (
  794. e.key === keys.Delete &&
  795. caretPos.begin - 1 === radixPos
  796. ) {
  797. restoreCaretPos = $.extend({}, caretPos);
  798. caretPos.begin--;
  799. caretPos.end--;
  800. }
  801. }
  802. bffr = buffer.slice().reverse();
  803. bffr.splice(
  804. bffr.length - caretPos.begin,
  805. caretPos.begin - caretPos.end + 1
  806. );
  807. // console.log(caretPos);
  808. bffr = alignDigits(bffr, opts.digits, opts).join("");
  809. if (restoreCaretPos) {
  810. caretPos = restoreCaretPos;
  811. }
  812. $input.trigger("setvalue", [
  813. bffr,
  814. caretPos.begin >= bffr.length ? radixPos + 1 : caretPos.begin
  815. ]);
  816. return false;
  817. }
  818. } else if (radixPos === 0) {
  819. bffr = buffer.slice().reverse();
  820. bffr.pop();
  821. $input.trigger("setvalue", [
  822. bffr.join(""),
  823. caretPos.begin >= bffr.length ? bffr.length : caretPos.begin
  824. ]);
  825. return false;
  826. }
  827. }
  828. }
  829. }
  830. },
  831. currency: {
  832. prefix: "", // "$ ",
  833. groupSeparator: ",",
  834. alias: "numeric",
  835. digits: 2,
  836. digitsOptional: false
  837. },
  838. decimal: {
  839. alias: "numeric"
  840. },
  841. integer: {
  842. alias: "numeric",
  843. inputmode: "numeric",
  844. digits: 0
  845. },
  846. percentage: {
  847. alias: "numeric",
  848. min: 0,
  849. max: 100,
  850. suffix: " %",
  851. digits: 0,
  852. allowMinus: false
  853. },
  854. indianns: {
  855. // indian numbering system
  856. alias: "numeric",
  857. _mask: function (opts) {
  858. return (
  859. "(" +
  860. opts.groupSeparator +
  861. "99){*|1}(" +
  862. opts.groupSeparator +
  863. "999){1|1}"
  864. );
  865. },
  866. groupSeparator: ",",
  867. radixPoint: ".",
  868. placeholder: "0",
  869. digits: 2,
  870. digitsOptional: false
  871. }
  872. });