inputmask.numeric.extensions.js 27 KB

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