inputmask.numeric.extensions.js 25 KB

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