inputmask.numeric.extensions.js 25 KB

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