inputmask.numeric.extensions.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 0.0.0-dev
  7. Optional extensions on the jquery.inputmask base
  8. */
  9. (function (factory) {
  10. if (typeof define === "function" && define.amd) {
  11. define(["inputmask.dependencyLib", "inputmask"], factory);
  12. } else if (typeof exports === "object") {
  13. module.exports = factory(require("./inputmask.dependencyLib"), require("./inputmask"));
  14. } else {
  15. factory(window.dependencyLib || jQuery, window.Inputmask);
  16. }
  17. }
  18. (function ($, Inputmask) {
  19. //number aliases
  20. Inputmask.extendAliases({
  21. "numeric": {
  22. mask: function (opts) {
  23. function autoEscape(txt) {
  24. var escapedTxt = "";
  25. for (var i = 0; i < txt.length; i++) {
  26. if (opts.definitions[txt.charAt(i)] ||
  27. opts.optionalmarker.start === txt.charAt(i) ||
  28. opts.optionalmarker.end === txt.charAt(i) ||
  29. opts.quantifiermarker.start === txt.charAt(i) ||
  30. opts.quantifiermarker.end === txt.charAt(i) ||
  31. opts.groupmarker.start === txt.charAt(i) ||
  32. opts.groupmarker.end === txt.charAt(i) ||
  33. opts.alternatormarker === txt.charAt(i))
  34. escapedTxt += "\\" + txt.charAt(i)
  35. else escapedTxt += txt.charAt(i);
  36. }
  37. return escapedTxt;
  38. }
  39. if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
  40. opts.integerDigits = opts.repeat;
  41. }
  42. opts.repeat = 0;
  43. if (opts.groupSeparator === opts.radixPoint) { //treat equal separator and radixpoint
  44. if (opts.radixPoint === ".") {
  45. opts.groupSeparator = ",";
  46. } else if (opts.radixPoint === ",") {
  47. opts.groupSeparator = ".";
  48. } else opts.groupSeparator = "";
  49. }
  50. if (opts.groupSeparator === " ") { //prevent conflict with default skipOptionalPartCharacter
  51. opts.skipOptionalPartCharacter = undefined;
  52. }
  53. opts.autoGroup = opts.autoGroup && opts.groupSeparator !== "";
  54. if (opts.autoGroup) {
  55. if (typeof opts.groupSize == "string" && isFinite(opts.groupSize)) opts.groupSize = parseInt(opts.groupSize);
  56. if (isFinite(opts.integerDigits)) {
  57. var seps = Math.floor(opts.integerDigits / opts.groupSize);
  58. var mod = opts.integerDigits % opts.groupSize;
  59. opts.integerDigits = parseInt(opts.integerDigits) + (mod === 0 ? seps - 1 : seps);
  60. if (opts.integerDigits < 1) {
  61. opts.integerDigits = "*";
  62. }
  63. }
  64. }
  65. //enforce placeholder to single
  66. if (opts.placeholder.length > 1) {
  67. opts.placeholder = opts.placeholder.charAt(0);
  68. }
  69. //only allow radixfocus when placeholder = 0
  70. if (opts.positionCaretOnClick === "radixFocus" && (opts.placeholder === "" && opts.integerOptional === false)) {
  71. opts.positionCaretOnClick = "lvp";
  72. }
  73. opts.definitions[";"] = opts.definitions["~"]; //clone integer def for decimals
  74. opts.definitions[";"].definitionSymbol = "~";
  75. if (opts.numericInput === true) { //finance people input style
  76. opts.positionCaretOnClick = opts.positionCaretOnClick === "radixFocus" ? "lvp" : opts.positionCaretOnClick;
  77. opts.digitsOptional = false;
  78. if (isNaN(opts.digits)) opts.digits = 2;
  79. opts.decimalProtect = false;
  80. }
  81. var mask = "[+]";
  82. mask += autoEscape(opts.prefix);
  83. if (opts.integerOptional === true) {
  84. mask += "~{1," + opts.integerDigits + "}";
  85. } else mask += "~{" + opts.integerDigits + "}";
  86. if (opts.digits !== undefined) {
  87. opts.radixPointDefinitionSymbol = opts.decimalProtect ? ":" : opts.radixPoint;
  88. var dq = opts.digits.toString().split(",");
  89. if (isFinite(dq[0] && dq[1] && isFinite(dq[1]))) {
  90. mask += opts.radixPointDefinitionSymbol + ";{" + opts.digits + "}";
  91. } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
  92. if (opts.digitsOptional) {
  93. mask += "[" + opts.radixPointDefinitionSymbol + ";{1," + opts.digits + "}]";
  94. } else mask += opts.radixPointDefinitionSymbol + ";{" + opts.digits + "}";
  95. }
  96. }
  97. mask += autoEscape(opts.suffix);
  98. mask += "[-]";
  99. opts.greedy = false; //enforce greedy false
  100. //convert min and max options
  101. if (opts.min !== null) {
  102. opts.min = opts.min.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  103. if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
  104. }
  105. if (opts.max !== null) {
  106. opts.max = opts.max.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  107. if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
  108. }
  109. return mask;
  110. },
  111. placeholder: "",
  112. greedy: false,
  113. digits: "*", //number of fractionalDigits
  114. digitsOptional: true,
  115. radixPoint: ".",
  116. positionCaretOnClick: "radixFocus",
  117. groupSize: 3,
  118. groupSeparator: "",
  119. autoGroup: false,
  120. allowPlus: true,
  121. allowMinus: true,
  122. negationSymbol: {
  123. front: "-", //"("
  124. back: "" //")"
  125. },
  126. integerDigits: "+", //number of integerDigits
  127. integerOptional: true,
  128. prefix: "",
  129. suffix: "",
  130. rightAlign: true,
  131. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  132. min: null, //minimum value
  133. max: null, //maximum value
  134. step: 1,
  135. insertMode: true,
  136. autoUnmask: false,
  137. unmaskAsNumber: false,
  138. inputmode: "numeric",
  139. postFormat: function (buffer, pos, opts) { //this needs to be removed // this is crap
  140. // console.log(buffer);
  141. if (opts.numericInput === true) {
  142. buffer = buffer.reverse();
  143. if (isFinite(pos)) {
  144. pos = buffer.join("").length - pos - 1;
  145. }
  146. }
  147. var i, l;
  148. //position overflow corrections
  149. pos = pos >= buffer.length ? buffer.length - 1 : (pos < 0 ? 0 : pos);
  150. var charAtPos = buffer[pos];
  151. var cbuf = buffer.slice();
  152. if (charAtPos === opts.groupSeparator) {
  153. cbuf.splice(pos--, 1);
  154. charAtPos = cbuf[pos];
  155. }
  156. var isNegative = cbuf.join("").match(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)));
  157. isNegative = isNegative !== null && isNegative.length === 1;
  158. if (pos > ((isNegative ? opts.negationSymbol.front.length : 0 ) + opts.prefix.length ) && (pos < (cbuf.length - opts.suffix.length))) {
  159. //mark current pos
  160. cbuf[pos] = "!";
  161. }
  162. var bufVal = cbuf.join(""), bufValOrigin = cbuf.join(); //join without args to keep the exact elements
  163. if (isNegative) {
  164. bufVal = bufVal.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "");
  165. bufVal = bufVal.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  166. }
  167. bufVal = bufVal.replace(new RegExp(Inputmask.escapeRegex(opts.suffix) + "$"), "");
  168. bufVal = bufVal.replace(new RegExp("^" + Inputmask.escapeRegex(opts.prefix)), "");
  169. if (bufVal.length > 0 && opts.autoGroup || bufVal.indexOf(opts.groupSeparator) !== -1) {
  170. var escapedGroupSeparator = Inputmask.escapeRegex(opts.groupSeparator);
  171. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  172. var radixSplit = bufVal.split(charAtPos === opts.radixPoint ? "!" : opts.radixPoint);
  173. bufVal = opts.radixPoint === "" ? bufVal : radixSplit[0];
  174. if (charAtPos !== opts.negationSymbol.front) bufVal = bufVal.replace("!", "?");
  175. if (bufVal.length > opts.groupSize) {
  176. var reg = new RegExp("([-\+]?[\\d\?]+)([\\d\?]{" + opts.groupSize + "})");
  177. while (reg.test(bufVal) && opts.groupSeparator !== "") {
  178. bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2");
  179. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  180. }
  181. }
  182. bufVal = bufVal.replace("?", "!");
  183. if (opts.radixPoint !== "" && radixSplit.length > 1) {
  184. bufVal += (charAtPos === opts.radixPoint ? "!" : opts.radixPoint) + radixSplit[1];
  185. }
  186. }
  187. bufVal = opts.prefix + bufVal + opts.suffix;
  188. if (isNegative) {
  189. bufVal = opts.negationSymbol.front + bufVal + opts.negationSymbol.back;
  190. }
  191. var needsRefresh = bufValOrigin !== bufVal.split('').join(),
  192. newPos = $.inArray("!", bufVal);
  193. if (newPos === -1) newPos = pos;
  194. if (needsRefresh) {
  195. buffer.length = bufVal.length; //align the length
  196. for (i = 0, l = bufVal.length; i < l; i++) {
  197. buffer[i] = bufVal.charAt(i);
  198. }
  199. buffer[newPos] = charAtPos;
  200. }
  201. // console.log("formatted " + buffer + " refresh " + needsRefresh);
  202. newPos = (opts.numericInput && isFinite(pos)) ? buffer.join("").length - newPos - 1 : newPos;
  203. if (opts.numericInput) {
  204. buffer = buffer.reverse();
  205. if ($.inArray(opts.radixPoint, buffer) < newPos && (buffer.join("").length - opts.suffix.length) !== newPos) {
  206. newPos = newPos - 1;
  207. }
  208. }
  209. return {
  210. pos: newPos,
  211. "refreshFromBuffer": needsRefresh,
  212. "buffer": buffer,
  213. isNegative: isNegative
  214. };
  215. }
  216. ,
  217. onBeforeWrite: function (e, buffer, caretPos, opts) {
  218. var rslt;
  219. if (e && (e.type === "blur" || e.type === "checkval" || e.type === "keydown")) {
  220. var maskedValue = opts.numericInput ? buffer.slice().reverse().join("") : buffer.join(""),
  221. processValue = maskedValue.replace(opts.prefix, "");
  222. processValue = processValue.replace(opts.suffix, "");
  223. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  224. if (opts.radixPoint === ",") processValue = processValue.replace(opts.radixPoint, ".");
  225. //handle negation symbol
  226. var isNegative = processValue.match(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"));
  227. isNegative = isNegative !== null && isNegative.length === 1;
  228. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  229. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  230. //strip placeholder at the end
  231. if (isNaN(opts.placeholder)) {
  232. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.placeholder), "g"), "");
  233. }
  234. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  235. if (processValue !== "" && isFinite(processValue)) {
  236. var floatValue = parseFloat(processValue),
  237. signedFloatValue = isNegative ? floatValue * -1 : floatValue;
  238. if (opts.min !== null && isFinite(opts.min) && signedFloatValue < parseFloat(opts.min)) {
  239. floatValue = Math.abs(opts.min);
  240. isNegative = opts.min < 0;
  241. maskedValue = undefined;
  242. }
  243. else if (opts.max !== null && isFinite(opts.max) && signedFloatValue > parseFloat(opts.max)) {
  244. floatValue = Math.abs(opts.max);
  245. isNegative = opts.max < 0;
  246. maskedValue = undefined;
  247. }
  248. processValue = floatValue.toString().replace(".", opts.radixPoint).split('');
  249. if (isFinite(opts.digits)) {
  250. var radixPosition = $.inArray(opts.radixPoint, processValue);
  251. var rpb = $.inArray(opts.radixPoint, maskedValue);
  252. if (radixPosition === -1) {
  253. processValue.push(opts.radixPoint);
  254. radixPosition = processValue.length - 1;
  255. }
  256. for (var i = 1; i <= opts.digits; i++) {
  257. if (!opts.digitsOptional && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  258. processValue[radixPosition + i] = "0";
  259. } else if (rpb !== -1 && maskedValue[rpb + i] !== undefined) {
  260. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  261. }
  262. }
  263. if (processValue[processValue.length - 1] === opts.radixPoint) {
  264. delete processValue[processValue.length - 1];
  265. }
  266. }
  267. if ((floatValue.toString() !== processValue && floatValue.toString() + "." !== processValue) || isNegative) {
  268. processValue = (opts.prefix + processValue.join("")).split("");
  269. if (isNegative && (floatValue !== 0 || e.type !== "blur")) {
  270. processValue.unshift(opts.negationSymbol.front);
  271. processValue.push(opts.negationSymbol.back);
  272. }
  273. if (opts.numericInput) processValue = processValue.reverse();
  274. rslt = opts.postFormat(processValue, opts.numericInput ? caretPos : caretPos - 1, opts);
  275. if (rslt.buffer) rslt.refreshFromBuffer = rslt.buffer.join("") !== buffer.join("");
  276. return rslt;
  277. }
  278. }
  279. }
  280. if (opts.autoGroup) {
  281. rslt = opts.postFormat(buffer, opts.numericInput ? caretPos : (caretPos - 1), opts);
  282. rslt.caret =
  283. ((caretPos < (rslt.isNegative ? opts.negationSymbol.front.length : 0) + opts.prefix.length) ||
  284. (caretPos > (rslt.buffer.length - (rslt.isNegative ? opts.negationSymbol.back.length : 0))))
  285. ? rslt.pos : rslt.pos + 1;
  286. return rslt;
  287. }
  288. },
  289. regex: {
  290. integerPart: function (opts) {
  291. return new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?\\d+");
  292. }
  293. ,
  294. integerNPart: function (opts) {
  295. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + Inputmask.escapeRegex(opts.placeholder.charAt(0)) + "]+");
  296. }
  297. },
  298. signHandler: function (chrs, maskset, pos, strict, opts) {
  299. if (!strict && (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+")) {
  300. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  301. if (matchRslt && matchRslt[0].length > 0) {
  302. if (maskset.buffer[matchRslt.index] === (chrs === "-" ? "+" : opts.negationSymbol.front)) {
  303. if (chrs === "-") {
  304. if (opts.negationSymbol.back !== "") {
  305. return {
  306. "pos": 0,
  307. "c": opts.negationSymbol.front,
  308. "remove": 0,
  309. "caret": pos,
  310. "insert": {
  311. "pos": maskset.buffer.length - 1,
  312. "c": opts.negationSymbol.back
  313. }
  314. };
  315. } else {
  316. return {
  317. "pos": 0,
  318. "c": opts.negationSymbol.front,
  319. "remove": 0,
  320. "caret": pos
  321. };
  322. }
  323. } else {
  324. if (opts.negationSymbol.back !== "") {
  325. return {
  326. "pos": 0,
  327. "c": "+",
  328. "remove": [0, maskset.buffer.length - 1],
  329. "caret": pos
  330. };
  331. } else {
  332. return {
  333. "pos": 0,
  334. "c": "+",
  335. "remove": 0,
  336. "caret": pos
  337. };
  338. }
  339. }
  340. } else if (maskset.buffer[0] === (chrs === "-" ? opts.negationSymbol.front : "+")) {
  341. if (chrs === "-" && opts.negationSymbol.back !== "") {
  342. return {
  343. "remove": [0, maskset.buffer.length - 1],
  344. "caret": pos - 1
  345. };
  346. } else {
  347. return {
  348. "remove": 0,
  349. "caret": pos - 1
  350. };
  351. }
  352. } else {
  353. if (chrs === "-") {
  354. if (opts.negationSymbol.back !== "") {
  355. return {
  356. "pos": 0,
  357. "c": opts.negationSymbol.front,
  358. "caret": pos + 1,
  359. "insert": {
  360. "pos": maskset.buffer.length,
  361. "c": opts.negationSymbol.back
  362. }
  363. };
  364. } else {
  365. return {
  366. "pos": 0,
  367. "c": opts.negationSymbol.front,
  368. "caret": pos + 1
  369. };
  370. }
  371. } else {
  372. return {
  373. "pos": 0,
  374. "c": chrs,
  375. "caret": pos + 1
  376. };
  377. }
  378. }
  379. }
  380. }
  381. return false;
  382. }
  383. ,
  384. radixHandler: function (chrs, maskset, pos, strict, opts) {
  385. if (!strict && opts.numericInput !== true) {
  386. //if ($.inArray(chrs, [",", "."]) !== -1) chrs = opts.radixPoint;
  387. if (chrs === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  388. var radixPos = $.inArray(opts.radixPoint, maskset.buffer),
  389. integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  390. if (radixPos !== -1 && maskset.validPositions[radixPos]) {
  391. if (maskset.validPositions[radixPos - 1]) {
  392. return {
  393. "caret": radixPos + 1
  394. };
  395. } else {
  396. return {
  397. "pos": integerValue.index,
  398. c: integerValue[0],
  399. "caret": radixPos + 1
  400. };
  401. }
  402. } else if (!integerValue || (integerValue["0"] === "0" && (integerValue.index + 1) !== pos)) {
  403. maskset.buffer[integerValue ? integerValue.index : pos] = "0";
  404. return {
  405. "pos": (integerValue ? integerValue.index : pos) + 1,
  406. c: opts.radixPoint
  407. };
  408. }
  409. }
  410. }
  411. return false;
  412. },
  413. leadingZeroHandler: function (chrs, maskset, pos, strict, opts, isSelection) {
  414. if (!strict) {
  415. var initialPos = pos,
  416. buffer = opts.numericInput === true ? maskset.buffer.slice("").reverse() : maskset.buffer.slice("");
  417. if (opts.numericInput) {
  418. pos = buffer.join("").length - pos - 1;
  419. }
  420. buffer.splice(0, opts.prefix.length);
  421. buffer.splice(buffer.length - opts.suffix.length, opts.suffix.length);
  422. pos = pos - opts.prefix.length;
  423. var radixPosition = $.inArray(opts.radixPoint, buffer),
  424. matchRslt = buffer.slice(0, radixPosition !== -1 ? radixPosition : undefined).join("").match(opts.regex.integerNPart(opts));
  425. if (matchRslt && (radixPosition === -1 || pos <= radixPosition || opts.numericInput)) {
  426. var decimalPart = radixPosition === -1 ? 0 : parseInt(buffer.slice(radixPosition + 1).join("")),
  427. leadingZero = matchRslt["0"].indexOf(opts.placeholder !== "" ? opts.placeholder.charAt(0) : "0") === 0;
  428. if (opts.numericInput) {
  429. if (leadingZero && decimalPart !== 0 && isSelection !== true) {
  430. maskset.buffer.splice((buffer.length - matchRslt.index - 1) + opts.suffix.length, 1);
  431. return {
  432. "pos": initialPos,
  433. "remove": (buffer.length - matchRslt.index - 1) + opts.suffix.length
  434. };
  435. }
  436. } else {
  437. if (leadingZero && (matchRslt.index + 1 === pos || (isSelection !== true && decimalPart === 0))) {
  438. maskset.buffer.splice(matchRslt.index + opts.prefix.length, 1);
  439. return {
  440. "pos": matchRslt.index + opts.prefix.length,
  441. "remove": matchRslt.index + opts.prefix.length
  442. };
  443. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  444. return false;
  445. }
  446. }
  447. }
  448. }
  449. return true;
  450. },
  451. definitions: {
  452. "~": {
  453. validator: function (chrs, maskset, pos, strict, opts, isSelection) {
  454. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  455. if (!isValid) {
  456. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  457. if (!isValid) {
  458. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  459. if (isValid === true) {
  460. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts, isSelection);
  461. if (isValid === true && opts.numericInput !== true) {
  462. //handle overwrite when fixed precision
  463. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  464. if (radixPosition !== -1 && (opts.digitsOptional === false || maskset.validPositions[pos]) && opts.numericInput !== true && pos > radixPosition && !strict) {
  465. isValid = {
  466. "pos": pos,
  467. "remove": pos
  468. };
  469. } else {
  470. isValid = {
  471. pos: pos
  472. };
  473. }
  474. }
  475. }
  476. }
  477. }
  478. return isValid;
  479. }
  480. ,
  481. cardinality: 1
  482. }
  483. ,
  484. "+": {
  485. validator: function (chrs, maskset, pos, strict, opts) {
  486. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  487. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  488. if (!strict && chrs === "-") {
  489. if (opts.negationSymbol.back !== "") {
  490. isValid = {
  491. "pos": pos,
  492. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  493. "caret": pos + 1,
  494. "insert": {
  495. "pos": maskset.buffer.length,
  496. "c": opts.negationSymbol.back
  497. }
  498. };
  499. } else {
  500. isValid = {
  501. "pos": pos,
  502. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  503. "caret": pos + 1
  504. };
  505. }
  506. } else {
  507. isValid = true;
  508. }
  509. }
  510. return isValid;
  511. }
  512. ,
  513. cardinality: 1,
  514. placeholder: ""
  515. },
  516. "-": {
  517. validator: function (chrs, maskset, pos, strict, opts) {
  518. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  519. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  520. isValid = true;
  521. }
  522. return isValid;
  523. }
  524. ,
  525. cardinality: 1,
  526. placeholder: ""
  527. }
  528. ,
  529. ":": {
  530. validator: function (chrs, maskset, pos, strict, opts) {
  531. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  532. if (!isValid) {
  533. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  534. isValid = new RegExp(radix).test(chrs);
  535. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  536. isValid = {
  537. "caret": pos + 1
  538. };
  539. }
  540. }
  541. return isValid;
  542. },
  543. cardinality: 1,
  544. placeholder: function (opts) {
  545. return opts.radixPoint;
  546. }
  547. }
  548. }
  549. ,
  550. onUnMask: function (maskedValue, unmaskedValue, opts) {
  551. if (unmaskedValue === "" && opts.nullable === true) {
  552. return unmaskedValue;
  553. }
  554. var processValue = maskedValue.replace(opts.prefix, "");
  555. processValue = processValue.replace(opts.suffix, "");
  556. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  557. if (opts.unmaskAsNumber) {
  558. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  559. return Number(processValue);
  560. }
  561. return processValue;
  562. }
  563. ,
  564. isComplete: function (buffer, opts) {
  565. var maskedValue = buffer.join(""),
  566. bufClone = buffer.slice();
  567. //verify separator positions
  568. opts.postFormat(bufClone, 0, opts);
  569. if (bufClone.join("") !== maskedValue) return false;
  570. var processValue = maskedValue.replace(opts.prefix, "");
  571. processValue = processValue.replace(opts.suffix, "");
  572. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  573. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  574. return isFinite(processValue);
  575. }
  576. ,
  577. onBeforeMask: function (initialValue, opts) {
  578. initialValue = initialValue.toString();
  579. if (opts.numericInput === true) {
  580. initialValue = initialValue.split("").reverse().join("");
  581. }
  582. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  583. var vs = initialValue.split("."),
  584. groupSize = opts.groupSeparator !== "" ? parseInt(opts.groupSize) : 0;
  585. if (vs.length === 2 && (vs[0].length > groupSize || vs[1].length > groupSize))
  586. initialValue = initialValue.replace(".", opts.radixPoint);
  587. }
  588. var kommaMatches = initialValue.match(/,/g);
  589. var dotMatches = initialValue.match(/\./g);
  590. if (dotMatches && kommaMatches) {
  591. if (dotMatches.length > kommaMatches.length) {
  592. initialValue = initialValue.replace(/\./g, "");
  593. initialValue = initialValue.replace(",", opts.radixPoint);
  594. } else if (kommaMatches.length > dotMatches.length) {
  595. initialValue = initialValue.replace(/,/g, "");
  596. initialValue = initialValue.replace(".", opts.radixPoint);
  597. } else { //equal
  598. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  599. }
  600. } else {
  601. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  602. }
  603. if (opts.digits === 0) {
  604. if (initialValue.indexOf(".") !== -1) {
  605. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  606. } else if (initialValue.indexOf(",") !== -1) {
  607. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  608. }
  609. }
  610. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  611. var valueParts = initialValue.split(opts.radixPoint),
  612. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  613. if (parseInt(opts.digits) < decPart.toString().length) {
  614. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  615. //make the initialValue a valid javascript number for the parsefloat
  616. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  617. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  618. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  619. }
  620. }
  621. if (opts.numericInput === true) {
  622. initialValue = initialValue.split("").reverse().join("");
  623. }
  624. return initialValue;
  625. },
  626. canClearPosition: function (maskset, position, lvp, strict, opts) {
  627. var positionInput = maskset.validPositions[position].input,
  628. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  629. position === lvp ||
  630. positionInput === opts.groupSeparator ||
  631. positionInput === opts.negationSymbol.front ||
  632. positionInput === opts.negationSymbol.back;
  633. return canClear;
  634. },
  635. onKeyDown: function (e, buffer, caretPos, opts) {
  636. var $input = $(this);
  637. if (e.ctrlKey) {
  638. switch (e.keyCode) {
  639. case Inputmask.keyCode.UP:
  640. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  641. $input.trigger("setvalue");
  642. break;
  643. case Inputmask.keyCode.DOWN:
  644. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  645. $input.trigger("setvalue");
  646. break;
  647. }
  648. }
  649. }
  650. },
  651. "currency": {
  652. prefix: "$ ",
  653. groupSeparator: ",",
  654. alias: "numeric",
  655. placeholder: "0",
  656. autoGroup: true,
  657. digits: 2,
  658. digitsOptional: false,
  659. clearMaskOnLostFocus: false
  660. },
  661. "decimal": {
  662. alias: "numeric"
  663. },
  664. "integer": {
  665. alias: "numeric",
  666. digits: 0,
  667. radixPoint: ""
  668. },
  669. "percentage": {
  670. alias: "numeric",
  671. digits: 2,
  672. radixPoint: ".",
  673. placeholder: "0",
  674. autoGroup: false,
  675. min: 0,
  676. max: 100,
  677. suffix: " %",
  678. allowPlus: false,
  679. allowMinus: false
  680. }
  681. })
  682. ;
  683. return Inputmask;
  684. }));