inputmask.numeric.extensions.js 23 KB

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