inputmask.numeric.extensions.js 25 KB

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