inputmask.numeric.extensions.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) Robin Herbots
  5. Licensed under the MIT license
  6. */
  7. import Inputmask from "../inputmask";
  8. import escapeRegex from "../escapeRegex";
  9. import {seekNext} from "../positioning";
  10. import {keys} from "../keycode";
  11. const $ = Inputmask.dependencyLib;
  12. function autoEscape(txt, opts) {
  13. var escapedTxt = "";
  14. for (var i = 0; i < txt.length; i++) {
  15. if (Inputmask.prototype.definitions[txt.charAt(i)] ||
  16. opts.definitions[txt.charAt(i)] ||
  17. opts.optionalmarker[0] === txt.charAt(i) ||
  18. opts.optionalmarker[1] === txt.charAt(i) ||
  19. opts.quantifiermarker[0] === txt.charAt(i) ||
  20. opts.quantifiermarker[1] === txt.charAt(i) ||
  21. opts.groupmarker[0] === txt.charAt(i) ||
  22. opts.groupmarker[1] === txt.charAt(i) ||
  23. opts.alternatormarker === txt.charAt(i)) {
  24. escapedTxt += "\\" + txt.charAt(i);
  25. } else {
  26. escapedTxt += txt.charAt(i);
  27. }
  28. }
  29. return escapedTxt;
  30. }
  31. function alignDigits(buffer, digits, opts, force) {
  32. if (buffer.length > 0 && digits > 0 && (!opts.digitsOptional || force)) {
  33. var radixPosition = buffer.indexOf(opts.radixPoint), negationBack = false;
  34. if (opts.negationSymbol.back === buffer[buffer.length - 1]) {
  35. negationBack = true;
  36. buffer.length--;
  37. }
  38. if (radixPosition === -1) {
  39. buffer.push(opts.radixPoint);
  40. radixPosition = buffer.length - 1;
  41. }
  42. for (var i = 1; i <= digits; i++) {
  43. if (!isFinite(buffer[radixPosition + i])) {
  44. buffer[radixPosition + i] = "0";
  45. }
  46. }
  47. }
  48. if (negationBack)
  49. buffer.push(opts.negationSymbol.back);
  50. return buffer;
  51. }
  52. function findValidator(symbol, maskset) {
  53. var posNdx = 0;
  54. if (symbol === "+") {
  55. posNdx = seekNext.call(this, maskset.validPositions.length - 1);
  56. }
  57. for (var tstNdx in maskset.tests) {
  58. tstNdx = parseInt(tstNdx);
  59. if (tstNdx >= posNdx) {
  60. for (var ndx = 0, ndxl = maskset.tests[tstNdx].length; ndx < ndxl; ndx++) {
  61. if ((maskset.validPositions[tstNdx] === undefined || symbol === "-") && maskset.tests[tstNdx][ndx].match.def === symbol) {
  62. return tstNdx + ((maskset.validPositions[tstNdx] !== undefined && symbol !== "-") ? 1 : 0);
  63. }
  64. }
  65. }
  66. }
  67. return posNdx;
  68. }
  69. function findValid(symbol, maskset) {
  70. var ret = -1;
  71. for (let ndx = 0, vpl = maskset.validPositions.length; ndx < vpl; ndx++) {
  72. let tst = maskset.validPositions[ndx];
  73. if (tst && tst.match.def === symbol) {
  74. ret = ndx;
  75. break;
  76. }
  77. }
  78. return ret;
  79. }
  80. function parseMinMaxOptions(opts) {
  81. if (opts.parseMinMaxOptions === undefined) {
  82. // convert min and max options
  83. if (opts.min !== null) {
  84. opts.min = opts.min.toString().replace(new RegExp(escapeRegex(opts.groupSeparator), "g"), "");
  85. if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
  86. opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN;
  87. if (isNaN(opts.min)) opts.min = Number.MIN_VALUE;
  88. }
  89. if (opts.max !== null) {
  90. opts.max = opts.max.toString().replace(new RegExp(escapeRegex(opts.groupSeparator), "g"), "");
  91. if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
  92. opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN;
  93. if (isNaN(opts.max)) opts.max = Number.MAX_VALUE;
  94. }
  95. opts.parseMinMaxOptions = "done";
  96. }
  97. }
  98. function genMask(opts) {
  99. opts.repeat = 0;
  100. //treat equal separator and radixpoint
  101. if (opts.groupSeparator === opts.radixPoint && opts.digits && opts.digits !== "0") {
  102. if (opts.radixPoint === ".") {
  103. opts.groupSeparator = ",";
  104. } else if (opts.radixPoint === ",") {
  105. opts.groupSeparator = ".";
  106. } else {
  107. opts.groupSeparator = "";
  108. }
  109. }
  110. //prevent conflict with default skipOptionalPartCharacter
  111. if (opts.groupSeparator === " ") {
  112. opts.skipOptionalPartCharacter = undefined;
  113. }
  114. //enforce placeholder to single
  115. if (opts.placeholder.length > 1) {
  116. opts.placeholder = opts.placeholder.charAt(0);
  117. }
  118. //only allow radixfocus when placeholder = 0
  119. if (opts.positionCaretOnClick === "radixFocus" && opts.placeholder === "") {
  120. opts.positionCaretOnClick = "lvp";
  121. }
  122. var decimalDef = "0", radixPointDef = opts.radixPoint;
  123. if (opts.numericInput === true && opts.__financeInput === undefined) { //finance people input style
  124. decimalDef = "1";
  125. opts.positionCaretOnClick = opts.positionCaretOnClick === "radixFocus" ? "lvp" : opts.positionCaretOnClick;
  126. opts.digitsOptional = false;
  127. if (isNaN(opts.digits)) opts.digits = 2;
  128. opts._radixDance = false;
  129. radixPointDef = (opts.radixPoint === "," ? "?" : "!");
  130. if (opts.radixPoint !== "" && opts.definitions[radixPointDef] === undefined) {
  131. //update separator definition
  132. opts.definitions[radixPointDef] = {};
  133. opts.definitions[radixPointDef].validator = "[" + opts.radixPoint + "]";
  134. opts.definitions[radixPointDef].placeholder = opts.radixPoint;
  135. opts.definitions[radixPointDef].static = true;
  136. opts.definitions[radixPointDef].generated = true; //forced marker as generated input
  137. }
  138. } else {
  139. opts.__financeInput = false; //needed to keep original selection when remasking
  140. opts.numericInput = true;
  141. }
  142. var mask = "[+]", altMask;
  143. mask += autoEscape(opts.prefix, opts);
  144. if (opts.groupSeparator !== "") {
  145. if (opts.definitions[opts.groupSeparator] === undefined) {
  146. //update separatot definition
  147. opts.definitions[opts.groupSeparator] = {};
  148. opts.definitions[opts.groupSeparator].validator = "[" + opts.groupSeparator + "]";
  149. opts.definitions[opts.groupSeparator].placeholder = opts.groupSeparator;
  150. opts.definitions[opts.groupSeparator].static = true;
  151. opts.definitions[opts.groupSeparator].generated = true; //forced marker as generated input
  152. }
  153. mask += opts._mask(opts);
  154. } else {
  155. mask += "9{+}";
  156. }
  157. if (opts.digits !== undefined && opts.digits !== 0) {
  158. var dq = opts.digits.toString().split(",");
  159. if (isFinite(dq[0]) && dq[1] && isFinite(dq[1])) {
  160. mask += radixPointDef + decimalDef + "{" + opts.digits + "}";
  161. } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
  162. if (opts.digitsOptional || opts.jitMasking) {
  163. altMask = mask + radixPointDef + decimalDef + "{0," + opts.digits + "}";
  164. // mask += "[" + opts.radixPoint + "]";
  165. opts.keepStatic = true;
  166. } else {
  167. mask += radixPointDef + decimalDef + "{" + opts.digits + "}";
  168. }
  169. }
  170. } else {
  171. opts.inputmode = "numeric";
  172. }
  173. mask += autoEscape(opts.suffix, opts);
  174. mask += "[-]";
  175. if (altMask) {
  176. mask = [(altMask + autoEscape(opts.suffix, opts) + "[-]"), mask];
  177. }
  178. opts.greedy = false; //enforce greedy false
  179. parseMinMaxOptions(opts);
  180. if (opts.radixPoint !== "" && opts.substituteRadixPoint)
  181. opts.substitutes[opts.radixPoint == "." ? "," : "."] = opts.radixPoint;
  182. // console.log(mask);
  183. return mask;
  184. }
  185. function hanndleRadixDance(pos, c, radixPos, maskset, opts) {
  186. if (opts._radixDance && opts.numericInput && c !== opts.negationSymbol.back) {
  187. if (pos <= radixPos && (radixPos > 0 || c == opts.radixPoint) && (maskset.validPositions[pos - 1] === undefined || maskset.validPositions[pos - 1].input !== opts.negationSymbol.back)) {
  188. pos -= 1;
  189. }
  190. }
  191. return pos;
  192. }
  193. function decimalValidator(chrs, maskset, pos, strict, opts) {
  194. var radixPos = maskset.buffer ? maskset.buffer.indexOf(opts.radixPoint) : -1,
  195. result = (radixPos !== -1 || (strict && opts.jitMasking)) && new RegExp(opts.definitions["9"].validator).test(chrs);
  196. if (opts._radixDance && radixPos !== -1 && result && maskset.validPositions[radixPos] == undefined) {
  197. return {
  198. insert: {
  199. pos: radixPos === pos ? radixPos + 1 : radixPos,
  200. c: opts.radixPoint
  201. },
  202. pos: pos
  203. };
  204. }
  205. return result;
  206. }
  207. function checkForLeadingZeroes(buffer, opts) {
  208. //check leading zeros
  209. var numberMatches = new RegExp("(^" + (opts.negationSymbol.front !== "" ? escapeRegex(opts.negationSymbol.front) + "?" : "") + escapeRegex(opts.prefix) + ")(.*)(" + escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? escapeRegex(opts.negationSymbol.back) + "?" : "") + "$)").exec(buffer.slice().reverse().join("")),
  210. number = numberMatches ? numberMatches[2] : "", leadingzeroes = false;
  211. if (number) {
  212. number = number.split(opts.radixPoint.charAt(0))[0];
  213. leadingzeroes = new RegExp("^[0" + opts.groupSeparator + "]*").exec(number);
  214. }
  215. return leadingzeroes && (leadingzeroes[0].length > 1 || leadingzeroes[0].length > 0 && leadingzeroes[0].length < number.length) ? leadingzeroes : false;
  216. }
  217. //number aliases
  218. Inputmask.extendAliases({
  219. "numeric": {
  220. mask: genMask,
  221. _mask: function (opts) {
  222. return "(" + opts.groupSeparator + "999){+|1}";
  223. },
  224. digits: "*", //number of fractionalDigits
  225. digitsOptional: true,
  226. enforceDigitsOnBlur: false,
  227. radixPoint: ".",
  228. positionCaretOnClick: "radixFocus",
  229. _radixDance: true,
  230. groupSeparator: "",
  231. allowMinus: true,
  232. negationSymbol: {
  233. front: "-", //"("
  234. back: "" //")"
  235. },
  236. prefix: "",
  237. suffix: "",
  238. min: null, //minimum value
  239. max: null, //maximum value
  240. SetMaxOnOverflow: false,
  241. step: 1,
  242. inputType: "text", //number ~ specify that values which are set are in textform (radix point is same as in the options) or in numberform (radixpoint = .)
  243. unmaskAsNumber: false,
  244. roundingFN: Math.round, //Math.floor , fn(x)
  245. inputmode: "decimal",
  246. shortcuts: {k: "1000", m: "1000000"},
  247. //global options
  248. placeholder: "0",
  249. greedy: false,
  250. rightAlign: true,
  251. insertMode: true,
  252. autoUnmask: false,
  253. skipOptionalPartCharacter: "",
  254. usePrototypeDefinitions: false,
  255. stripLeadingZeroes: true,
  256. substituteRadixPoint: true,
  257. definitions: {
  258. "0": {
  259. validator: decimalValidator
  260. },
  261. "1": {
  262. validator: decimalValidator,
  263. definitionSymbol: "9"
  264. },
  265. "9": { //\uFF11-\uFF19 #1606
  266. validator: "[0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9]",
  267. definitionSymbol: "*"
  268. },
  269. "+": {
  270. validator: function (chrs, maskset, pos, strict, opts) {
  271. return (opts.allowMinus && (chrs === "-" || chrs === opts.negationSymbol.front));
  272. }
  273. },
  274. "-": {
  275. validator: function (chrs, maskset, pos, strict, opts) {
  276. return (opts.allowMinus && chrs === opts.negationSymbol.back);
  277. }
  278. }
  279. },
  280. preValidation: function (buffer, pos, c, isSelection, opts, maskset, caretPos, strict) {
  281. const inputmask = this;
  282. if (opts.__financeInput !== false && c === opts.radixPoint) return false;
  283. var radixPos = buffer.indexOf(opts.radixPoint), initPos = pos;
  284. pos = hanndleRadixDance(pos, c, radixPos, maskset, opts);
  285. if (c === "-" || c === opts.negationSymbol.front) {
  286. if (opts.allowMinus !== true) return false;
  287. var isNegative = false,
  288. front = findValid("+", maskset), back = findValid("-", maskset);
  289. if (front !== -1) {
  290. isNegative = [front, back];
  291. }
  292. return isNegative !== false ? {
  293. remove: isNegative,
  294. caret: initPos - opts.negationSymbol.back.length
  295. } : {
  296. insert: [
  297. {
  298. pos: findValidator.call(inputmask, "+", maskset),
  299. c: opts.negationSymbol.front,
  300. fromIsValid: true
  301. },
  302. {
  303. pos: findValidator.call(inputmask, "-", maskset),
  304. c: opts.negationSymbol.back,
  305. fromIsValid: undefined
  306. }],
  307. caret: initPos + opts.negationSymbol.back.length
  308. };
  309. }
  310. if (c === opts.groupSeparator) {
  311. return {caret: initPos};
  312. }
  313. if (strict) return true;
  314. if (radixPos !== -1 && (opts._radixDance === true && isSelection === false && c === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) && radixPos !== pos)) {
  315. return {
  316. "caret": opts._radixDance && pos === radixPos - 1 ? radixPos + 1 : radixPos
  317. };
  318. }
  319. if (opts.__financeInput === false) {
  320. if (isSelection) {
  321. if (opts.digitsOptional) {
  322. return {rewritePosition: caretPos.end};
  323. } else if (!opts.digitsOptional) {
  324. if (caretPos.begin > radixPos && caretPos.end <= radixPos) {
  325. if (c === opts.radixPoint) {
  326. return {
  327. insert: {pos: radixPos + 1, c: "0", fromIsValid: true},
  328. rewritePosition: radixPos
  329. };
  330. } else {
  331. return {rewritePosition: radixPos + 1};
  332. }
  333. } else if (caretPos.begin < radixPos) {
  334. return {rewritePosition: caretPos.begin - 1};
  335. }
  336. }
  337. } else if (!opts.showMaskOnHover && !opts.showMaskOnFocus && !opts.digitsOptional && opts.digits > 0 && this.__valueGet.call(this.el) === "") {
  338. return {rewritePosition: radixPos};
  339. }
  340. }
  341. return {rewritePosition: pos};
  342. },
  343. postValidation: function (buffer, pos, c, currentResult, opts, maskset, strict) {
  344. if (currentResult === false) return currentResult;
  345. if (strict) return true;
  346. if (opts.min !== null || opts.max !== null) {
  347. var unmasked = opts.onUnMask(buffer.slice().reverse().join(""), undefined, $.extend({}, opts, {
  348. unmaskAsNumber: true
  349. }));
  350. if (opts.min !== null && unmasked < opts.min && (unmasked.toString().length > opts.min.toString().length || unmasked < 0)) {
  351. return false;
  352. // return {
  353. // refreshFromBuffer: true,
  354. // buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
  355. // };
  356. }
  357. if (opts.max !== null && unmasked > opts.max) {
  358. return opts.SetMaxOnOverflow ? {
  359. refreshFromBuffer: true,
  360. buffer: alignDigits(opts.max.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
  361. } : false;
  362. }
  363. }
  364. return currentResult;
  365. },
  366. onUnMask: function (maskedValue, unmaskedValue, opts) {
  367. if (unmaskedValue === "" && opts.nullable === true) {
  368. return unmaskedValue;
  369. }
  370. var processValue = maskedValue.replace(opts.prefix, "");
  371. processValue = processValue.replace(opts.suffix, "");
  372. processValue = processValue.replace(new RegExp(escapeRegex(opts.groupSeparator), "g"), "");
  373. if (opts.placeholder.charAt(0) !== "") {
  374. processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0");
  375. }
  376. if (opts.unmaskAsNumber) {
  377. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(escapeRegex.call(this, opts.radixPoint), ".");
  378. processValue = processValue.replace(new RegExp("^" + escapeRegex(opts.negationSymbol.front)), "-");
  379. processValue = processValue.replace(new RegExp(escapeRegex(opts.negationSymbol.back) + "$"), "");
  380. return Number(processValue);
  381. }
  382. return processValue;
  383. }
  384. ,
  385. isComplete: function (buffer, opts) {
  386. var maskedValue = (opts.numericInput ? buffer.slice().reverse() : buffer).join("");
  387. maskedValue = maskedValue.replace(new RegExp("^" + escapeRegex(opts.negationSymbol.front)), "-");
  388. maskedValue = maskedValue.replace(new RegExp(escapeRegex(opts.negationSymbol.back) + "$"), "");
  389. maskedValue = maskedValue.replace(opts.prefix, "");
  390. maskedValue = maskedValue.replace(opts.suffix, "");
  391. maskedValue = maskedValue.replace(new RegExp(escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"), "$1");
  392. if (opts.radixPoint === ",") maskedValue = maskedValue.replace(escapeRegex(opts.radixPoint), ".");
  393. return isFinite(maskedValue);
  394. },
  395. onBeforeMask: function (initialValue, opts) {
  396. var radixPoint = opts.radixPoint || ",";
  397. if (isFinite(opts.digits)) opts.digits = parseInt(opts.digits);
  398. if ((typeof initialValue == "number" || opts.inputType === "number") && radixPoint !== "") {
  399. initialValue = initialValue.toString().replace(".", radixPoint);
  400. }
  401. var isNagtive = initialValue.charAt(0) === "-" || initialValue.charAt(0) === opts.negationSymbol.front;
  402. var valueParts = initialValue.split(radixPoint),
  403. integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
  404. decimalPart = valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "",
  405. forceDigits = valueParts.length > 1;
  406. initialValue = integerPart + (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
  407. var digits = 0;
  408. if (radixPoint !== "") {
  409. digits = !opts.digitsOptional ? opts.digits : (opts.digits < decimalPart.length ? opts.digits : decimalPart.length);
  410. if (decimalPart !== "" || !opts.digitsOptional) {
  411. var digitsFactor = Math.pow(10, digits || 1);
  412. //make the initialValue a valid javascript number for the parsefloat
  413. initialValue = initialValue.replace(escapeRegex(radixPoint), ".");
  414. if (!isNaN(parseFloat(initialValue))) {
  415. initialValue = (opts.roundingFN(parseFloat(initialValue) * digitsFactor) / digitsFactor).toFixed(digits);
  416. }
  417. initialValue = initialValue.toString().replace(".", radixPoint);
  418. }
  419. }
  420. //this needs to be in a separate part and not directly in decimalPart to allow rounding
  421. if (opts.digits === 0 && initialValue.indexOf(radixPoint) !== -1) {
  422. initialValue = initialValue.substring(0, initialValue.indexOf(radixPoint));
  423. }
  424. if (opts.min !== null || opts.max !== null) {
  425. var numberValue = initialValue.toString().replace(radixPoint, ".");
  426. if (opts.min !== null && numberValue < opts.min) {
  427. initialValue = opts.min.toString().replace(".", radixPoint);
  428. } else if (opts.max !== null && numberValue > opts.max) {
  429. initialValue = opts.max.toString().replace(".", radixPoint);
  430. }
  431. }
  432. if (isNagtive && initialValue.charAt(0) !== "-") {
  433. initialValue = "-" + initialValue;
  434. }
  435. return alignDigits(initialValue.toString().split(""), digits, opts, forceDigits).join("");
  436. }
  437. ,
  438. onBeforeWrite: function (e, buffer, caretPos, opts) {
  439. function stripBuffer(buffer, stripRadix) {
  440. if (opts.__financeInput !== false || stripRadix) {
  441. var position = buffer.indexOf(opts.radixPoint);
  442. if (position !== -1) {
  443. buffer.splice(position, 1);
  444. }
  445. }
  446. if (opts.groupSeparator !== "") {
  447. while ((position = buffer.indexOf(opts.groupSeparator)) !== -1) {
  448. buffer.splice(position, 1);
  449. }
  450. }
  451. return buffer;
  452. }
  453. let result, leadingzeroes;
  454. if (opts.stripLeadingZeroes && (leadingzeroes = checkForLeadingZeroes(buffer, opts))) {
  455. const caretNdx = buffer.join("").lastIndexOf(leadingzeroes[0].split("").reverse().join("")) - (leadingzeroes[0] == leadingzeroes.input ? 0 : 1),
  456. offset = (leadingzeroes[0] == leadingzeroes.input ? 1 : 0);
  457. for (let i = leadingzeroes[0].length - offset; i > 0; i--) {
  458. delete this.maskset.validPositions[caretNdx + i];
  459. delete buffer[caretNdx + i];
  460. }
  461. }
  462. if (e) {
  463. switch (e.type) {
  464. case "blur":
  465. case "checkval":
  466. if (opts.min !== null) {
  467. var unmasked = opts.onUnMask(buffer.slice().reverse().join(""), undefined, $.extend({}, opts, {
  468. unmaskAsNumber: true
  469. }));
  470. if (opts.min !== null && unmasked < opts.min) {
  471. return {
  472. refreshFromBuffer: true,
  473. buffer: alignDigits(opts.min.toString().replace(".", opts.radixPoint).split(""), opts.digits, opts).reverse()
  474. };
  475. }
  476. }
  477. if (buffer[buffer.length - 1] === opts.negationSymbol.front) { //strip negation symbol on blur when value is 0
  478. var nmbrMtchs = new RegExp("(^" + (opts.negationSymbol.front != "" ? escapeRegex(opts.negationSymbol.front) + "?" : "") + escapeRegex(opts.prefix) + ")(.*)(" + escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? escapeRegex(opts.negationSymbol.back) + "?" : "") + "$)").exec(stripBuffer(buffer.slice(), true).reverse().join("")),
  479. number = nmbrMtchs ? nmbrMtchs[2] : "";
  480. if (number == 0) {
  481. result = {refreshFromBuffer: true, buffer: [0]};
  482. }
  483. } else if (opts.radixPoint !== "") { //strip radixpoint on blur when it is the latest char
  484. var radixNDX = buffer.indexOf(opts.radixPoint);
  485. if (radixNDX === opts.suffix.length) {
  486. if (result && result.buffer) {
  487. result.buffer.splice(0, 1 + opts.suffix.length);
  488. } else {
  489. buffer.splice(0, 1 + opts.suffix.length);
  490. result =
  491. {refreshFromBuffer: true, buffer: stripBuffer(buffer)};
  492. }
  493. }
  494. }
  495. if (opts.enforceDigitsOnBlur) {
  496. result = result || {};
  497. var bffr = (result && result.buffer) || buffer.slice().reverse();
  498. result.refreshFromBuffer = true;
  499. result.buffer = alignDigits(bffr, opts.digits, opts, true).reverse();
  500. }
  501. }
  502. }
  503. return result;
  504. },
  505. onKeyDown: function (e, buffer, caretPos, opts) {
  506. var $input = $(this), bffr;
  507. if (e.location != 3) {
  508. var pattern, c = e.key;
  509. if ((pattern = (opts.shortcuts && opts.shortcuts[c]))) {
  510. if (pattern.length > 1) {
  511. this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) * parseInt(pattern));
  512. $input.trigger("setvalue");
  513. return false;
  514. }
  515. }
  516. }
  517. if (e.ctrlKey) {
  518. switch (e.key) {
  519. case keys.Up:
  520. this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  521. $input.trigger("setvalue");
  522. return false;
  523. case keys.Down:
  524. this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  525. $input.trigger("setvalue");
  526. return false;
  527. }
  528. }
  529. if (!e.shiftKey && (e.key === keys.Delete || e.key === keys.Backspace || e.key === keys.BACKSPACE_SAFARI) && caretPos.begin !== buffer.length) {
  530. if (buffer[e.key === keys.Delete ? caretPos.begin - 1 : caretPos.end] === opts.negationSymbol.front) {
  531. bffr = buffer.slice().reverse();
  532. if (opts.negationSymbol.front !== "") bffr.shift();
  533. if (opts.negationSymbol.back !== "") bffr.pop();
  534. $input.trigger("setvalue", [bffr.join(""), caretPos.begin]);
  535. return false;
  536. } else if (opts._radixDance === true) {
  537. var radixPos = buffer.indexOf(opts.radixPoint);
  538. if (!opts.digitsOptional) {
  539. if (radixPos !== -1 && (caretPos.begin < radixPos || caretPos.end < radixPos || (e.key === keys.Delete && (caretPos.begin === radixPos || caretPos.begin - 1 === radixPos)))) {
  540. var restoreCaretPos = undefined;
  541. if (caretPos.begin === caretPos.end) { //only adjust when not a selection
  542. if (e.key === keys.Backspace || e.key === keys.BACKSPACE_SAFARI)
  543. caretPos.begin++;
  544. else if (e.key === keys.Delete && caretPos.begin - 1 === radixPos) {
  545. restoreCaretPos = $.extend({}, caretPos);
  546. caretPos.begin--;
  547. caretPos.end--;
  548. }
  549. }
  550. bffr = buffer.slice().reverse();
  551. bffr.splice(bffr.length - caretPos.begin, caretPos.begin - caretPos.end + 1);
  552. // console.log(caretPos);
  553. bffr = alignDigits(bffr, opts.digits, opts).join("");
  554. if (restoreCaretPos) {
  555. caretPos = restoreCaretPos;
  556. }
  557. $input.trigger("setvalue", [bffr, caretPos.begin >= bffr.length ? radixPos + 1 : caretPos.begin]);
  558. return false;
  559. }
  560. } else if (radixPos === 0) {
  561. bffr = buffer.slice().reverse();
  562. bffr.pop();
  563. $input.trigger("setvalue", [bffr.join(""), caretPos.begin >= bffr.length ? bffr.length : caretPos.begin]);
  564. return false;
  565. }
  566. }
  567. }
  568. }
  569. },
  570. "currency": {
  571. prefix: "", //"$ ",
  572. groupSeparator: ",",
  573. alias: "numeric",
  574. digits: 2,
  575. digitsOptional: false
  576. },
  577. "decimal": {
  578. alias: "numeric"
  579. },
  580. "integer": {
  581. alias: "numeric",
  582. inputmode: "numeric",
  583. digits: 0
  584. },
  585. "percentage": {
  586. alias: "numeric",
  587. min: 0,
  588. max: 100,
  589. suffix: " %",
  590. digits: 0,
  591. allowMinus: false
  592. },
  593. "indianns": { //indian numbering system
  594. alias: "numeric",
  595. _mask: function (opts) {
  596. return "(" + opts.groupSeparator + "99){*|1}(" + opts.groupSeparator + "999){1|1}";
  597. },
  598. groupSeparator: ",",
  599. radixPoint: ".",
  600. placeholder: "0",
  601. digits: 2,
  602. digitsOptional: false
  603. }
  604. });