inputmask.numeric.extensions.js 27 KB

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