inputmask.numeric.extensions.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. }
  390. }
  391. }
  392. return isValid;
  393. },
  394. definitions: {
  395. '~': {
  396. validator: function(chrs, maskset, pos, strict, opts) {
  397. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  398. if (!isValid) {
  399. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  400. if (!isValid) {
  401. isValid = strict ? new RegExp("[0-9" + inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  402. if (isValid === true) {
  403. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  404. if (isValid === true) {
  405. //handle overwrite when fixed precision
  406. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  407. if (radixPosition != -1 && opts.digitsOptional === false && pos > radixPosition && !strict) {
  408. isValid = {
  409. "pos": pos,
  410. "remove": pos
  411. };
  412. } else isValid = {
  413. pos: pos
  414. };
  415. }
  416. }
  417. }
  418. }
  419. return isValid;
  420. },
  421. cardinality: 1,
  422. prevalidator: null
  423. },
  424. '+': {
  425. validator: function(chrs, maskset, pos, strict, opts) {
  426. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts),
  427. nbl;
  428. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs == "-") || (opts.allowPlus && chrs == "+"))) {
  429. if (chrs == "-") {
  430. if (opts.negationSymbol.back != "")
  431. isValid = {
  432. "pos": pos,
  433. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  434. "caret": pos + 1,
  435. "insert": {
  436. "pos": maskset["buffer"].length,
  437. "c": opts.negationSymbol.back
  438. }
  439. };
  440. else isValid = {
  441. "pos": pos,
  442. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  443. "caret": pos + 1
  444. };
  445. } else {
  446. isValid = true;
  447. }
  448. }
  449. return isValid;
  450. },
  451. cardinality: 1,
  452. prevalidator: null,
  453. placeholder: ''
  454. },
  455. '-': {
  456. validator: function(chrs, maskset, pos, strict, opts) {
  457. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  458. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  459. isValid = true;
  460. }
  461. return isValid;
  462. },
  463. cardinality: 1,
  464. prevalidator: null,
  465. placeholder: ''
  466. },
  467. ':': {
  468. validator: function(chrs, maskset, pos, strict, opts) {
  469. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  470. if (!isValid) {
  471. var radix = "[" + inputmask.escapeRegex(opts.radixPoint) + ",\\." + "]";
  472. isValid = new RegExp(radix).test(chrs);
  473. if (isValid && maskset["validPositions"][pos] && maskset["validPositions"][pos]["match"].placeholder == opts.radixPoint) {
  474. isValid = {
  475. "caret": pos + 1
  476. };
  477. }
  478. }
  479. return isValid ? {
  480. c: opts.radixPoint
  481. } : isValid;
  482. },
  483. cardinality: 1,
  484. prevalidator: null,
  485. placeholder: function(opts) {
  486. return opts.radixPoint;
  487. }
  488. }
  489. },
  490. onUnMask: function(maskedValue, unmaskedValue, opts) {
  491. var processValue = maskedValue.replace(opts.prefix, "");
  492. processValue = processValue.replace(opts.suffix, "");
  493. processValue = processValue.replace(new RegExp(inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  494. if (opts.unmaskAsNumber) {
  495. processValue = processValue.replace(inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  496. return Number(processValue);
  497. }
  498. return processValue;
  499. },
  500. isComplete: function(buffer, opts) {
  501. var maskedValue = buffer.join(''),
  502. bufClone = buffer.slice();
  503. //verify separator positions
  504. opts.postFormat(bufClone, 0, true, opts);
  505. if (bufClone.join('') != maskedValue) return false;
  506. var processValue = maskedValue.replace(opts.prefix, "");
  507. processValue = processValue.replace(opts.suffix, "");
  508. processValue = processValue.replace(new RegExp(inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  509. if (opts.radixPoint === ",") processValue = processValue.replace(inputmask.escapeRegex(opts.radixPoint), ".");
  510. return isFinite(processValue);
  511. },
  512. onBeforeMask: function(initialValue, opts) {
  513. if (opts.radixPoint != "" && isFinite(initialValue)) {
  514. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  515. } else {
  516. var kommaMatches = initialValue.match(/,/g);
  517. var dotMatches = initialValue.match(/\./g);
  518. if (dotMatches && kommaMatches) {
  519. if (dotMatches.length > kommaMatches.length) {
  520. initialValue = initialValue.replace(/\./g, "");
  521. initialValue = initialValue.replace(",", opts.radixPoint);
  522. } else if (kommaMatches.length > dotMatches.length) {
  523. initialValue = initialValue.replace(/,/g, "");
  524. initialValue = initialValue.replace(".", opts.radixPoint);
  525. } else { //equal
  526. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  527. }
  528. } else {
  529. initialValue = initialValue.replace(new RegExp(inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  530. }
  531. }
  532. if (opts.digits == 0) {
  533. if (initialValue.indexOf(".") != -1) {
  534. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  535. } else if (initialValue.indexOf(",") != -1) {
  536. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  537. }
  538. }
  539. if (opts.radixPoint != "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) != -1) {
  540. var valueParts = initialValue.split(opts.radixPoint),
  541. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  542. if (parseInt(opts.digits) < decPart.toString().length) {
  543. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  544. //make the initialValue a valid javascript number for the parsefloat
  545. initialValue = initialValue.replace(inputmask.escapeRegex(opts.radixPoint), ".");
  546. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  547. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  548. }
  549. }
  550. return initialValue.toString();
  551. },
  552. canClearPosition: function(maskset, position, lvp, strict, opts) {
  553. var positionInput = maskset["validPositions"][position].input,
  554. canClear = (positionInput != opts.radixPoint && isFinite(positionInput)) ||
  555. position == lvp ||
  556. positionInput == opts.groupSeparator ||
  557. positionInput == opts.negationSymbol.front ||
  558. positionInput == opts.negationSymbol.back,
  559. posOffset = 0;
  560. if (canClear && isFinite(positionInput)) {
  561. var matchRslt
  562. if (!strict && maskset["buffer"]) {
  563. matchRslt = maskset["buffer"].join('').substr(0, position).match(opts.regex.integerNPart(opts));
  564. var pos = position + 1,
  565. isNull = matchRslt == null || parseInt(matchRslt["0"].replace(new RegExp(inputmask.escapeRegex(opts.groupSeparator), "g"), "")) == 0;
  566. if (isNull) {
  567. while (maskset["validPositions"][pos] && (maskset["validPositions"][pos].input == opts.groupSeparator || maskset["validPositions"][pos].input == "0")) {
  568. delete maskset["validPositions"][pos];
  569. pos++;
  570. }
  571. }
  572. }
  573. var buffer = [];
  574. //build new buffer from validPositions
  575. for (var vp in maskset.validPositions) {
  576. buffer.push(maskset.validPositions[vp].input);
  577. }
  578. matchRslt = buffer.join('').match(opts.regex.integerNPart(opts));
  579. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  580. if (matchRslt && (radixPosition == -1 || position <= radixPosition)) {
  581. if (matchRslt["0"].indexOf("0") == 0) {
  582. canClear = matchRslt.index != position || radixPosition == -1;
  583. } else {
  584. var intPart = parseInt(matchRslt["0"].replace(new RegExp(inputmask.escapeRegex(opts.groupSeparator), "g"), ""));
  585. if (radixPosition != -1 && intPart < 10 /*&& opts.placeholder.charAt(0) == "0"*/ ) {
  586. maskset["validPositions"][position].input = "0";
  587. maskset["p"] = opts.prefix.length + 1;
  588. canClear = false;
  589. }
  590. }
  591. }
  592. }
  593. return canClear;
  594. },
  595. onKeyDown: function(e, buffer, caretPos, opts) {
  596. var $input = $(this);
  597. if (e.ctrlKey) {
  598. switch (e.keyCode) {
  599. case inputmask.keyCode.UP:
  600. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  601. $input.triggerHandler('setvalue.inputmask');
  602. break;
  603. case inputmask.keyCode.DOWN:
  604. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  605. $input.triggerHandler('setvalue.inputmask');
  606. break;
  607. }
  608. }
  609. },
  610. },
  611. 'currency': {
  612. prefix: "$ ",
  613. groupSeparator: ",",
  614. alias: "numeric",
  615. placeholder: "0",
  616. autoGroup: true,
  617. digits: 2,
  618. digitsOptional: false,
  619. clearMaskOnLostFocus: false
  620. },
  621. 'decimal': {
  622. alias: "numeric"
  623. },
  624. 'integer': {
  625. alias: "numeric",
  626. digits: 0,
  627. radixPoint: ""
  628. },
  629. 'percentage': {
  630. alias: "numeric",
  631. digits: 2,
  632. radixPoint: ".",
  633. placeholder: "0",
  634. autoGroup: false,
  635. min: 0,
  636. max: 100,
  637. suffix: " %",
  638. allowPlus: false,
  639. allowMinus: false
  640. },
  641. 'numeric2': {
  642. alias: "numeric"
  643. },
  644. });
  645. return inputmask;
  646. })(jQuery);