inputmask.numeric.extensions.js 25 KB

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