inputmask.numeric.extensions.js 25 KB

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