inputmask.numeric.extensions.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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, l;
  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 (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) {
  206. if ((matchRslt[0] === opts.negationSymbol.front + "0" || matchRslt[0] === opts.negationSymbol.front || matchRslt[0] === "+") && (matchRsltDigits === undefined || matchRsltDigits[0].match(/^0+$/))) {
  207. buffer.splice(matchRslt.index, 1);
  208. }
  209. var radixPosition = $.inArray(opts.radixPoint, buffer);
  210. if (radixPosition !== -1) {
  211. if (isFinite(opts.digits) && !opts.digitsOptional) {
  212. for (var i = 1; i <= opts.digits; i++) {
  213. if (buffer[radixPosition + i] === undefined || buffer[radixPosition + i] === opts.placeholder.charAt(0)) buffer[radixPosition + i] = "0";
  214. }
  215. return {
  216. "refreshFromBuffer": true,
  217. "buffer": buffer
  218. };
  219. } else if (radixPosition === buffer.length - opts.suffix.length - 1) {
  220. buffer.splice(radixPosition, 1);
  221. return {
  222. "refreshFromBuffer": true,
  223. "buffer": buffer
  224. };
  225. }
  226. }
  227. }
  228. }
  229. }
  230. if (opts.autoGroup) {
  231. var rslt = opts.postFormat(buffer, caretPos - 1, true, opts);
  232. rslt.caret = caretPos <= opts.prefix.length ? rslt.pos : rslt.pos + 1;
  233. return rslt;
  234. }
  235. },
  236. regex: {
  237. integerPart: function(opts) {
  238. return new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?\\d+");
  239. },
  240. integerNPart: function(opts) {
  241. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + "]+");
  242. }
  243. },
  244. signHandler: function(chrs, maskset, pos, strict, opts) {
  245. if (!strict && (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+")) {
  246. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  247. if (matchRslt && matchRslt[0].length > 0) {
  248. if (maskset.buffer[matchRslt.index] === (chrs === "-" ? "+" : opts.negationSymbol.front)) {
  249. if (chrs === "-") {
  250. if (opts.negationSymbol.back !== "") {
  251. return {
  252. "pos": matchRslt.index,
  253. "c": opts.negationSymbol.front,
  254. "remove": matchRslt.index,
  255. "caret": pos,
  256. "insert": {
  257. "pos": maskset.buffer.length - opts.suffix.length - 1,
  258. "c": opts.negationSymbol.back
  259. }
  260. };
  261. } else {
  262. return {
  263. "pos": matchRslt.index,
  264. "c": opts.negationSymbol.front,
  265. "remove": matchRslt.index,
  266. "caret": pos
  267. };
  268. }
  269. } else {
  270. if (opts.negationSymbol.back !== "") {
  271. return {
  272. "pos": matchRslt.index,
  273. "c": "+",
  274. "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  275. "caret": pos
  276. };
  277. } else {
  278. return {
  279. "pos": matchRslt.index,
  280. "c": "+",
  281. "remove": matchRslt.index,
  282. "caret": pos
  283. };
  284. }
  285. }
  286. } else if (maskset.buffer[matchRslt.index] === (chrs === "-" ? opts.negationSymbol.front : "+")) {
  287. if (chrs === "-" && opts.negationSymbol.back !== "") {
  288. return {
  289. "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  290. "caret": pos - 1
  291. };
  292. } else {
  293. return {
  294. "remove": matchRslt.index,
  295. "caret": pos - 1
  296. };
  297. }
  298. } else {
  299. if (chrs === "-") {
  300. if (opts.negationSymbol.back !== "") {
  301. return {
  302. "pos": matchRslt.index,
  303. "c": opts.negationSymbol.front,
  304. "caret": pos + 1,
  305. "insert": {
  306. "pos": maskset.buffer.length - opts.suffix.length,
  307. "c": opts.negationSymbol.back
  308. }
  309. };
  310. } else {
  311. return {
  312. "pos": matchRslt.index,
  313. "c": opts.negationSymbol.front,
  314. "caret": pos + 1
  315. };
  316. }
  317. } else {
  318. return {
  319. "pos": matchRslt.index,
  320. "c": chrs,
  321. "caret": pos + 1
  322. };
  323. }
  324. }
  325. }
  326. }
  327. return false;
  328. },
  329. radixHandler: function(chrs, maskset, pos, strict, opts) {
  330. if (!strict) {
  331. if ($.inArray(chrs, [",", "."]) !== -1) chrs = opts.radixPoint;
  332. if (chrs === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  333. var radixPos = $.inArray(opts.radixPoint, maskset.buffer),
  334. integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  335. if (radixPos !== -1 && maskset.validPositions[radixPos]) {
  336. if (maskset.validPositions[radixPos - 1]) {
  337. return {
  338. "caret": radixPos + 1
  339. };
  340. } else {
  341. return {
  342. "pos": integerValue.index,
  343. c: integerValue[0],
  344. "caret": radixPos + 1
  345. };
  346. }
  347. } else if (!integerValue || (integerValue["0"] === "0" && (integerValue.index + 1) !== pos)) {
  348. maskset.buffer[integerValue ? integerValue.index : pos] = "0";
  349. return {
  350. "pos": (integerValue ? integerValue.index : pos) + 1,
  351. c: opts.radixPoint
  352. };
  353. }
  354. }
  355. }
  356. return false;
  357. },
  358. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  359. if (opts.numericInput === true) {
  360. if (maskset.buffer[maskset.buffer.length - opts.prefix.length - 1] === "0") {
  361. return {
  362. "pos": pos,
  363. "remove": maskset.buffer.length - opts.prefix.length - 1
  364. };
  365. }
  366. } else {
  367. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)),
  368. radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  369. if (matchRslt && !strict && (radixPosition === -1 || pos <= radixPosition)) {
  370. if (matchRslt["0"].indexOf("0") === 0) {
  371. if (pos < opts.prefix.length) pos = matchRslt.index; //position
  372. var _radixPosition = $.inArray(opts.radixPoint, maskset._buffer);
  373. var digitsMatch = maskset._buffer && maskset.buffer.slice(radixPosition).join("") === maskset._buffer.slice(_radixPosition).join("") || parseInt(maskset.buffer.slice(radixPosition + 1).join("")) === 0;
  374. 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";
  375. if (radixPosition === -1 || digitsMatch && integerMatch) {
  376. maskset.buffer.splice(matchRslt.index, 1);
  377. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  378. return {
  379. "pos": pos,
  380. "remove": matchRslt.index
  381. };
  382. } else if (matchRslt.index + 1 === pos || chrs === "0") {
  383. maskset.buffer.splice(matchRslt.index, 1);
  384. pos = matchRslt.index;
  385. return {
  386. "pos": pos,
  387. "remove": matchRslt.index
  388. };
  389. }
  390. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  391. return false;
  392. }
  393. }
  394. }
  395. return true;
  396. },
  397. postValidation: function(buffer, opts) {
  398. //handle maxvalue
  399. var isValid = true,
  400. maskedValue = buffer.join(""),
  401. processValue = maskedValue.replace(opts.prefix, "");
  402. processValue = processValue.replace(opts.suffix, "");
  403. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  404. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  405. //handle negation symbol
  406. processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  407. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  408. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  409. if (isFinite(processValue)) {
  410. if (isFinite(opts.max)) {
  411. isValid = parseFloat(processValue) <= parseFloat(opts.max);
  412. }
  413. if (isValid && isFinite(opts.min) && (processValue <= 0 || processValue.toString().length >= opts.min.toString().length)) {
  414. isValid = parseFloat(processValue) >= parseFloat(opts.min);
  415. if (!isValid) {
  416. isValid = $.extend(true, {
  417. "refreshFromBuffer": true,
  418. "buffer": (opts.prefix + opts.min).split("")
  419. }, opts.postFormat((opts.prefix + opts.min).split(""), 0, true, opts));
  420. isValid.refreshFromBuffer = true; //enforce refresh
  421. }
  422. }
  423. }
  424. return isValid;
  425. },
  426. definitions: {
  427. "~": {
  428. validator: function(chrs, maskset, pos, strict, opts) {
  429. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  430. if (!isValid) {
  431. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  432. if (!isValid) {
  433. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  434. if (isValid === true) {
  435. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  436. if (isValid === true) {
  437. //handle overwrite when fixed precision
  438. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  439. if (radixPosition !== -1 && opts.digitsOptional === false && pos > radixPosition && !strict) {
  440. isValid = {
  441. "pos": pos,
  442. "remove": pos
  443. };
  444. } else {
  445. isValid = {
  446. pos: pos
  447. };
  448. }
  449. }
  450. }
  451. }
  452. }
  453. return isValid;
  454. },
  455. cardinality: 1,
  456. prevalidator: null
  457. },
  458. "+": {
  459. validator: function(chrs, maskset, pos, strict, opts) {
  460. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  461. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  462. if (chrs === "-") {
  463. if (opts.negationSymbol.back !== "") {
  464. isValid = {
  465. "pos": pos,
  466. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  467. "caret": pos + 1,
  468. "insert": {
  469. "pos": maskset.buffer.length,
  470. "c": opts.negationSymbol.back
  471. }
  472. };
  473. } else {
  474. isValid = {
  475. "pos": pos,
  476. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  477. "caret": pos + 1
  478. };
  479. }
  480. } else {
  481. isValid = true;
  482. }
  483. }
  484. return isValid;
  485. },
  486. cardinality: 1,
  487. prevalidator: null,
  488. placeholder: ""
  489. },
  490. "-": {
  491. validator: function(chrs, maskset, pos, strict, opts) {
  492. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  493. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  494. isValid = true;
  495. }
  496. return isValid;
  497. },
  498. cardinality: 1,
  499. prevalidator: null,
  500. placeholder: ""
  501. },
  502. ":": {
  503. validator: function(chrs, maskset, pos, strict, opts) {
  504. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  505. if (!isValid) {
  506. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + ",\\." + "]";
  507. isValid = new RegExp(radix).test(chrs);
  508. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  509. isValid = {
  510. "caret": pos + 1
  511. };
  512. }
  513. }
  514. return isValid ? {
  515. c: opts.radixPoint
  516. } : isValid;
  517. },
  518. cardinality: 1,
  519. prevalidator: null,
  520. placeholder: function(opts) {
  521. return opts.radixPoint;
  522. }
  523. }
  524. },
  525. onUnMask: function(maskedValue, unmaskedValue, opts) {
  526. var processValue = maskedValue.replace(opts.prefix, "");
  527. processValue = processValue.replace(opts.suffix, "");
  528. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  529. if (opts.unmaskAsNumber) {
  530. processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  531. return Number(processValue);
  532. }
  533. return processValue;
  534. },
  535. isComplete: function(buffer, opts) {
  536. var maskedValue = buffer.join(""),
  537. bufClone = buffer.slice();
  538. //verify separator positions
  539. opts.postFormat(bufClone, 0, true, opts);
  540. if (bufClone.join("") !== maskedValue) return false;
  541. var processValue = maskedValue.replace(opts.prefix, "");
  542. processValue = processValue.replace(opts.suffix, "");
  543. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  544. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  545. return isFinite(processValue);
  546. },
  547. onBeforeMask: function(initialValue, opts) {
  548. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  549. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  550. } else {
  551. var kommaMatches = initialValue.match(/,/g);
  552. var dotMatches = initialValue.match(/\./g);
  553. if (dotMatches && kommaMatches) {
  554. if (dotMatches.length > kommaMatches.length) {
  555. initialValue = initialValue.replace(/\./g, "");
  556. initialValue = initialValue.replace(",", opts.radixPoint);
  557. } else if (kommaMatches.length > dotMatches.length) {
  558. initialValue = initialValue.replace(/,/g, "");
  559. initialValue = initialValue.replace(".", opts.radixPoint);
  560. } else { //equal
  561. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  562. }
  563. } else {
  564. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  565. }
  566. }
  567. if (opts.digits === 0) {
  568. if (initialValue.indexOf(".") !== -1) {
  569. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  570. } else if (initialValue.indexOf(",") !== -1) {
  571. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  572. }
  573. }
  574. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  575. var valueParts = initialValue.split(opts.radixPoint),
  576. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  577. if (parseInt(opts.digits) < decPart.toString().length) {
  578. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  579. //make the initialValue a valid javascript number for the parsefloat
  580. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  581. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  582. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  583. }
  584. }
  585. return initialValue.toString();
  586. },
  587. canClearPosition: function(maskset, position, lvp, strict, opts) {
  588. var positionInput = maskset.validPositions[position].input,
  589. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  590. position === lvp ||
  591. positionInput === opts.groupSeparator ||
  592. positionInput === opts.negationSymbol.front ||
  593. positionInput === opts.negationSymbol.back;
  594. if (canClear && isFinite(positionInput)) {
  595. var matchRslt,
  596. radixPos = $.inArray(opts.radixPoint, maskset.buffer);
  597. //inject radixpoint
  598. var radixInjection = false;
  599. if (maskset.validPositions[radixPos] === undefined) {
  600. maskset.validPositions[radixPos] = {
  601. input: opts.radixPoint
  602. };
  603. radixInjection = true;
  604. }
  605. if (!strict && maskset.buffer) {
  606. matchRslt = maskset.buffer.join("").substr(0, position).match(opts.regex.integerNPart(opts));
  607. var pos = position + 1,
  608. isNull = matchRslt == null || parseInt(matchRslt["0"].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "")) === 0;
  609. if (isNull) {
  610. while (maskset.validPositions[pos] && (maskset.validPositions[pos].input === opts.groupSeparator || maskset.validPositions[pos].input === "0")) {
  611. delete maskset.validPositions[pos];
  612. pos++;
  613. }
  614. }
  615. }
  616. var buffer = [];
  617. //build new buffer from validPositions
  618. for (var vp in maskset.validPositions) {
  619. if (maskset.validPositions[vp].input !== undefined) buffer.push(maskset.validPositions[vp].input);
  620. }
  621. //remove radix Injection
  622. if (radixInjection) {
  623. delete maskset.validPositions[radixPos];
  624. }
  625. if (radixPos > 0) {
  626. matchRslt = buffer.join("").match(opts.regex.integerNPart(opts));
  627. if (matchRslt && position <= radixPos) {
  628. if (matchRslt["0"].indexOf("0") === 0) {
  629. canClear = matchRslt.index !== position;
  630. } else {
  631. var intPart = parseInt(matchRslt["0"].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), ""));
  632. if (intPart < 10 && maskset.validPositions[position]) {
  633. maskset.validPositions[position].input = "0";
  634. maskset.p = opts.prefix.length + 1;
  635. canClear = false;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. return canClear;
  642. },
  643. onKeyDown: function(e, buffer, caretPos, opts) {
  644. var $input = $(this);
  645. if (e.ctrlKey) {
  646. switch (e.keyCode) {
  647. case Inputmask.keyCode.UP:
  648. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  649. $input.triggerHandler("setvalue.inputmask");
  650. break;
  651. case Inputmask.keyCode.DOWN:
  652. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  653. $input.triggerHandler("setvalue.inputmask");
  654. break;
  655. }
  656. }
  657. }
  658. },
  659. "currency": {
  660. prefix: "$ ",
  661. groupSeparator: ",",
  662. alias: "numeric",
  663. placeholder: "0",
  664. autoGroup: true,
  665. digits: 2,
  666. digitsOptional: false,
  667. clearMaskOnLostFocus: false
  668. },
  669. "decimal": {
  670. alias: "numeric"
  671. },
  672. "integer": {
  673. alias: "numeric",
  674. digits: 0,
  675. radixPoint: ""
  676. },
  677. "percentage": {
  678. alias: "numeric",
  679. digits: 2,
  680. radixPoint: ".",
  681. placeholder: "0",
  682. autoGroup: false,
  683. min: 0,
  684. max: 100,
  685. suffix: " %",
  686. allowPlus: false,
  687. allowMinus: false
  688. }
  689. });
  690. return Inputmask;
  691. })(jQuery);