inputmask.numeric.extensions.js 26 KB

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