inputmask.numeric.extensions.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 && pos > opts.prefix.length && pos < (buffer.length - opts.suffix.length)) {
  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 (e.type === "blur") {
  239. if (opts.min !== null && isFinite(opts.min) && signedFloatValue < parseFloat(opts.min)) {
  240. floatValue = Math.abs(opts.min);
  241. isNegative = opts.min < 0;
  242. maskedValue = undefined;
  243. }
  244. else if (opts.max !== null && isFinite(opts.max) && signedFloatValue > parseFloat(opts.max)) {
  245. floatValue = Math.abs(opts.max);
  246. isNegative = opts.max < 0;
  247. maskedValue = undefined;
  248. }
  249. }
  250. processValue = floatValue.toString().replace(".", opts.radixPoint).split('');
  251. if (isFinite(opts.digits)) {
  252. var radixPosition = $.inArray(opts.radixPoint, processValue);
  253. var rpb = $.inArray(opts.radixPoint, maskedValue);
  254. if (radixPosition === -1) {
  255. processValue.push(opts.radixPoint);
  256. radixPosition = processValue.length - 1;
  257. }
  258. for (var i = 1; i <= opts.digits; i++) {
  259. if (!opts.digitsOptional && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  260. processValue[radixPosition + i] = "0";
  261. } else if (rpb !== -1 && maskedValue[rpb + i] !== undefined) {
  262. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  263. }
  264. }
  265. if (processValue[processValue.length - 1] === opts.radixPoint) {
  266. delete processValue[processValue.length - 1];
  267. }
  268. }
  269. if ((floatValue.toString() !== processValue && floatValue.toString() + "." !== processValue) || isNegative) {
  270. processValue = (opts.prefix + processValue.join("")).split("");
  271. if (isNegative && (floatValue !== 0 || e.type !== "blur")) {
  272. processValue.unshift(opts.negationSymbol.front);
  273. processValue.push(opts.negationSymbol.back);
  274. }
  275. if (opts.numericInput) processValue = processValue.reverse();
  276. rslt = opts.postFormat(processValue, opts.numericInput ? caretPos : caretPos - 1, opts);
  277. if (rslt.buffer) rslt.refreshFromBuffer = rslt.buffer.join("") !== buffer.join("");
  278. return rslt;
  279. }
  280. }
  281. }
  282. if (opts.autoGroup) {
  283. rslt = opts.postFormat(buffer, opts.numericInput ? caretPos : (caretPos - 1), opts);
  284. rslt.caret =
  285. ((caretPos < (rslt.isNegative ? opts.negationSymbol.front.length : 0) + opts.prefix.length) ||
  286. (caretPos > (rslt.buffer.length - (rslt.isNegative ? opts.negationSymbol.back.length : 0))))
  287. ? rslt.pos : rslt.pos + 1;
  288. return rslt;
  289. }
  290. },
  291. regex: {
  292. integerPart: function (opts) {
  293. return new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?\\d+");
  294. }
  295. ,
  296. integerNPart: function (opts) {
  297. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + Inputmask.escapeRegex(opts.placeholder.charAt(0)) + "]+");
  298. }
  299. },
  300. signHandler: function (chrs, maskset, pos, strict, opts) {
  301. if (!strict && (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+")) {
  302. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  303. if (matchRslt && matchRslt[0].length > 0) {
  304. if (maskset.buffer[matchRslt.index] === (chrs === "-" ? "+" : opts.negationSymbol.front)) {
  305. if (chrs === "-") {
  306. if (opts.negationSymbol.back !== "") {
  307. return {
  308. "pos": 0,
  309. "c": opts.negationSymbol.front,
  310. "remove": 0,
  311. "caret": pos,
  312. "insert": {
  313. "pos": maskset.buffer.length - 1,
  314. "c": opts.negationSymbol.back
  315. }
  316. };
  317. } else {
  318. return {
  319. "pos": 0,
  320. "c": opts.negationSymbol.front,
  321. "remove": 0,
  322. "caret": pos
  323. };
  324. }
  325. } else {
  326. if (opts.negationSymbol.back !== "") {
  327. return {
  328. "pos": 0,
  329. "c": "+",
  330. "remove": [0, maskset.buffer.length - 1],
  331. "caret": pos
  332. };
  333. } else {
  334. return {
  335. "pos": 0,
  336. "c": "+",
  337. "remove": 0,
  338. "caret": pos
  339. };
  340. }
  341. }
  342. } else if (maskset.buffer[0] === (chrs === "-" ? opts.negationSymbol.front : "+")) {
  343. if (chrs === "-" && opts.negationSymbol.back !== "") {
  344. return {
  345. "remove": [0, maskset.buffer.length - 1],
  346. "caret": pos - 1
  347. };
  348. } else {
  349. return {
  350. "remove": 0,
  351. "caret": pos - 1
  352. };
  353. }
  354. } else {
  355. if (chrs === "-") {
  356. if (opts.negationSymbol.back !== "") {
  357. return {
  358. "pos": 0,
  359. "c": opts.negationSymbol.front,
  360. "caret": pos + 1,
  361. "insert": {
  362. "pos": maskset.buffer.length,
  363. "c": opts.negationSymbol.back
  364. }
  365. };
  366. } else {
  367. return {
  368. "pos": 0,
  369. "c": opts.negationSymbol.front,
  370. "caret": pos + 1
  371. };
  372. }
  373. } else {
  374. return {
  375. "pos": 0,
  376. "c": chrs,
  377. "caret": pos + 1
  378. };
  379. }
  380. }
  381. }
  382. }
  383. return false;
  384. }
  385. ,
  386. radixHandler: function (chrs, maskset, pos, strict, opts) {
  387. if (!strict && opts.numericInput !== true) {
  388. //if ($.inArray(chrs, [",", "."]) !== -1) chrs = opts.radixPoint;
  389. if (chrs === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  390. var radixPos = $.inArray(opts.radixPoint, maskset.buffer),
  391. integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  392. if (radixPos !== -1 && maskset.validPositions[radixPos]) {
  393. if (maskset.validPositions[radixPos - 1]) {
  394. return {
  395. "caret": radixPos + 1
  396. };
  397. } else {
  398. return {
  399. "pos": integerValue.index,
  400. c: integerValue[0],
  401. "caret": radixPos + 1
  402. };
  403. }
  404. } else if (!integerValue || (integerValue["0"] === "0" && (integerValue.index + 1) !== pos)) {
  405. maskset.buffer[integerValue ? integerValue.index : pos] = "0";
  406. return {
  407. "pos": (integerValue ? integerValue.index : pos) + 1,
  408. c: opts.radixPoint
  409. };
  410. }
  411. }
  412. }
  413. return false;
  414. },
  415. leadingZeroHandler: function (chrs, maskset, pos, strict, opts, isSelection) {
  416. if (!strict) {
  417. var initialPos = pos,
  418. buffer = opts.numericInput === true ? maskset.buffer.slice("").reverse() : maskset.buffer.slice("");
  419. if (opts.numericInput) {
  420. pos = buffer.join("").length - pos - 1;
  421. }
  422. buffer.splice(0, opts.prefix.length);
  423. buffer.splice(buffer.length - opts.suffix.length, opts.suffix.length);
  424. pos = pos - opts.prefix.length;
  425. var radixPosition = $.inArray(opts.radixPoint, buffer),
  426. matchRslt = buffer.slice(0, radixPosition !== -1 ? radixPosition : undefined).join("").match(opts.regex.integerNPart(opts));
  427. if (matchRslt && (radixPosition === -1 || pos <= radixPosition || opts.numericInput)) {
  428. var decimalPart = radixPosition === -1 ? 0 : parseInt(buffer.slice(radixPosition + 1).join("")),
  429. leadingZero = matchRslt["0"].indexOf(opts.placeholder !== "" ? opts.placeholder.charAt(0) : "0") === 0;
  430. if (opts.numericInput) {
  431. if (leadingZero && decimalPart !== 0 && isSelection !== true) {
  432. maskset.buffer.splice((buffer.length - matchRslt.index - 1) + opts.suffix.length, 1);
  433. return {
  434. "pos": initialPos,
  435. "remove": (buffer.length - matchRslt.index - 1) + opts.suffix.length
  436. };
  437. }
  438. } else {
  439. if (leadingZero && (matchRslt.index + 1 === pos || (isSelection !== true && decimalPart === 0))) {
  440. maskset.buffer.splice(matchRslt.index + opts.prefix.length, 1);
  441. return {
  442. "pos": matchRslt.index + opts.prefix.length,
  443. "remove": matchRslt.index + opts.prefix.length
  444. };
  445. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  446. return false;
  447. }
  448. }
  449. }
  450. }
  451. return true;
  452. },
  453. definitions: {
  454. "~": {
  455. validator: function (chrs, maskset, pos, strict, opts, isSelection) {
  456. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  457. if (!isValid) {
  458. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  459. if (!isValid) {
  460. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  461. if (isValid === true) {
  462. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts, isSelection);
  463. if (isValid === true && opts.numericInput !== true) {
  464. //handle overwrite when fixed precision
  465. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  466. if (radixPosition !== -1 && (opts.digitsOptional === false || maskset.validPositions[pos]) && opts.numericInput !== true && pos > radixPosition && !strict) {
  467. isValid = {
  468. "pos": pos,
  469. "remove": pos
  470. };
  471. } else {
  472. isValid = {
  473. pos: pos
  474. };
  475. }
  476. }
  477. }
  478. }
  479. }
  480. return isValid;
  481. }
  482. ,
  483. cardinality: 1
  484. }
  485. ,
  486. "+": {
  487. validator: function (chrs, maskset, pos, strict, opts) {
  488. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  489. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  490. if (!strict && chrs === "-") {
  491. if (opts.negationSymbol.back !== "") {
  492. isValid = {
  493. "pos": pos,
  494. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  495. "caret": pos + 1,
  496. "insert": {
  497. "pos": maskset.buffer.length,
  498. "c": opts.negationSymbol.back
  499. }
  500. };
  501. } else {
  502. isValid = {
  503. "pos": pos,
  504. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  505. "caret": pos + 1
  506. };
  507. }
  508. } else {
  509. isValid = true;
  510. }
  511. }
  512. return isValid;
  513. }
  514. ,
  515. cardinality: 1,
  516. placeholder: ""
  517. },
  518. "-": {
  519. validator: function (chrs, maskset, pos, strict, opts) {
  520. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  521. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  522. isValid = true;
  523. }
  524. return isValid;
  525. }
  526. ,
  527. cardinality: 1,
  528. placeholder: ""
  529. }
  530. ,
  531. ":": {
  532. validator: function (chrs, maskset, pos, strict, opts) {
  533. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  534. if (!isValid) {
  535. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  536. isValid = new RegExp(radix).test(chrs);
  537. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  538. isValid = {
  539. "caret": pos + 1
  540. };
  541. }
  542. }
  543. return isValid;
  544. },
  545. cardinality: 1,
  546. placeholder: function (opts) {
  547. return opts.radixPoint;
  548. }
  549. }
  550. }
  551. ,
  552. onUnMask: function (maskedValue, unmaskedValue, opts) {
  553. if (unmaskedValue === "" && opts.nullable === true) {
  554. return unmaskedValue;
  555. }
  556. var processValue = maskedValue.replace(opts.prefix, "");
  557. processValue = processValue.replace(opts.suffix, "");
  558. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  559. if (opts.unmaskAsNumber) {
  560. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  561. return Number(processValue);
  562. }
  563. return processValue;
  564. }
  565. ,
  566. isComplete: function (buffer, opts) {
  567. var maskedValue = buffer.join(""),
  568. bufClone = buffer.slice();
  569. //verify separator positions
  570. opts.postFormat(bufClone, 0, opts);
  571. if (bufClone.join("") !== maskedValue) return false;
  572. var processValue = maskedValue.replace(opts.prefix, "");
  573. processValue = processValue.replace(opts.suffix, "");
  574. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  575. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  576. return isFinite(processValue);
  577. }
  578. ,
  579. onBeforeMask: function (initialValue, opts) {
  580. initialValue = initialValue.toString();
  581. if (opts.numericInput === true) {
  582. initialValue = initialValue.split("").reverse().join("");
  583. }
  584. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  585. var vs = initialValue.split("."),
  586. groupSize = opts.groupSeparator !== "" ? parseInt(opts.groupSize) : 0;
  587. if (vs.length === 2 && (vs[0].length > groupSize || vs[1].length > groupSize))
  588. initialValue = initialValue.replace(".", opts.radixPoint);
  589. }
  590. var kommaMatches = initialValue.match(/,/g);
  591. var dotMatches = initialValue.match(/\./g);
  592. if (dotMatches && kommaMatches) {
  593. if (dotMatches.length > kommaMatches.length) {
  594. initialValue = initialValue.replace(/\./g, "");
  595. initialValue = initialValue.replace(",", opts.radixPoint);
  596. } else if (kommaMatches.length > dotMatches.length) {
  597. initialValue = initialValue.replace(/,/g, "");
  598. initialValue = initialValue.replace(".", opts.radixPoint);
  599. } else { //equal
  600. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  601. }
  602. } else {
  603. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  604. }
  605. if (opts.digits === 0) {
  606. if (initialValue.indexOf(".") !== -1) {
  607. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  608. } else if (initialValue.indexOf(",") !== -1) {
  609. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  610. }
  611. }
  612. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  613. var valueParts = initialValue.split(opts.radixPoint),
  614. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  615. if (parseInt(opts.digits) < decPart.toString().length) {
  616. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  617. //make the initialValue a valid javascript number for the parsefloat
  618. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  619. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  620. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  621. }
  622. }
  623. if (opts.numericInput === true) {
  624. initialValue = initialValue.split("").reverse().join("");
  625. }
  626. return initialValue;
  627. },
  628. canClearPosition: function (maskset, position, lvp, strict, opts) {
  629. var positionInput = maskset.validPositions[position].input,
  630. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  631. position === lvp ||
  632. positionInput === opts.groupSeparator ||
  633. positionInput === opts.negationSymbol.front ||
  634. positionInput === opts.negationSymbol.back;
  635. return canClear;
  636. },
  637. onKeyDown: function (e, buffer, caretPos, opts) {
  638. var $input = $(this);
  639. if (e.ctrlKey) {
  640. switch (e.keyCode) {
  641. case Inputmask.keyCode.UP:
  642. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  643. $input.trigger("setvalue");
  644. break;
  645. case Inputmask.keyCode.DOWN:
  646. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  647. $input.trigger("setvalue");
  648. break;
  649. }
  650. }
  651. }
  652. },
  653. "currency": {
  654. prefix: "$ ",
  655. groupSeparator: ",",
  656. alias: "numeric",
  657. placeholder: "0",
  658. autoGroup: true,
  659. digits: 2,
  660. digitsOptional: false,
  661. clearMaskOnLostFocus: false
  662. },
  663. "decimal": {
  664. alias: "numeric"
  665. },
  666. "integer": {
  667. alias: "numeric",
  668. digits: 0,
  669. radixPoint: ""
  670. },
  671. "percentage": {
  672. alias: "numeric",
  673. digits: 2,
  674. radixPoint: ".",
  675. placeholder: "0",
  676. autoGroup: false,
  677. min: 0,
  678. max: 100,
  679. suffix: " %",
  680. allowPlus: false,
  681. allowMinus: false
  682. }
  683. })
  684. ;
  685. return Inputmask;
  686. }));