inputmask.numeric.extensions.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. if (opts.decimalProtect) opts.radixPointDefinitionSymbol = ":";
  88. var dq = opts.digits.toString().split(",");
  89. if (isFinite(dq[0] && dq[1] && isFinite(dq[1]))) {
  90. mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}";
  91. } else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
  92. if (opts.digitsOptional) {
  93. mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{1," + opts.digits + "}]";
  94. } else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + 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. ,
  127. integerDigits: "+", //number of integerDigits
  128. integerOptional: true,
  129. prefix: "",
  130. suffix: "",
  131. rightAlign: true,
  132. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  133. min: null, //minimum value
  134. max: null, //maximum value
  135. step: 1,
  136. insertMode: true,
  137. autoUnmask: false,
  138. unmaskAsNumber: false,
  139. inputmode: "numeric",
  140. postFormat: function (buffer, pos, opts) { //this needs to be removed // this is crap
  141. // console.log(buffer);
  142. if (opts.numericInput === true) {
  143. buffer = buffer.reverse();
  144. if (isFinite(pos)) {
  145. pos = buffer.join("").length - pos - 1;
  146. }
  147. }
  148. var i, l;
  149. //position overflow corrections
  150. pos = pos >= buffer.length ? buffer.length - 1 : (pos < 0 ? 0 : pos);
  151. var charAtPos = buffer[pos];
  152. var cbuf = buffer.slice();
  153. if (charAtPos === opts.groupSeparator) {
  154. cbuf.splice(pos--, 1);
  155. charAtPos = cbuf[pos];
  156. }
  157. var isNegative = cbuf.join("").match(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)));
  158. isNegative = isNegative !== null && isNegative.length === 1;
  159. if (pos > ((isNegative ? opts.negationSymbol.front.length : 0 ) + opts.prefix.length ) && (pos < (cbuf.length - opts.suffix.length))) {
  160. //mark current pos
  161. cbuf[pos]= "!";
  162. }
  163. var bufVal = cbuf.join(""), bufValOrigin = bufVal;
  164. if (isNegative) {
  165. bufVal = bufVal.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "");
  166. bufVal = bufVal.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  167. }
  168. bufVal = bufVal.replace(new RegExp(Inputmask.escapeRegex(opts.suffix) + "$"), "");
  169. bufVal = bufVal.replace(new RegExp("^" + Inputmask.escapeRegex(opts.prefix)), "");
  170. if (bufVal.length > 0 && opts.autoGroup || bufVal.indexOf(opts.groupSeparator) !== -1) {
  171. var escapedGroupSeparator = Inputmask.escapeRegex(opts.groupSeparator);
  172. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  173. var radixSplit = bufVal.split(charAtPos === opts.radixPoint ? "!" : opts.radixPoint);
  174. bufVal = opts.radixPoint === "" ? bufVal : radixSplit[0];
  175. if (charAtPos !== opts.negationSymbol.front) bufVal = bufVal.replace("!", "?");
  176. if (bufVal.length > opts.groupSize) {
  177. var reg = new RegExp("([-\+]?[\\d\?]+)([\\d\?]{" + opts.groupSize + "})");
  178. while (reg.test(bufVal) && opts.groupSeparator !== "") {
  179. bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2");
  180. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  181. }
  182. }
  183. bufVal = bufVal.replace("?", "!");
  184. if (opts.radixPoint !== "" && radixSplit.length > 1) {
  185. bufVal += (charAtPos === opts.radixPoint ? "!" : opts.radixPoint) + radixSplit[1];
  186. }
  187. }
  188. bufVal = opts.prefix + bufVal + opts.suffix;
  189. if (isNegative) {
  190. bufVal = opts.negationSymbol.front + bufVal + opts.negationSymbol.back;
  191. }
  192. var needsRefresh = bufValOrigin !== bufVal;
  193. if (needsRefresh) {
  194. buffer.length = bufVal.length; //align the length
  195. for (i = 0, l = bufVal.length; i < l; i++) {
  196. buffer[i] = bufVal.charAt(i);
  197. }
  198. }
  199. var newPos = $.inArray("!", bufVal);
  200. if (newPos === -1) newPos = pos;
  201. buffer[newPos] = charAtPos;
  202. // console.log("formatted " + buffer + " refresh " + needsRefresh);
  203. newPos = (opts.numericInput && isFinite(pos)) ? buffer.join("").length - newPos - 1 : newPos;
  204. if (opts.numericInput) {
  205. buffer = buffer.reverse();
  206. if ($.inArray(opts.radixPoint, buffer) < newPos && (buffer.join("").length - opts.suffix.length) !== newPos) {
  207. newPos = newPos - 1;
  208. }
  209. }
  210. return {
  211. pos: newPos,
  212. "refreshFromBuffer": needsRefresh,
  213. "buffer": buffer,
  214. isNegative: isNegative
  215. };
  216. }
  217. ,
  218. onBeforeWrite: function (e, buffer, caretPos, opts) {
  219. var rslt;
  220. if (e && (e.type === "blur" || e.type === "checkval" || e.type === "keydown")) {
  221. var maskedValue = opts.numericInput ? buffer.slice().reverse().join("") : buffer.join(""),
  222. processValue = maskedValue.replace(opts.prefix, "");
  223. processValue = processValue.replace(opts.suffix, "");
  224. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  225. if (opts.radixPoint === ",") processValue = processValue.replace(opts.radixPoint, ".");
  226. //handle negation symbol
  227. var isNegative = processValue.match(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"));
  228. isNegative = isNegative !== null && isNegative.length === 1;
  229. processValue = processValue.replace(new RegExp("[-" + Inputmask.escapeRegex(opts.negationSymbol.front) + "]", "g"), "");
  230. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  231. //strip placeholder at the end
  232. if (isNaN(opts.placeholder)) {
  233. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.placeholder), "g"), "");
  234. }
  235. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  236. if (processValue !== "" && isFinite(processValue)) {
  237. var floatValue = parseFloat(processValue),
  238. signedFloatValue = isNegative ? floatValue * -1 : floatValue;
  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. processValue = floatValue.toString().replace(".", opts.radixPoint).split('');
  250. if (isFinite(opts.digits)) {
  251. var radixPosition = $.inArray(opts.radixPoint, processValue);
  252. var rpb = $.inArray(opts.radixPoint, maskedValue);
  253. if (radixPosition === -1) {
  254. processValue.push(opts.radixPoint);
  255. radixPosition = processValue.length - 1;
  256. }
  257. for (var i = 1; i <= opts.digits; i++) {
  258. if (!opts.digitsOptional && (processValue[radixPosition + i] === undefined || processValue[radixPosition + i] === opts.placeholder.charAt(0))) {
  259. processValue[radixPosition + i] = "0";
  260. } else if (rpb !== -1 && maskedValue[rpb + i] !== undefined) {
  261. processValue[radixPosition + i] = processValue[radixPosition + i] || maskedValue[rpb + i];
  262. }
  263. }
  264. if (processValue[processValue.length - 1] === opts.radixPoint) {
  265. delete processValue[processValue.length - 1];
  266. }
  267. }
  268. if ((floatValue.toString() !== processValue && floatValue.toString() + "." !== processValue) || isNegative) {
  269. processValue = (opts.prefix + processValue.join("")).split("");
  270. if (isNegative && (floatValue !== 0 || e.type !== "blur")) {
  271. processValue.unshift(opts.negationSymbol.front);
  272. processValue.push(opts.negationSymbol.back);
  273. }
  274. if (opts.numericInput) processValue = processValue.reverse();
  275. rslt = opts.postFormat(processValue, opts.numericInput ? caretPos : caretPos - 1, opts);
  276. if (rslt.buffer) rslt.refreshFromBuffer = rslt.buffer.join("") !== buffer.join("");
  277. return rslt;
  278. }
  279. }
  280. }
  281. if (opts.autoGroup) {
  282. rslt = opts.postFormat(buffer, opts.numericInput ? caretPos : (caretPos - 1), opts);
  283. rslt.caret =
  284. ((caretPos < (rslt.isNegative ? opts.negationSymbol.front.length : 0) + opts.prefix.length) ||
  285. (caretPos > (rslt.buffer.length - (rslt.isNegative ? opts.negationSymbol.back.length : 0))))
  286. ? rslt.pos : rslt.pos + 1;
  287. return rslt;
  288. }
  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. ,
  301. signHandler: function (chrs, maskset, pos, strict, opts) {
  302. if (!strict && (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+")) {
  303. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  304. if (matchRslt && matchRslt[0].length > 0) {
  305. if (maskset.buffer[matchRslt.index] === (chrs === "-" ? "+" : opts.negationSymbol.front)) {
  306. if (chrs === "-") {
  307. if (opts.negationSymbol.back !== "") {
  308. return {
  309. "pos": 0,
  310. "c": opts.negationSymbol.front,
  311. "remove": 0,
  312. "caret": pos,
  313. "insert": {
  314. "pos": maskset.buffer.length - 1,
  315. "c": opts.negationSymbol.back
  316. }
  317. };
  318. } else {
  319. return {
  320. "pos": 0,
  321. "c": opts.negationSymbol.front,
  322. "remove": 0,
  323. "caret": pos
  324. };
  325. }
  326. } else {
  327. if (opts.negationSymbol.back !== "") {
  328. return {
  329. "pos": 0,
  330. "c": "+",
  331. "remove": [0, maskset.buffer.length - 1],
  332. "caret": pos
  333. };
  334. } else {
  335. return {
  336. "pos": 0,
  337. "c": "+",
  338. "remove": 0,
  339. "caret": pos
  340. };
  341. }
  342. }
  343. } else if (maskset.buffer[0] === (chrs === "-" ? opts.negationSymbol.front : "+")) {
  344. if (chrs === "-" && opts.negationSymbol.back !== "") {
  345. return {
  346. "remove": [0, maskset.buffer.length - 1],
  347. "caret": pos - 1
  348. };
  349. } else {
  350. return {
  351. "remove": 0,
  352. "caret": pos - 1
  353. };
  354. }
  355. } else {
  356. if (chrs === "-") {
  357. if (opts.negationSymbol.back !== "") {
  358. return {
  359. "pos": 0,
  360. "c": opts.negationSymbol.front,
  361. "caret": pos + 1,
  362. "insert": {
  363. "pos": maskset.buffer.length,
  364. "c": opts.negationSymbol.back
  365. }
  366. };
  367. } else {
  368. return {
  369. "pos": 0,
  370. "c": opts.negationSymbol.front,
  371. "caret": pos + 1
  372. };
  373. }
  374. } else {
  375. return {
  376. "pos": 0,
  377. "c": chrs,
  378. "caret": pos + 1
  379. };
  380. }
  381. }
  382. }
  383. }
  384. return false;
  385. }
  386. ,
  387. radixHandler: function (chrs, maskset, pos, strict, opts) {
  388. if (!strict && opts.numericInput !== true) {
  389. //if ($.inArray(chrs, [",", "."]) !== -1) chrs = opts.radixPoint;
  390. if (chrs === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  391. var radixPos = $.inArray(opts.radixPoint, maskset.buffer),
  392. integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  393. if (radixPos !== -1 && maskset.validPositions[radixPos]) {
  394. if (maskset.validPositions[radixPos - 1]) {
  395. return {
  396. "caret": radixPos + 1
  397. };
  398. } else {
  399. return {
  400. "pos": integerValue.index,
  401. c: integerValue[0],
  402. "caret": radixPos + 1
  403. };
  404. }
  405. } else if (!integerValue || (integerValue["0"] === "0" && (integerValue.index + 1) !== pos)) {
  406. maskset.buffer[integerValue ? integerValue.index : pos] = "0";
  407. return {
  408. "pos": (integerValue ? integerValue.index : pos) + 1,
  409. c: opts.radixPoint
  410. };
  411. }
  412. }
  413. }
  414. return false;
  415. }
  416. ,
  417. leadingZeroHandler: function (chrs, maskset, pos, strict, opts, isSelection) {
  418. if (!strict) {
  419. var buffer = maskset.buffer.slice("");
  420. buffer.splice(0, opts.prefix.length);
  421. buffer.splice(buffer.length - opts.suffix.length, opts.suffix.length);
  422. if (opts.numericInput === true) {
  423. var buffer = buffer.reverse();
  424. var bufferChar = buffer[0];
  425. if (bufferChar === "0" && maskset.validPositions[pos - 1] === undefined) {
  426. return {
  427. "pos": pos,
  428. "remove": buffer.length - 1
  429. };
  430. }
  431. } else {
  432. pos = pos - opts.prefix.length;
  433. var radixPosition = $.inArray(opts.radixPoint, buffer),
  434. matchRslt = buffer.slice(0, radixPosition !== -1 ? radixPosition : undefined).join("").match(opts.regex.integerNPart(opts));
  435. if (matchRslt && (radixPosition === -1 || pos <= radixPosition)) {
  436. var decimalPart = radixPosition === -1 ? 0 : parseInt(buffer.slice(radixPosition + 1).join(""));
  437. if (matchRslt["0"].indexOf(opts.placeholder !== "" ? opts.placeholder.charAt(0) : "0") === 0 &&
  438. (matchRslt.index + 1 === pos || (isSelection !== true && decimalPart === 0))) {
  439. maskset.buffer.splice(matchRslt.index + opts.prefix.length, 1);
  440. return {
  441. "pos": matchRslt.index + opts.prefix.length,
  442. "remove": matchRslt.index + opts.prefix.length
  443. };
  444. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  445. return false;
  446. }
  447. }
  448. }
  449. }
  450. return true;
  451. }
  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) {
  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. "-": {
  520. validator: function (chrs, maskset, pos, strict, opts) {
  521. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  522. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  523. isValid = true;
  524. }
  525. return isValid;
  526. }
  527. ,
  528. cardinality: 1,
  529. placeholder: ""
  530. }
  531. ,
  532. ":": {
  533. validator: function (chrs, maskset, pos, strict, opts) {
  534. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  535. if (!isValid) {
  536. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + "]";
  537. isValid = new RegExp(radix).test(chrs);
  538. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  539. isValid = {
  540. "caret": pos + 1
  541. };
  542. }
  543. }
  544. return isValid ? {
  545. c: opts.radixPoint
  546. } : isValid;
  547. }
  548. ,
  549. cardinality: 1,
  550. placeholder: function (opts) {
  551. return opts.radixPoint;
  552. }
  553. }
  554. }
  555. ,
  556. onUnMask: function (maskedValue, unmaskedValue, opts) {
  557. if (unmaskedValue === "" && opts.nullable === true) {
  558. return unmaskedValue;
  559. }
  560. var processValue = maskedValue.replace(opts.prefix, "");
  561. processValue = processValue.replace(opts.suffix, "");
  562. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  563. if (opts.unmaskAsNumber) {
  564. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  565. return Number(processValue);
  566. }
  567. return processValue;
  568. }
  569. ,
  570. isComplete: function (buffer, opts) {
  571. var maskedValue = buffer.join(""),
  572. bufClone = buffer.slice();
  573. //verify separator positions
  574. opts.postFormat(bufClone, 0, opts);
  575. if (bufClone.join("") !== maskedValue) return false;
  576. var processValue = maskedValue.replace(opts.prefix, "");
  577. processValue = processValue.replace(opts.suffix, "");
  578. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  579. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  580. return isFinite(processValue);
  581. }
  582. ,
  583. onBeforeMask: function (initialValue, opts) {
  584. if (opts.numericInput === true) {
  585. initialValue = initialValue.split("").reverse().join("");
  586. }
  587. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  588. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  589. } else {
  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. }
  606. if (opts.digits === 0) {
  607. if (initialValue.indexOf(".") !== -1) {
  608. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  609. } else if (initialValue.indexOf(",") !== -1) {
  610. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  611. }
  612. }
  613. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  614. var valueParts = initialValue.split(opts.radixPoint),
  615. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  616. if (parseInt(opts.digits) < decPart.toString().length) {
  617. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  618. //make the initialValue a valid javascript number for the parsefloat
  619. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  620. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  621. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  622. }
  623. }
  624. if (opts.numericInput === true) {
  625. initialValue = initialValue.split("").reverse().join("");
  626. }
  627. return initialValue.toString();
  628. }
  629. ,
  630. canClearPosition: function (maskset, position, lvp, strict, opts) {
  631. var positionInput = maskset.validPositions[position].input,
  632. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  633. position === lvp ||
  634. positionInput === opts.groupSeparator ||
  635. positionInput === opts.negationSymbol.front ||
  636. positionInput === opts.negationSymbol.back;
  637. return canClear;
  638. }
  639. ,
  640. onKeyDown: function (e, buffer, caretPos, opts) {
  641. var $input = $(this);
  642. if (e.ctrlKey) {
  643. switch (e.keyCode) {
  644. case Inputmask.keyCode.UP:
  645. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  646. $input.trigger("setvalue");
  647. break;
  648. case Inputmask.keyCode.DOWN:
  649. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  650. $input.trigger("setvalue");
  651. break;
  652. }
  653. }
  654. }
  655. },
  656. "currency": {
  657. prefix: "$ ",
  658. groupSeparator: ",",
  659. alias: "numeric",
  660. placeholder: "0",
  661. autoGroup: true,
  662. digits: 2,
  663. digitsOptional: false,
  664. clearMaskOnLostFocus: false
  665. }
  666. ,
  667. "decimal": {
  668. alias: "numeric"
  669. }
  670. ,
  671. "integer": {
  672. alias: "numeric",
  673. digits: 0,
  674. radixPoint: ""
  675. }
  676. ,
  677. "percentage": {
  678. alias: "numeric",
  679. digits: 2,
  680. radixPoint: ".",
  681. placeholder: "0",
  682. autoGroup: false,
  683. min: 0,
  684. max: 100,
  685. suffix: " %",
  686. allowPlus: false,
  687. allowMinus: false
  688. }
  689. })
  690. ;
  691. return Inputmask;
  692. }));