inputmask.numeric.extensions.js 26 KB

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