inputmask.numeric.extensions.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. opts._radixDance &&
  241. radixPos !== -1 &&
  242. result &&
  243. maskset.validPositions[radixPos] == undefined
  244. ) {
  245. return {
  246. insert: {
  247. pos: radixPos === pos ? radixPos + 1 : radixPos,
  248. c: opts.radixPoint
  249. },
  250. pos
  251. };
  252. }
  253. return result;
  254. }
  255. function checkForLeadingZeroes(buffer, opts) {
  256. // check leading zeros
  257. let numberMatches = new RegExp(
  258. "(^" +
  259. (opts.negationSymbol.front !== ""
  260. ? escapeRegex(opts.negationSymbol.front) + "?"
  261. : "") +
  262. escapeRegex(opts.prefix) +
  263. ")(.*)(" +
  264. escapeRegex(opts.suffix) +
  265. (opts.negationSymbol.back != ""
  266. ? escapeRegex(opts.negationSymbol.back) + "?"
  267. : "") +
  268. "$)"
  269. ).exec(buffer.slice().reverse().join("")),
  270. number = numberMatches ? numberMatches[2] : "",
  271. leadingzeroes = false;
  272. if (number) {
  273. number = number.split(opts.radixPoint.charAt(0))[0];
  274. leadingzeroes = new RegExp("^[0" + opts.groupSeparator + "]*").exec(number);
  275. }
  276. return leadingzeroes &&
  277. (leadingzeroes[0].length > 1 ||
  278. (leadingzeroes[0].length > 0 && leadingzeroes[0].length < number.length))
  279. ? leadingzeroes
  280. : false;
  281. }
  282. // number aliases
  283. Inputmask.extendAliases({
  284. numeric: {
  285. mask: genMask,
  286. _mask: function (opts) {
  287. return "(" + opts.groupSeparator + "999){+|1}";
  288. },
  289. digits: "*", // number of fractionalDigits
  290. digitsOptional: true,
  291. enforceDigitsOnBlur: false,
  292. radixPoint: ".",
  293. positionCaretOnClick: "radixFocus",
  294. _radixDance: true,
  295. groupSeparator: "",
  296. allowMinus: true,
  297. negationSymbol: {
  298. front: "-", // "("
  299. back: "" // ")"
  300. },
  301. prefix: "",
  302. suffix: "",
  303. min: null, // minimum value
  304. max: null, // maximum value
  305. SetMaxOnOverflow: false,
  306. step: 1,
  307. inputType: "text", // number ~ specify that values which are set are in textform (radix point is same as in the options) or in numberform (radixpoint = .)
  308. unmaskAsNumber: false,
  309. roundingFN: Math.round, // Math.floor , fn(x)
  310. inputmode: "decimal",
  311. shortcuts: { k: "1000", m: "1000000" },
  312. // global options
  313. placeholder: "0",
  314. greedy: false,
  315. rightAlign: true,
  316. insertMode: true,
  317. autoUnmask: false,
  318. skipOptionalPartCharacter: "",
  319. usePrototypeDefinitions: false,
  320. stripLeadingZeroes: true,
  321. substituteRadixPoint: true,
  322. definitions: {
  323. 0: {
  324. validator: decimalValidator
  325. },
  326. 1: {
  327. validator: decimalValidator,
  328. definitionSymbol: "9"
  329. },
  330. 9: {
  331. // \uFF11-\uFF19 #1606
  332. validator: "[0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9]",
  333. definitionSymbol: "*"
  334. },
  335. "+": {
  336. validator: function (chrs, maskset, pos, strict, opts) {
  337. return (
  338. opts.allowMinus &&
  339. (chrs === "-" || chrs === opts.negationSymbol.front)
  340. );
  341. }
  342. },
  343. "-": {
  344. validator: function (chrs, maskset, pos, strict, opts) {
  345. return opts.allowMinus && chrs === opts.negationSymbol.back;
  346. }
  347. }
  348. },
  349. preValidation: function (
  350. buffer,
  351. pos,
  352. c,
  353. isSelection,
  354. opts,
  355. maskset,
  356. caretPos,
  357. strict
  358. ) {
  359. const inputmask = this;
  360. if (opts.__financeInput !== false && c === opts.radixPoint) return false;
  361. const radixPos = buffer.indexOf(opts.radixPoint),
  362. initPos = pos;
  363. pos = hanndleRadixDance(pos, c, radixPos, maskset, opts);
  364. if (c === "-" || c === opts.negationSymbol.front) {
  365. if (opts.allowMinus !== true) return false;
  366. let isNegative = false,
  367. front = findValid("+", maskset),
  368. back = findValid("-", maskset);
  369. if (front !== -1) {
  370. isNegative = [front, back];
  371. }
  372. return isNegative !== false
  373. ? {
  374. remove: isNegative,
  375. caret: initPos - opts.negationSymbol.back.length
  376. }
  377. : {
  378. insert: [
  379. {
  380. pos: findValidator.call(inputmask, "+", maskset),
  381. c: opts.negationSymbol.front,
  382. fromIsValid: true
  383. },
  384. {
  385. pos: findValidator.call(inputmask, "-", maskset),
  386. c: opts.negationSymbol.back,
  387. fromIsValid: undefined
  388. }
  389. ],
  390. caret: initPos + opts.negationSymbol.back.length
  391. };
  392. }
  393. if (c === opts.groupSeparator) {
  394. return { caret: initPos };
  395. }
  396. if (strict) return true;
  397. if (
  398. radixPos !== -1 &&
  399. opts._radixDance === true &&
  400. isSelection === false &&
  401. c === opts.radixPoint &&
  402. opts.digits !== undefined &&
  403. (isNaN(opts.digits) || parseInt(opts.digits) > 0) &&
  404. radixPos !== pos
  405. ) {
  406. const radixValidatorPos = findValidator.call(
  407. inputmask,
  408. opts.radixPoint,
  409. maskset
  410. );
  411. if (maskset.validPositions[radixValidatorPos]) {
  412. maskset.validPositions[radixValidatorPos].generatedInput =
  413. maskset.validPositions[radixValidatorPos].generated || false;
  414. }
  415. return {
  416. caret:
  417. opts._radixDance && pos === radixPos - 1 ? radixPos + 1 : radixPos
  418. };
  419. }
  420. if (opts.__financeInput === false) {
  421. if (isSelection) {
  422. if (opts.digitsOptional) {
  423. return { rewritePosition: caretPos.end };
  424. } else if (!opts.digitsOptional) {
  425. if (caretPos.begin > radixPos && caretPos.end <= radixPos) {
  426. if (c === opts.radixPoint) {
  427. return {
  428. insert: { pos: radixPos + 1, c: "0", fromIsValid: true },
  429. rewritePosition: radixPos
  430. };
  431. } else {
  432. return { rewritePosition: radixPos + 1 };
  433. }
  434. } else if (caretPos.begin < radixPos) {
  435. return { rewritePosition: caretPos.begin - 1 };
  436. }
  437. }
  438. } else if (
  439. !opts.showMaskOnHover &&
  440. !opts.showMaskOnFocus &&
  441. !opts.digitsOptional &&
  442. opts.digits > 0 &&
  443. this.__valueGet.call(this.el) === ""
  444. ) {
  445. return { rewritePosition: radixPos };
  446. }
  447. }
  448. return { rewritePosition: pos };
  449. },
  450. postValidation: function (
  451. buffer,
  452. pos,
  453. c,
  454. currentResult,
  455. opts,
  456. maskset,
  457. strict
  458. ) {
  459. if (currentResult === false) return currentResult;
  460. if (strict) return true;
  461. if (opts.min !== null || opts.max !== null) {
  462. const unmasked = opts.onUnMask(
  463. buffer.slice().reverse().join(""),
  464. undefined,
  465. $.extend({}, opts, {
  466. unmaskAsNumber: true
  467. })
  468. );
  469. if (
  470. opts.min !== null &&
  471. unmasked < opts.min &&
  472. (unmasked.toString().length > opts.min.toString().length ||
  473. unmasked < 0)
  474. ) {
  475. return false;
  476. // return {
  477. // refreshFromBuffer: true,
  478. // buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
  479. // };
  480. }
  481. if (opts.max !== null && unmasked > opts.max) {
  482. return opts.SetMaxOnOverflow
  483. ? {
  484. refreshFromBuffer: true,
  485. buffer: alignDigits(
  486. opts.max.toString().replace(".", opts.radixPoint).split(""),
  487. opts.digits,
  488. opts
  489. ).reverse()
  490. }
  491. : false;
  492. }
  493. }
  494. return currentResult;
  495. },
  496. onUnMask: function (maskedValue, unmaskedValue, opts) {
  497. if (unmaskedValue === "" && opts.nullable === true) {
  498. return unmaskedValue;
  499. }
  500. let processValue = maskedValue.replace(opts.prefix, "");
  501. processValue = processValue.replace(opts.suffix, "");
  502. processValue = processValue.replace(
  503. new RegExp(escapeRegex(opts.groupSeparator), "g"),
  504. ""
  505. );
  506. if (opts.placeholder.charAt(0) !== "") {
  507. processValue = processValue.replace(
  508. new RegExp(opts.placeholder.charAt(0), "g"),
  509. "0"
  510. );
  511. }
  512. if (opts.unmaskAsNumber) {
  513. if (
  514. opts.radixPoint !== "" &&
  515. processValue.indexOf(opts.radixPoint) !== -1
  516. )
  517. processValue = processValue.replace(
  518. escapeRegex.call(this, opts.radixPoint),
  519. "."
  520. );
  521. processValue = processValue.replace(
  522. new RegExp("^" + escapeRegex(opts.negationSymbol.front)),
  523. "-"
  524. );
  525. processValue = processValue.replace(
  526. new RegExp(escapeRegex(opts.negationSymbol.back) + "$"),
  527. ""
  528. );
  529. return Number(processValue);
  530. }
  531. return processValue;
  532. },
  533. isComplete: function (buffer, opts) {
  534. let maskedValue = (
  535. opts.numericInput ? buffer.slice().reverse() : buffer
  536. ).join("");
  537. maskedValue = maskedValue.replace(
  538. new RegExp("^" + escapeRegex(opts.negationSymbol.front)),
  539. "-"
  540. );
  541. maskedValue = maskedValue.replace(
  542. new RegExp(escapeRegex(opts.negationSymbol.back) + "$"),
  543. ""
  544. );
  545. maskedValue = maskedValue.replace(opts.prefix, "");
  546. maskedValue = maskedValue.replace(opts.suffix, "");
  547. maskedValue = maskedValue.replace(
  548. new RegExp(escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"),
  549. "$1"
  550. );
  551. if (opts.radixPoint === ",")
  552. maskedValue = maskedValue.replace(escapeRegex(opts.radixPoint), ".");
  553. return isFinite(maskedValue);
  554. },
  555. onBeforeMask: function (initialValue, opts) {
  556. const radixPoint = opts.radixPoint || ",";
  557. if (isFinite(opts.digits)) opts.digits = parseInt(opts.digits);
  558. if (
  559. (typeof initialValue === "number" || opts.inputType === "number") &&
  560. radixPoint !== ""
  561. ) {
  562. initialValue = initialValue.toString().replace(".", radixPoint);
  563. }
  564. const isNagtive =
  565. initialValue.charAt(0) === "-" ||
  566. initialValue.charAt(0) === opts.negationSymbol.front,
  567. valueParts = initialValue.split(radixPoint),
  568. integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
  569. decimalPart =
  570. valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "",
  571. forceDigits = valueParts.length > 1;
  572. initialValue =
  573. integerPart +
  574. (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
  575. let digits = 0;
  576. if (radixPoint !== "") {
  577. digits = !opts.digitsOptional
  578. ? opts.digits
  579. : opts.digits < decimalPart.length
  580. ? opts.digits
  581. : decimalPart.length;
  582. if (decimalPart !== "" || !opts.digitsOptional) {
  583. const digitsFactor = Math.pow(10, digits || 1);
  584. // make the initialValue a valid javascript number for the parsefloat
  585. initialValue = initialValue.replace(escapeRegex(radixPoint), ".");
  586. if (!isNaN(parseFloat(initialValue))) {
  587. initialValue = (
  588. opts.roundingFN(parseFloat(initialValue) * digitsFactor) /
  589. digitsFactor
  590. ).toFixed(digits);
  591. }
  592. initialValue = initialValue.toString().replace(".", radixPoint);
  593. }
  594. }
  595. // this needs to be in a separate part and not directly in decimalPart to allow rounding
  596. if (opts.digits === 0 && initialValue.indexOf(radixPoint) !== -1) {
  597. initialValue = initialValue.substring(
  598. 0,
  599. initialValue.indexOf(radixPoint)
  600. );
  601. }
  602. if (opts.min !== null || opts.max !== null) {
  603. const numberValue = initialValue.toString().replace(radixPoint, ".");
  604. if (opts.min !== null && numberValue < opts.min) {
  605. initialValue = opts.min.toString().replace(".", radixPoint);
  606. } else if (opts.max !== null && numberValue > opts.max) {
  607. initialValue = opts.max.toString().replace(".", radixPoint);
  608. }
  609. }
  610. if (isNagtive && initialValue.charAt(0) !== "-") {
  611. initialValue = "-" + initialValue;
  612. }
  613. return alignDigits(
  614. initialValue.toString().split(""),
  615. digits,
  616. opts,
  617. forceDigits
  618. ).join("");
  619. },
  620. onBeforeWrite: function (e, buffer, caretPos, opts) {
  621. function stripBuffer(buffer, stripRadix) {
  622. if (opts.__financeInput !== false || stripRadix) {
  623. var position = buffer.indexOf(opts.radixPoint);
  624. if (position !== -1) {
  625. buffer.splice(position, 1);
  626. }
  627. }
  628. if (opts.groupSeparator !== "") {
  629. while ((position = buffer.indexOf(opts.groupSeparator)) !== -1) {
  630. buffer.splice(position, 1);
  631. }
  632. }
  633. return buffer;
  634. }
  635. let result, leadingzeroes;
  636. if (
  637. opts.stripLeadingZeroes &&
  638. (leadingzeroes = checkForLeadingZeroes(buffer, opts))
  639. ) {
  640. const caretNdx =
  641. buffer
  642. .join("")
  643. .lastIndexOf(leadingzeroes[0].split("").reverse().join("")) -
  644. (leadingzeroes[0] == leadingzeroes.input ? 0 : 1),
  645. offset = leadingzeroes[0] == leadingzeroes.input ? 1 : 0;
  646. for (let i = leadingzeroes[0].length - offset; i > 0; i--) {
  647. this.maskset.validPositions.splice(caretNdx + i, 1);
  648. delete buffer[caretNdx + i];
  649. }
  650. }
  651. if (e) {
  652. switch (e.type) {
  653. case "blur":
  654. case "checkval":
  655. if (opts.min !== null) {
  656. const unmasked = opts.onUnMask(
  657. buffer.slice().reverse().join(""),
  658. undefined,
  659. $.extend({}, opts, {
  660. unmaskAsNumber: true
  661. })
  662. );
  663. if (opts.min !== null && unmasked < opts.min) {
  664. return {
  665. refreshFromBuffer: true,
  666. buffer: alignDigits(
  667. opts.min.toString().replace(".", opts.radixPoint).split(""),
  668. opts.digits,
  669. opts
  670. ).reverse()
  671. };
  672. }
  673. }
  674. if (buffer[buffer.length - 1] === opts.negationSymbol.front) {
  675. // strip negation symbol on blur when value is 0
  676. const nmbrMtchs = new RegExp(
  677. "(^" +
  678. (opts.negationSymbol.front != ""
  679. ? escapeRegex(opts.negationSymbol.front) + "?"
  680. : "") +
  681. escapeRegex(opts.prefix) +
  682. ")(.*)(" +
  683. escapeRegex(opts.suffix) +
  684. (opts.negationSymbol.back != ""
  685. ? escapeRegex(opts.negationSymbol.back) + "?"
  686. : "") +
  687. "$)"
  688. ).exec(stripBuffer(buffer.slice(), true).reverse().join("")),
  689. number = nmbrMtchs ? nmbrMtchs[2] : "";
  690. if (number == 0) {
  691. result = { refreshFromBuffer: true, buffer: [0] };
  692. }
  693. } else if (opts.radixPoint !== "") {
  694. // strip radixpoint on blur when it is the latest char
  695. const radixNDX = buffer.indexOf(opts.radixPoint);
  696. if (radixNDX === opts.suffix.length) {
  697. if (result && result.buffer) {
  698. result.buffer.splice(0, 1 + opts.suffix.length);
  699. } else {
  700. buffer.splice(0, 1 + opts.suffix.length);
  701. result = {
  702. refreshFromBuffer: true,
  703. buffer: stripBuffer(buffer)
  704. };
  705. }
  706. }
  707. }
  708. if (opts.enforceDigitsOnBlur) {
  709. result = result || {};
  710. const bffr =
  711. (result && result.buffer) || buffer.slice().reverse();
  712. result.refreshFromBuffer = true;
  713. result.buffer = alignDigits(
  714. bffr,
  715. opts.digits,
  716. opts,
  717. true
  718. ).reverse();
  719. }
  720. }
  721. }
  722. return result;
  723. },
  724. onKeyDown: function (e, buffer, caretPos, opts) {
  725. let $input = $(this),
  726. bffr;
  727. if (e.location != 3) {
  728. let pattern,
  729. c = e.key;
  730. if ((pattern = opts.shortcuts && opts.shortcuts[c])) {
  731. if (pattern.length > 1) {
  732. this.inputmask.__valueSet.call(
  733. this,
  734. parseFloat(this.inputmask.unmaskedvalue()) * parseInt(pattern)
  735. );
  736. $input.trigger("setvalue");
  737. return false;
  738. }
  739. }
  740. }
  741. if (e.ctrlKey) {
  742. switch (e.key) {
  743. case keys.ArrowUp:
  744. this.inputmask.__valueSet.call(
  745. this,
  746. parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step)
  747. );
  748. $input.trigger("setvalue");
  749. return false;
  750. case keys.ArrowDown:
  751. this.inputmask.__valueSet.call(
  752. this,
  753. parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step)
  754. );
  755. $input.trigger("setvalue");
  756. return false;
  757. }
  758. }
  759. if (
  760. !e.shiftKey &&
  761. (e.key === keys.Delete ||
  762. e.key === keys.Backspace ||
  763. e.key === keys.BACKSPACE_SAFARI) &&
  764. caretPos.begin !== buffer.length
  765. ) {
  766. if (
  767. buffer[e.key === keys.Delete ? caretPos.begin - 1 : caretPos.end] ===
  768. opts.negationSymbol.front
  769. ) {
  770. bffr = buffer.slice().reverse();
  771. if (opts.negationSymbol.front !== "") bffr.shift();
  772. if (opts.negationSymbol.back !== "") bffr.pop();
  773. $input.trigger("setvalue", [bffr.join(""), caretPos.begin]);
  774. return false;
  775. } else if (opts._radixDance === true) {
  776. const radixPos = buffer.indexOf(opts.radixPoint);
  777. if (!opts.digitsOptional) {
  778. if (
  779. radixPos !== -1 &&
  780. (caretPos.begin < radixPos ||
  781. caretPos.end < radixPos ||
  782. (e.key === keys.Delete &&
  783. (caretPos.begin === radixPos ||
  784. caretPos.begin - 1 === radixPos)))
  785. ) {
  786. let restoreCaretPos;
  787. if (caretPos.begin === caretPos.end) {
  788. // only adjust when not a selection
  789. if (e.key === keys.Backspace || e.key === keys.BACKSPACE_SAFARI)
  790. caretPos.begin++;
  791. else if (
  792. e.key === keys.Delete &&
  793. caretPos.begin - 1 === radixPos
  794. ) {
  795. restoreCaretPos = $.extend({}, caretPos);
  796. caretPos.begin--;
  797. caretPos.end--;
  798. }
  799. }
  800. bffr = buffer.slice().reverse();
  801. bffr.splice(
  802. bffr.length - caretPos.begin,
  803. caretPos.begin - caretPos.end + 1
  804. );
  805. // console.log(caretPos);
  806. bffr = alignDigits(bffr, opts.digits, opts).join("");
  807. if (restoreCaretPos) {
  808. caretPos = restoreCaretPos;
  809. }
  810. $input.trigger("setvalue", [
  811. bffr,
  812. caretPos.begin >= bffr.length ? radixPos + 1 : caretPos.begin
  813. ]);
  814. return false;
  815. }
  816. } else if (radixPos === 0) {
  817. bffr = buffer.slice().reverse();
  818. bffr.pop();
  819. $input.trigger("setvalue", [
  820. bffr.join(""),
  821. caretPos.begin >= bffr.length ? bffr.length : caretPos.begin
  822. ]);
  823. return false;
  824. }
  825. }
  826. }
  827. }
  828. },
  829. currency: {
  830. prefix: "", // "$ ",
  831. groupSeparator: ",",
  832. alias: "numeric",
  833. digits: 2,
  834. digitsOptional: false
  835. },
  836. decimal: {
  837. alias: "numeric"
  838. },
  839. integer: {
  840. alias: "numeric",
  841. inputmode: "numeric",
  842. digits: 0
  843. },
  844. percentage: {
  845. alias: "numeric",
  846. min: 0,
  847. max: 100,
  848. suffix: " %",
  849. digits: 0,
  850. allowMinus: false
  851. },
  852. indianns: {
  853. // indian numbering system
  854. alias: "numeric",
  855. _mask: function (opts) {
  856. return (
  857. "(" +
  858. opts.groupSeparator +
  859. "99){*|1}(" +
  860. opts.groupSeparator +
  861. "999){1|1}"
  862. );
  863. },
  864. groupSeparator: ",",
  865. radixPoint: ".",
  866. placeholder: "0",
  867. digits: 2,
  868. digitsOptional: false
  869. }
  870. });