inputmask.numeric.extensions.js 25 KB

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