inputmask.numeric.extensions.js 25 KB

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