inputmask.numeric.extensions.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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(["inputmask.dependencyLib", "inputmask"], factory);
  12. } else if (typeof exports === "object") {
  13. module.exports = factory(require("./inputmask.dependencyLib.jquery"), require("./inputmask"));
  14. } else {
  15. factory(window.dependencyLib || jQuery, window.Inputmask);
  16. }
  17. }
  18. (function($, Inputmask) {
  19. //number aliases
  20. Inputmask.extendAliases({
  21. "numeric2": {
  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. var seps = "+",
  46. mod = 0;
  47. if (opts.autoGroup) {
  48. if (typeof opts.groupSize == "string" && isFinite(opts.groupSize)) opts.groupSize = parseInt(opts.groupSize);
  49. if (isFinite(opts.integerDigits)) {
  50. seps = Math.floor(opts.integerDigits / opts.groupSize);
  51. mod = opts.integerDigits % opts.groupSize;
  52. }
  53. }
  54. //enforce placeholder to single
  55. if (opts.placeholder.length > 1) {
  56. opts.placeholder = opts.placeholder.charAt(0);
  57. }
  58. if (opts.jitMasking === true && opts.placeholder.length > 0) {
  59. opts.jitMasking = 1;
  60. }
  61. //only allow radixfocus when placeholder = 0
  62. opts.radixFocus = opts.radixFocus && opts.placeholder !== "" && opts.integerOptional === true;
  63. opts.definitions[";"] = opts.definitions["~"]; //clone integer def for decimals
  64. opts.definitions[";"].definitionSymbol = "~";
  65. var mask = autoEscape(opts.prefix);
  66. mask += "[+]";
  67. if (opts.autoGroup) {
  68. mask += "(" + opts.groupSeparator + "~{" + opts.groupSize + "}){" + seps + "}";
  69. if (mod > 0) mask += "~{" + mod + "}";
  70. } else {
  71. mask += "~{" + opts.integerDigits + "}";
  72. }
  73. if (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  74. if (opts.digitsOptional) {
  75. mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{1," + opts.digits + "}]";
  76. } else {
  77. mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}";
  78. if (isFinite(opts.jitMasking)) {
  79. if (opts.jitMasking < (1 + opts.radixPoint.length + (isFinite(opts.digits) ? opts.digits : 2))) {
  80. opts.jitMasking = (1 + opts.radixPoint.length + (isFinite(opts.digits) ? opts.digits : 2));
  81. }
  82. }
  83. }
  84. }
  85. if (opts.negationSymbol.back !== "") {
  86. mask += "[-]";
  87. }
  88. mask += autoEscape(opts.suffix);
  89. opts.greedy = false; //enforce greedy false
  90. return mask;
  91. },
  92. placeholder: "",
  93. greedy: false,
  94. digits: "*", //number of fractionalDigits
  95. digitsOptional: true,
  96. radixPoint: ".",
  97. radixFocus: true,
  98. groupSize: 3,
  99. groupSeparator: "",
  100. autoGroup: false,
  101. allowPlus: true,
  102. allowMinus: true,
  103. negationSymbol: {
  104. front: "-", //"("
  105. back: "" //")"
  106. },
  107. integerDigits: "+", //number of integerDigits
  108. integerOptional: true,
  109. prefix: "",
  110. suffix: "",
  111. rightAlign: true,
  112. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  113. min: null, //minimum value
  114. max: null, //maximum value
  115. step: 1,
  116. insertMode: true,
  117. autoUnmask: false,
  118. unmaskAsNumber: false,
  119. jitMasking: true,
  120. numericInput: true,
  121. onBeforeWrite: function(e, buffer, caretPos, opts) {
  122. if (e && (e.type === "blur" || e.type === "checkval")) {
  123. //handle minvalue
  124. var maskedValue = buffer.join(""),
  125. processValue = maskedValue.replace(opts.prefix, "");
  126. processValue = processValue.replace(opts.suffix, "");
  127. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  128. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  129. if (isFinite(processValue)) {
  130. if (isFinite(opts.min) && parseFloat(processValue) < parseFloat(opts.min)) {
  131. return {
  132. "refreshFromBuffer": true,
  133. "buffer": (opts.prefix + opts.min).split("")
  134. };
  135. }
  136. }
  137. var tmpBufSplit = opts.radixPoint !== "" ? buffer.join("").split(opts.radixPoint) : [buffer.join("")],
  138. matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)),
  139. matchRsltDigits = tmpBufSplit.length === 2 ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : undefined;
  140. if (matchRslt) {
  141. if ((matchRslt[0] === opts.negationSymbol.front + "0" || matchRslt[0] === opts.negationSymbol.front || matchRslt[0] === "+") && (matchRsltDigits === undefined || matchRsltDigits[0].match(/^0+$/))) {
  142. buffer.splice(matchRslt.index, 1);
  143. }
  144. var radixPosition = $.inArray(opts.radixPoint, buffer);
  145. if (radixPosition !== -1) {
  146. if (isFinite(opts.digits) && !opts.digitsOptional) {
  147. for (var i = 1; i <= opts.digits; i++) {
  148. if (buffer[radixPosition + i] === undefined || buffer[radixPosition + i] === opts.placeholder.charAt(0)) {
  149. buffer[radixPosition + i] = "0";
  150. }
  151. }
  152. return {
  153. "refreshFromBuffer": maskedValue !== buffer.join(""),
  154. "buffer": buffer
  155. };
  156. } else if (radixPosition === buffer.length - opts.suffix.length - 1) {
  157. buffer.splice(radixPosition, 1);
  158. return {
  159. "refreshFromBuffer": true,
  160. "buffer": buffer
  161. };
  162. }
  163. }
  164. }
  165. }
  166. },
  167. regex: {
  168. integerPart: function(opts) {
  169. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + "]+[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?$");
  170. },
  171. integerNPart: function(opts) {
  172. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + "]+");
  173. }
  174. },
  175. signHandler: function(chrs, maskset, pos, strict, opts) {
  176. // if (!strict && (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+")) {
  177. // var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  178. //
  179. // if (matchRslt && matchRslt[0].length > 0) {
  180. // if (maskset.buffer[matchRslt.index] === (chrs === "-" ? "+" : opts.negationSymbol.front)) {
  181. // if (chrs === "-") {
  182. // if (opts.negationSymbol.back !== "") {
  183. // return {
  184. // "pos": matchRslt.index,
  185. // "c": opts.negationSymbol.front,
  186. // "remove": matchRslt.index,
  187. // "caret": pos,
  188. // "insert": {
  189. // "pos": maskset.buffer.length - opts.suffix.length - 1,
  190. // "c": opts.negationSymbol.back
  191. // }
  192. // };
  193. // } else {
  194. // return {
  195. // "pos": matchRslt.index,
  196. // "c": opts.negationSymbol.front,
  197. // "remove": matchRslt.index,
  198. // "caret": pos
  199. // };
  200. // }
  201. // } else {
  202. // if (opts.negationSymbol.back !== "") {
  203. // return {
  204. // "pos": matchRslt.index,
  205. // "c": "+",
  206. // "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  207. // "caret": pos
  208. // };
  209. // } else {
  210. // return {
  211. // "pos": matchRslt.index,
  212. // "c": "+",
  213. // "remove": matchRslt.index,
  214. // "caret": pos
  215. // };
  216. // }
  217. // }
  218. // } else if (maskset.buffer[matchRslt.index] === (chrs === "-" ? opts.negationSymbol.front : "+")) {
  219. // if (chrs === "-" && opts.negationSymbol.back !== "") {
  220. // return {
  221. // "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  222. // "caret": pos - 1
  223. // };
  224. // } else {
  225. // return {
  226. // "remove": matchRslt.index,
  227. // "caret": pos - 1
  228. // };
  229. // }
  230. // } else {
  231. // if (chrs === "-") {
  232. // if (opts.negationSymbol.back !== "") {
  233. // return {
  234. // "pos": matchRslt.index,
  235. // "c": opts.negationSymbol.front,
  236. // "caret": pos + 1,
  237. // "insert": {
  238. // "pos": maskset.buffer.length - opts.suffix.length,
  239. // "c": opts.negationSymbol.back
  240. // }
  241. // };
  242. // } else {
  243. // return {
  244. // "pos": matchRslt.index,
  245. // "c": opts.negationSymbol.front,
  246. // "caret": pos + 1
  247. // };
  248. // }
  249. // } else {
  250. // return {
  251. // "pos": matchRslt.index,
  252. // "c": chrs,
  253. // "caret": pos + 1
  254. // };
  255. // }
  256. // }
  257. // }
  258. // }
  259. return false;
  260. },
  261. radixHandler: function(chrs, maskset, pos, strict, opts) {
  262. // if (!strict) {
  263. // if ($.inArray(chrs, [",", "."]) !== -1) chrs = opts.radixPoint;
  264. // if (chrs === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  265. // var radixPos = $.inArray(opts.radixPoint, maskset.buffer),
  266. // integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  267. //
  268. // }
  269. // }
  270. return false;
  271. },
  272. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  273. // if (maskset.buffer[maskset.buffer.length - opts.prefix.length - 1] === "0") {
  274. // return {
  275. // "pos": pos,
  276. // "remove": maskset.buffer.length - opts.prefix.length - 1
  277. // };
  278. // }
  279. return true;
  280. },
  281. postValidation: function(buffer, opts) {
  282. //handle maxvalue
  283. var isValid = true,
  284. maskedValue = buffer.join(""),
  285. processValue = maskedValue.replace(opts.prefix, "");
  286. processValue = processValue.replace(opts.suffix, "");
  287. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  288. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  289. //handle negation symbol
  290. processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  291. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  292. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  293. if (isFinite(processValue)) {
  294. if (opts.max !== null && isFinite(opts.max)) {
  295. isValid = parseFloat(processValue) <= parseFloat(opts.max);
  296. }
  297. if (isValid && opts.min !== null && isFinite(opts.min) && (processValue <= 0 || processValue.toString().length >= opts.min.toString().length)) {
  298. isValid = parseFloat(processValue) >= parseFloat(opts.min);
  299. if (!isValid) {
  300. isValid = {
  301. "refreshFromBuffer": true,
  302. "buffer": (opts.prefix + opts.min).split("")
  303. };
  304. }
  305. }
  306. }
  307. return isValid;
  308. },
  309. definitions: {
  310. "~": {
  311. validator: function(chrs, maskset, pos, strict, opts) {
  312. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  313. if (!isValid) {
  314. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  315. if (!isValid) {
  316. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  317. if (isValid === true) {
  318. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  319. if (isValid === true) {
  320. //handle overwrite when fixed precision
  321. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  322. if (radixPosition !== -1 && opts.digitsOptional === false && pos > radixPosition + 1 && !strict) {
  323. isValid = {
  324. "pos": pos - 1,
  325. "remove": pos - 1
  326. };
  327. }
  328. }
  329. }
  330. }
  331. }
  332. return isValid;
  333. },
  334. cardinality: 1,
  335. prevalidator: null
  336. },
  337. "+": {
  338. validator: function(chrs, maskset, pos, strict, opts) {
  339. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  340. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  341. if (chrs === "-") {
  342. if (opts.negationSymbol.back !== "") {
  343. isValid = {
  344. "pos": pos,
  345. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  346. "caret": pos + 1,
  347. "insert": {
  348. "pos": maskset.buffer.length,
  349. "c": opts.negationSymbol.back
  350. }
  351. };
  352. } else {
  353. isValid = {
  354. "pos": pos,
  355. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  356. "caret": pos + 1
  357. };
  358. }
  359. } else {
  360. isValid = true;
  361. }
  362. }
  363. return isValid;
  364. },
  365. cardinality: 1,
  366. prevalidator: null,
  367. placeholder: ""
  368. },
  369. "-": {
  370. validator: function(chrs, maskset, pos, strict, opts) {
  371. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  372. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  373. isValid = true;
  374. }
  375. return isValid;
  376. },
  377. cardinality: 1,
  378. prevalidator: null,
  379. placeholder: ""
  380. },
  381. ":": {
  382. validator: function(chrs, maskset, pos, strict, opts) {
  383. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  384. if (!isValid) {
  385. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + ",\\." + "]";
  386. isValid = new RegExp(radix).test(chrs);
  387. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  388. isValid = {
  389. "caret": pos + 1
  390. };
  391. }
  392. }
  393. return isValid ? {
  394. c: opts.radixPoint
  395. } : isValid;
  396. },
  397. cardinality: 1,
  398. prevalidator: null,
  399. placeholder: function(opts) {
  400. return opts.radixPoint;
  401. }
  402. }
  403. },
  404. onUnMask: function(maskedValue, unmaskedValue, opts) {
  405. var processValue = maskedValue.replace(opts.prefix, "");
  406. processValue = processValue.replace(opts.suffix, "");
  407. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  408. if (opts.unmaskAsNumber) {
  409. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  410. return Number(processValue);
  411. }
  412. return processValue;
  413. },
  414. isComplete: function(buffer, opts) {
  415. var maskedValue = buffer.join("");
  416. var processValue = maskedValue.replace(opts.prefix, "");
  417. processValue = processValue.replace(opts.suffix, "");
  418. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  419. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  420. return isFinite(processValue);
  421. },
  422. onBeforeMask: function(initialValue, opts) {
  423. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  424. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  425. } else {
  426. var kommaMatches = initialValue.match(/,/g);
  427. var dotMatches = initialValue.match(/\./g);
  428. if (dotMatches && kommaMatches) {
  429. if (dotMatches.length > kommaMatches.length) {
  430. initialValue = initialValue.replace(/\./g, "");
  431. initialValue = initialValue.replace(",", opts.radixPoint);
  432. } else if (kommaMatches.length > dotMatches.length) {
  433. initialValue = initialValue.replace(/,/g, "");
  434. initialValue = initialValue.replace(".", opts.radixPoint);
  435. } else { //equal
  436. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  437. }
  438. } else {
  439. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  440. }
  441. }
  442. if (opts.digits === 0) {
  443. if (initialValue.indexOf(".") !== -1) {
  444. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  445. } else if (initialValue.indexOf(",") !== -1) {
  446. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  447. }
  448. }
  449. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  450. var valueParts = initialValue.split(opts.radixPoint),
  451. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  452. if (parseInt(opts.digits) < decPart.toString().length) {
  453. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  454. //make the initialValue a valid javascript number for the parsefloat
  455. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  456. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  457. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  458. }
  459. }
  460. return initialValue.toString();
  461. },
  462. canClearPosition: function(maskset, position, lvp, strict, opts) {
  463. var positionInput = maskset.validPositions[position].input,
  464. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  465. position === lvp ||
  466. positionInput === opts.groupSeparator ||
  467. positionInput === opts.negationSymbol.front ||
  468. positionInput === opts.negationSymbol.back;
  469. if (canClear && isFinite(positionInput)) {
  470. var matchRslt,
  471. radixPos = $.inArray(opts.radixPoint, maskset.buffer);
  472. //inject radixpoint
  473. var radixInjection = false;
  474. if (maskset.validPositions[radixPos] === undefined) {
  475. maskset.validPositions[radixPos] = {
  476. input: opts.radixPoint
  477. };
  478. radixInjection = true;
  479. }
  480. if (!strict && maskset.buffer) {
  481. matchRslt = maskset.buffer.join("").substr(0, position).match(opts.regex.integerNPart(opts));
  482. var pos = position + 1,
  483. isNull = matchRslt == null || parseInt(matchRslt["0"].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "")) === 0;
  484. if (isNull) {
  485. while (maskset.validPositions[pos] && (maskset.validPositions[pos].input === opts.groupSeparator || maskset.validPositions[pos].input === "0")) {
  486. delete maskset.validPositions[pos];
  487. pos++;
  488. }
  489. }
  490. }
  491. var buffer = [];
  492. //build new buffer from validPositions
  493. for (var vp in maskset.validPositions) {
  494. if (maskset.validPositions[vp].input !== undefined) buffer.push(maskset.validPositions[vp].input);
  495. }
  496. //remove radix Injection
  497. if (radixInjection) {
  498. delete maskset.validPositions[radixPos];
  499. }
  500. if (radixPos > 0) {
  501. var bufVal = buffer.join("");
  502. matchRslt = bufVal.match(opts.regex.integerNPart(opts));
  503. if (matchRslt) {
  504. if (position <= radixPos) {
  505. if (matchRslt["0"].indexOf("0") === 0) {
  506. canClear = matchRslt.index !== position || opts.placeholder === "0";
  507. } else {
  508. var intPart = parseInt(matchRslt["0"].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "")),
  509. radixPart = parseInt(bufVal.split(opts.radixPoint)[1]);
  510. if (intPart < 10 && maskset.validPositions[position] && (opts.placeholder !== "0" || radixPart > 0)) {
  511. maskset.validPositions[position].input = "0";
  512. maskset.p = opts.prefix.length + 1;
  513. canClear = false;
  514. }
  515. }
  516. } else if (matchRslt["0"].indexOf("0") === 0) {
  517. if (bufVal.length === 3) {
  518. maskset.validPositions = {};
  519. canClear = false;
  520. }
  521. }
  522. }
  523. }
  524. }
  525. return canClear;
  526. },
  527. onKeyDown: function(e, buffer, caretPos, opts) {
  528. var $input = $(this);
  529. if (e.ctrlKey) {
  530. switch (e.keyCode) {
  531. case Inputmask.keyCode.UP:
  532. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  533. $input.trigger("setvalue");
  534. break;
  535. case Inputmask.keyCode.DOWN:
  536. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  537. $input.trigger("setvalue");
  538. break;
  539. }
  540. }
  541. }
  542. },
  543. "numeric": {
  544. mask: function(opts) {
  545. function autoEscape(txt) {
  546. var escapedTxt = "";
  547. for (var i = 0; i < txt.length; i++) {
  548. escapedTxt += opts.definitions[txt.charAt(i)] ? "\\" + txt.charAt(i) : txt.charAt(i);
  549. }
  550. return escapedTxt;
  551. }
  552. if (opts.repeat !== 0 && isNaN(opts.integerDigits)) {
  553. opts.integerDigits = opts.repeat;
  554. }
  555. opts.repeat = 0;
  556. if (opts.groupSeparator === opts.radixPoint) { //treat equal separator and radixpoint
  557. if (opts.radixPoint === ".") {
  558. opts.groupSeparator = ",";
  559. } else if (opts.radixPoint === ",") {
  560. opts.groupSeparator = ".";
  561. } else opts.groupSeparator = "";
  562. }
  563. if (opts.groupSeparator === " ") { //prevent conflict with default skipOptionalPartCharacter
  564. opts.skipOptionalPartCharacter = undefined;
  565. }
  566. opts.autoGroup = opts.autoGroup && opts.groupSeparator !== "";
  567. if (opts.autoGroup) {
  568. if (typeof opts.groupSize == "string" && isFinite(opts.groupSize)) opts.groupSize = parseInt(opts.groupSize);
  569. if (isFinite(opts.integerDigits)) {
  570. var seps = Math.floor(opts.integerDigits / opts.groupSize);
  571. var mod = opts.integerDigits % opts.groupSize;
  572. opts.integerDigits = parseInt(opts.integerDigits) + (mod === 0 ? seps - 1 : seps);
  573. if (opts.integerDigits < 1) {
  574. opts.integerDigits = "*";
  575. }
  576. }
  577. }
  578. //enforce placeholder to single
  579. if (opts.placeholder.length > 1) {
  580. opts.placeholder = opts.placeholder.charAt(0);
  581. }
  582. //only allow radixfocus when placeholder = 0
  583. opts.radixFocus = opts.radixFocus && opts.placeholder !== "" && opts.integerOptional === true;
  584. opts.definitions[";"] = opts.definitions["~"]; //clone integer def for decimals
  585. opts.definitions[";"].definitionSymbol = "~";
  586. var mask = autoEscape(opts.prefix);
  587. mask += "[+]";
  588. if (opts.integerOptional === true) {
  589. mask += "~{1," + opts.integerDigits + "}";
  590. } else mask += "~{" + opts.integerDigits + "}";
  591. if (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) {
  592. if (opts.digitsOptional) {
  593. mask += "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{1," + opts.digits + "}]";
  594. } else mask += (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}";
  595. }
  596. if (opts.negationSymbol.back !== "") {
  597. mask += "[-]";
  598. }
  599. mask += autoEscape(opts.suffix);
  600. opts.greedy = false; //enforce greedy false
  601. return mask;
  602. },
  603. placeholder: "",
  604. greedy: false,
  605. digits: "*", //number of fractionalDigits
  606. digitsOptional: true,
  607. radixPoint: ".",
  608. radixFocus: true,
  609. groupSize: 3,
  610. groupSeparator: "",
  611. autoGroup: false,
  612. allowPlus: true,
  613. allowMinus: true,
  614. negationSymbol: {
  615. front: "-", //"("
  616. back: "" //")"
  617. },
  618. integerDigits: "+", //number of integerDigits
  619. integerOptional: true,
  620. prefix: "",
  621. suffix: "",
  622. rightAlign: true,
  623. decimalProtect: true, //do not allow assumption of decimals input without entering the radixpoint
  624. min: null, //minimum value
  625. max: null, //maximum value
  626. step: 1,
  627. insertMode: true,
  628. autoUnmask: false,
  629. unmaskAsNumber: false,
  630. postFormat: function(buffer, pos, reformatOnly, opts) { //this needs to be removed // this is crap
  631. // console.log(buffer);
  632. if (opts.numericInput === true) {
  633. buffer = buffer.reverse();
  634. if (isFinite(pos)) {
  635. pos = buffer.join("").length - pos - 1;
  636. }
  637. }
  638. var suffixStripped = false,
  639. i, l;
  640. if (buffer.length >= opts.suffix.length && buffer.join("").indexOf(opts.suffix) === (buffer.length - opts.suffix.length)) {
  641. buffer.length = buffer.length - opts.suffix.length; //strip suffix
  642. suffixStripped = true;
  643. }
  644. //position overflow corrections
  645. pos = pos >= buffer.length ? buffer.length - 1 : (pos < opts.prefix.length ? opts.prefix.length : pos);
  646. var needsRefresh = false,
  647. charAtPos = buffer[pos];
  648. if (opts.groupSeparator === "" || (opts.numericInput !== true &&
  649. ($.inArray(opts.radixPoint, buffer) !== -1 && pos > $.inArray(opts.radixPoint, buffer)) ||
  650. new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]").test(charAtPos))) {
  651. if (suffixStripped) {
  652. for (i = 0, l = opts.suffix.length; i < l; i++) {
  653. buffer.push(opts.suffix.charAt(i));
  654. }
  655. }
  656. //console.log("return input " + buffer);
  657. return {
  658. pos: pos
  659. };
  660. }
  661. var cbuf = buffer.slice();
  662. if (charAtPos === opts.groupSeparator) {
  663. cbuf.splice(pos--, 1);
  664. charAtPos = cbuf[pos];
  665. }
  666. if (reformatOnly) {
  667. if (charAtPos !== opts.radixPoint) cbuf[pos] = "?";
  668. } else cbuf.splice(pos, 0, "?"); //set position indicator
  669. var bufVal = cbuf.join(""),
  670. bufValOrigin = bufVal;
  671. if (bufVal.length > 0 && opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) !== -1)) {
  672. var escapedGroupSeparator = Inputmask.escapeRegex(opts.groupSeparator);
  673. needsRefresh = bufVal.indexOf(opts.groupSeparator) === 0;
  674. bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  675. var radixSplit = bufVal.split(opts.radixPoint);
  676. bufVal = opts.radixPoint === "" ? bufVal : radixSplit[0];
  677. if (bufVal !== (opts.prefix + "?0") && bufVal.length >= (opts.groupSize + opts.prefix.length)) {
  678. //needsRefresh = true;
  679. var reg = new RegExp("([-\+]?[\\d\?]+)([\\d\?]{" + opts.groupSize + "})");
  680. while (reg.test(bufVal)) {
  681. bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2");
  682. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  683. }
  684. }
  685. if (opts.radixPoint !== "" && radixSplit.length > 1) {
  686. bufVal += opts.radixPoint + radixSplit[1];
  687. }
  688. }
  689. needsRefresh = bufValOrigin !== bufVal;
  690. buffer.length = bufVal.length; //align the length
  691. for (i = 0, l = bufVal.length; i < l; i++) {
  692. buffer[i] = bufVal.charAt(i);
  693. }
  694. var newPos = $.inArray("?", buffer);
  695. if (newPos === -1 && charAtPos === opts.radixPoint) newPos = $.inArray(opts.radixPoint, buffer);
  696. if (reformatOnly) buffer[newPos] = charAtPos;
  697. else buffer.splice(newPos, 1);
  698. if (!needsRefresh && suffixStripped) {
  699. for (i = 0, l = opts.suffix.length; i < l; i++) {
  700. buffer.push(opts.suffix.charAt(i));
  701. }
  702. }
  703. // console.log("formatted " + buffer + " refresh " + needsRefresh);
  704. newPos = (opts.numericInput && isFinite(pos)) ? buffer.join("").length - newPos - 1 : newPos;
  705. if (opts.numericInput) {
  706. buffer = buffer.reverse();
  707. if ($.inArray(opts.radixPoint, buffer) < newPos && (buffer.join("").length - opts.suffix.length) !== newPos) {
  708. newPos = newPos - 1;
  709. }
  710. }
  711. return {
  712. pos: newPos,
  713. "refreshFromBuffer": needsRefresh,
  714. "buffer": buffer
  715. };
  716. },
  717. onBeforeWrite: function(e, buffer, caretPos, opts) {
  718. if (e && (e.type === "blur" || e.type === "checkval")) {
  719. //handle minvalue
  720. var maskedValue = buffer.join(""),
  721. processValue = maskedValue.replace(opts.prefix, "");
  722. processValue = processValue.replace(opts.suffix, "");
  723. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  724. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  725. if (isFinite(processValue)) {
  726. if (isFinite(opts.min) && parseFloat(processValue) < parseFloat(opts.min)) {
  727. return $.extend(true, {
  728. "refreshFromBuffer": true,
  729. "buffer": (opts.prefix + opts.min).split("")
  730. }, opts.postFormat((opts.prefix + opts.min).split(""), 0, true, opts));
  731. }
  732. }
  733. if (opts.numericInput !== true) {
  734. var tmpBufSplit = opts.radixPoint !== "" ? buffer.join("").split(opts.radixPoint) : [buffer.join("")],
  735. matchRslt = tmpBufSplit[0].match(opts.regex.integerPart(opts)),
  736. matchRsltDigits = tmpBufSplit.length === 2 ? tmpBufSplit[1].match(opts.regex.integerNPart(opts)) : undefined;
  737. if (matchRslt) {
  738. if ((matchRslt[0] === opts.negationSymbol.front + "0" || matchRslt[0] === opts.negationSymbol.front || matchRslt[0] === "+") && (matchRsltDigits === undefined || matchRsltDigits[0].match(/^0+$/))) {
  739. buffer.splice(matchRslt.index, 1);
  740. }
  741. var radixPosition = $.inArray(opts.radixPoint, buffer);
  742. if (radixPosition !== -1) {
  743. if (isFinite(opts.digits) && !opts.digitsOptional) {
  744. for (var i = 1; i <= opts.digits; i++) {
  745. if (buffer[radixPosition + i] === undefined || buffer[radixPosition + i] === opts.placeholder.charAt(0)) {
  746. buffer[radixPosition + i] = "0";
  747. }
  748. }
  749. return {
  750. "refreshFromBuffer": maskedValue !== buffer.join(""),
  751. "buffer": buffer
  752. };
  753. } else if (radixPosition === buffer.length - opts.suffix.length - 1) {
  754. buffer.splice(radixPosition, 1);
  755. return {
  756. "refreshFromBuffer": true,
  757. "buffer": buffer
  758. };
  759. }
  760. }
  761. }
  762. }
  763. }
  764. if (opts.autoGroup) {
  765. var rslt = opts.postFormat(buffer, opts.numericInput ? caretPos : caretPos - 1, true, opts);
  766. rslt.caret = caretPos <= opts.prefix.length ? rslt.pos : rslt.pos + 1;
  767. return rslt;
  768. }
  769. },
  770. regex: {
  771. integerPart: function(opts) {
  772. return new RegExp("[" + Inputmask.escapeRegex(opts.negationSymbol.front) + "\+]?\\d+");
  773. },
  774. integerNPart: function(opts) {
  775. return new RegExp("[\\d" + Inputmask.escapeRegex(opts.groupSeparator) + "]+");
  776. }
  777. },
  778. signHandler: function(chrs, maskset, pos, strict, opts) {
  779. if (!strict && (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+")) {
  780. var matchRslt = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  781. if (matchRslt && matchRslt[0].length > 0) {
  782. if (maskset.buffer[matchRslt.index] === (chrs === "-" ? "+" : opts.negationSymbol.front)) {
  783. if (chrs === "-") {
  784. if (opts.negationSymbol.back !== "") {
  785. return {
  786. "pos": matchRslt.index,
  787. "c": opts.negationSymbol.front,
  788. "remove": matchRslt.index,
  789. "caret": pos,
  790. "insert": {
  791. "pos": maskset.buffer.length - opts.suffix.length - 1,
  792. "c": opts.negationSymbol.back
  793. }
  794. };
  795. } else {
  796. return {
  797. "pos": matchRslt.index,
  798. "c": opts.negationSymbol.front,
  799. "remove": matchRslt.index,
  800. "caret": pos
  801. };
  802. }
  803. } else {
  804. if (opts.negationSymbol.back !== "") {
  805. return {
  806. "pos": matchRslt.index,
  807. "c": "+",
  808. "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  809. "caret": pos
  810. };
  811. } else {
  812. return {
  813. "pos": matchRslt.index,
  814. "c": "+",
  815. "remove": matchRslt.index,
  816. "caret": pos
  817. };
  818. }
  819. }
  820. } else if (maskset.buffer[matchRslt.index] === (chrs === "-" ? opts.negationSymbol.front : "+")) {
  821. if (chrs === "-" && opts.negationSymbol.back !== "") {
  822. return {
  823. "remove": [matchRslt.index, maskset.buffer.length - opts.suffix.length - 1],
  824. "caret": pos - 1
  825. };
  826. } else {
  827. return {
  828. "remove": matchRslt.index,
  829. "caret": pos - 1
  830. };
  831. }
  832. } else {
  833. if (chrs === "-") {
  834. if (opts.negationSymbol.back !== "") {
  835. return {
  836. "pos": matchRslt.index,
  837. "c": opts.negationSymbol.front,
  838. "caret": pos + 1,
  839. "insert": {
  840. "pos": maskset.buffer.length - opts.suffix.length,
  841. "c": opts.negationSymbol.back
  842. }
  843. };
  844. } else {
  845. return {
  846. "pos": matchRslt.index,
  847. "c": opts.negationSymbol.front,
  848. "caret": pos + 1
  849. };
  850. }
  851. } else {
  852. return {
  853. "pos": matchRslt.index,
  854. "c": chrs,
  855. "caret": pos + 1
  856. };
  857. }
  858. }
  859. }
  860. }
  861. return false;
  862. },
  863. radixHandler: function(chrs, maskset, pos, strict, opts) {
  864. if (!strict) {
  865. if ($.inArray(chrs, [",", "."]) !== -1) chrs = opts.radixPoint;
  866. if (chrs === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0))) {
  867. var radixPos = $.inArray(opts.radixPoint, maskset.buffer),
  868. integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  869. if (radixPos !== -1 && maskset.validPositions[radixPos]) {
  870. if (maskset.validPositions[radixPos - 1]) {
  871. return {
  872. "caret": radixPos + 1
  873. };
  874. } else {
  875. return {
  876. "pos": integerValue.index,
  877. c: integerValue[0],
  878. "caret": radixPos + 1
  879. };
  880. }
  881. } else if (!integerValue || (integerValue["0"] === "0" && (integerValue.index + 1) !== pos)) {
  882. maskset.buffer[integerValue ? integerValue.index : pos] = "0";
  883. return {
  884. "pos": (integerValue ? integerValue.index : pos) + 1,
  885. c: opts.radixPoint
  886. };
  887. }
  888. }
  889. }
  890. return false;
  891. },
  892. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  893. if (opts.numericInput === true) {
  894. if (maskset.buffer[maskset.buffer.length - opts.prefix.length - 1] === "0") {
  895. return {
  896. "pos": pos,
  897. "remove": maskset.buffer.length - opts.prefix.length - 1
  898. };
  899. }
  900. } else {
  901. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)),
  902. radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  903. if (matchRslt && !strict && (radixPosition === -1 || pos <= radixPosition)) {
  904. if (matchRslt["0"].indexOf("0") === 0) {
  905. if (pos < opts.prefix.length) pos = matchRslt.index; //position
  906. var _radixPosition = $.inArray(opts.radixPoint, maskset._buffer);
  907. var digitsMatch = maskset._buffer && maskset.buffer.slice(radixPosition).join("") === maskset._buffer.slice(_radixPosition).join("") || parseInt(maskset.buffer.slice(radixPosition + 1).join("")) === 0;
  908. 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";
  909. if (radixPosition === -1 || digitsMatch && integerMatch) {
  910. maskset.buffer.splice(matchRslt.index, 1);
  911. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index;
  912. return {
  913. "pos": pos,
  914. "remove": matchRslt.index
  915. };
  916. } else if (matchRslt.index + 1 === pos || chrs === "0") {
  917. maskset.buffer.splice(matchRslt.index, 1);
  918. pos = matchRslt.index;
  919. return {
  920. "pos": pos,
  921. "remove": matchRslt.index
  922. };
  923. }
  924. } else if (chrs === "0" && pos <= matchRslt.index && matchRslt["0"] !== opts.groupSeparator) {
  925. return false;
  926. }
  927. }
  928. }
  929. return true;
  930. },
  931. postValidation: function(buffer, opts) {
  932. //handle maxvalue
  933. var isValid = true,
  934. maskedValue = buffer.join(""),
  935. processValue = maskedValue.replace(opts.prefix, "");
  936. processValue = processValue.replace(opts.suffix, "");
  937. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  938. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  939. //handle negation symbol
  940. processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
  941. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
  942. processValue = processValue === opts.negationSymbol.front ? processValue + "0" : processValue;
  943. if (isFinite(processValue)) {
  944. if (opts.max !== null && isFinite(opts.max)) {
  945. isValid = parseFloat(processValue) <= parseFloat(opts.max);
  946. }
  947. if (isValid && opts.min !== null && isFinite(opts.min) && (processValue <= 0 || processValue.toString().length >= opts.min.toString().length)) {
  948. isValid = parseFloat(processValue) >= parseFloat(opts.min);
  949. if (!isValid) {
  950. isValid = $.extend(true, {
  951. "refreshFromBuffer": true,
  952. "buffer": (opts.prefix + opts.min).split("")
  953. }, opts.postFormat((opts.prefix + opts.min).split(""), 0, true, opts));
  954. isValid.refreshFromBuffer = true; //enforce refresh
  955. }
  956. }
  957. }
  958. return isValid;
  959. },
  960. definitions: {
  961. "~": {
  962. validator: function(chrs, maskset, pos, strict, opts) {
  963. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  964. if (!isValid) {
  965. isValid = opts.radixHandler(chrs, maskset, pos, strict, opts);
  966. if (!isValid) {
  967. isValid = strict ? new RegExp("[0-9" + Inputmask.escapeRegex(opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs);
  968. if (isValid === true) {
  969. isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts);
  970. if (isValid === true) {
  971. //handle overwrite when fixed precision
  972. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  973. if (radixPosition !== -1 && opts.digitsOptional === false && opts.numericInput !== true && pos > radixPosition && !strict) {
  974. isValid = {
  975. "pos": pos,
  976. "remove": pos
  977. };
  978. } else {
  979. isValid = {
  980. pos: pos
  981. };
  982. }
  983. }
  984. }
  985. }
  986. }
  987. return isValid;
  988. },
  989. cardinality: 1,
  990. prevalidator: null
  991. },
  992. "+": {
  993. validator: function(chrs, maskset, pos, strict, opts) {
  994. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  995. if (!isValid && ((strict && opts.allowMinus && chrs === opts.negationSymbol.front) || (opts.allowMinus && chrs === "-") || (opts.allowPlus && chrs === "+"))) {
  996. if (chrs === "-") {
  997. if (opts.negationSymbol.back !== "") {
  998. isValid = {
  999. "pos": pos,
  1000. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  1001. "caret": pos + 1,
  1002. "insert": {
  1003. "pos": maskset.buffer.length,
  1004. "c": opts.negationSymbol.back
  1005. }
  1006. };
  1007. } else {
  1008. isValid = {
  1009. "pos": pos,
  1010. "c": chrs === "-" ? opts.negationSymbol.front : "+",
  1011. "caret": pos + 1
  1012. };
  1013. }
  1014. } else {
  1015. isValid = true;
  1016. }
  1017. }
  1018. return isValid;
  1019. },
  1020. cardinality: 1,
  1021. prevalidator: null,
  1022. placeholder: ""
  1023. },
  1024. "-": {
  1025. validator: function(chrs, maskset, pos, strict, opts) {
  1026. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  1027. if (!isValid && strict && opts.allowMinus && chrs === opts.negationSymbol.back) {
  1028. isValid = true;
  1029. }
  1030. return isValid;
  1031. },
  1032. cardinality: 1,
  1033. prevalidator: null,
  1034. placeholder: ""
  1035. },
  1036. ":": {
  1037. validator: function(chrs, maskset, pos, strict, opts) {
  1038. var isValid = opts.signHandler(chrs, maskset, pos, strict, opts);
  1039. if (!isValid) {
  1040. var radix = "[" + Inputmask.escapeRegex(opts.radixPoint) + ",\\." + "]";
  1041. isValid = new RegExp(radix).test(chrs);
  1042. if (isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder === opts.radixPoint) {
  1043. isValid = {
  1044. "caret": pos + 1
  1045. };
  1046. }
  1047. }
  1048. return isValid ? {
  1049. c: opts.radixPoint
  1050. } : isValid;
  1051. },
  1052. cardinality: 1,
  1053. prevalidator: null,
  1054. placeholder: function(opts) {
  1055. return opts.radixPoint;
  1056. }
  1057. }
  1058. },
  1059. onUnMask: function(maskedValue, unmaskedValue, opts) {
  1060. var processValue = maskedValue.replace(opts.prefix, "");
  1061. processValue = processValue.replace(opts.suffix, "");
  1062. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  1063. if (opts.unmaskAsNumber) {
  1064. if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
  1065. return Number(processValue);
  1066. }
  1067. return processValue;
  1068. },
  1069. isComplete: function(buffer, opts) {
  1070. var maskedValue = buffer.join(""),
  1071. bufClone = buffer.slice();
  1072. //verify separator positions
  1073. opts.postFormat(bufClone, 0, true, opts);
  1074. if (bufClone.join("") !== maskedValue) return false;
  1075. var processValue = maskedValue.replace(opts.prefix, "");
  1076. processValue = processValue.replace(opts.suffix, "");
  1077. processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  1078. if (opts.radixPoint === ",") processValue = processValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  1079. return isFinite(processValue);
  1080. },
  1081. onBeforeMask: function(initialValue, opts) {
  1082. if (opts.radixPoint !== "" && isFinite(initialValue)) {
  1083. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  1084. } else {
  1085. var kommaMatches = initialValue.match(/,/g);
  1086. var dotMatches = initialValue.match(/\./g);
  1087. if (dotMatches && kommaMatches) {
  1088. if (dotMatches.length > kommaMatches.length) {
  1089. initialValue = initialValue.replace(/\./g, "");
  1090. initialValue = initialValue.replace(",", opts.radixPoint);
  1091. } else if (kommaMatches.length > dotMatches.length) {
  1092. initialValue = initialValue.replace(/,/g, "");
  1093. initialValue = initialValue.replace(".", opts.radixPoint);
  1094. } else { //equal
  1095. initialValue = initialValue.indexOf(".") < initialValue.indexOf(",") ? initialValue.replace(/\./g, "") : initialValue = initialValue.replace(/,/g, "");
  1096. }
  1097. } else {
  1098. initialValue = initialValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
  1099. }
  1100. }
  1101. if (opts.digits === 0) {
  1102. if (initialValue.indexOf(".") !== -1) {
  1103. initialValue = initialValue.substring(0, initialValue.indexOf("."));
  1104. } else if (initialValue.indexOf(",") !== -1) {
  1105. initialValue = initialValue.substring(0, initialValue.indexOf(","));
  1106. }
  1107. }
  1108. if (opts.radixPoint !== "" && isFinite(opts.digits) && initialValue.indexOf(opts.radixPoint) !== -1) {
  1109. var valueParts = initialValue.split(opts.radixPoint),
  1110. decPart = valueParts[1].match(new RegExp("\\d*"))[0];
  1111. if (parseInt(opts.digits) < decPart.toString().length) {
  1112. var digitsFactor = Math.pow(10, parseInt(opts.digits));
  1113. //make the initialValue a valid javascript number for the parsefloat
  1114. initialValue = initialValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
  1115. initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
  1116. initialValue = initialValue.toString().replace(".", opts.radixPoint);
  1117. }
  1118. }
  1119. return initialValue.toString();
  1120. },
  1121. canClearPosition: function(maskset, position, lvp, strict, opts) {
  1122. var positionInput = maskset.validPositions[position].input,
  1123. canClear = ((positionInput !== opts.radixPoint || (maskset.validPositions[position].match.fn !== null && opts.decimalProtect === false)) || isFinite(positionInput)) ||
  1124. position === lvp ||
  1125. positionInput === opts.groupSeparator ||
  1126. positionInput === opts.negationSymbol.front ||
  1127. positionInput === opts.negationSymbol.back;
  1128. if (canClear && isFinite(positionInput)) {
  1129. var matchRslt,
  1130. radixPos = $.inArray(opts.radixPoint, maskset.buffer);
  1131. //inject radixpoint
  1132. var radixInjection = false;
  1133. if (maskset.validPositions[radixPos] === undefined) {
  1134. maskset.validPositions[radixPos] = {
  1135. input: opts.radixPoint
  1136. };
  1137. radixInjection = true;
  1138. }
  1139. if (!strict && maskset.buffer) {
  1140. matchRslt = maskset.buffer.join("").substr(0, position).match(opts.regex.integerNPart(opts));
  1141. var pos = position + 1,
  1142. isNull = matchRslt == null || parseInt(matchRslt["0"].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "")) === 0;
  1143. if (isNull) {
  1144. while (maskset.validPositions[pos] && (maskset.validPositions[pos].input === opts.groupSeparator || maskset.validPositions[pos].input === "0")) {
  1145. delete maskset.validPositions[pos];
  1146. pos++;
  1147. }
  1148. }
  1149. }
  1150. var buffer = [];
  1151. //build new buffer from validPositions
  1152. for (var vp in maskset.validPositions) {
  1153. if (maskset.validPositions[vp].input !== undefined) buffer.push(maskset.validPositions[vp].input);
  1154. }
  1155. //remove radix Injection
  1156. if (radixInjection) {
  1157. delete maskset.validPositions[radixPos];
  1158. }
  1159. if (radixPos > 0) {
  1160. var bufVal = buffer.join("");
  1161. matchRslt = bufVal.match(opts.regex.integerNPart(opts));
  1162. if (matchRslt) {
  1163. if (position <= radixPos) {
  1164. if (matchRslt["0"].indexOf("0") === 0) {
  1165. canClear = matchRslt.index !== position || opts.placeholder === "0";
  1166. } else {
  1167. var intPart = parseInt(matchRslt["0"].replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "")),
  1168. radixPart = parseInt(bufVal.split(opts.radixPoint)[1]);
  1169. if (intPart < 10 && maskset.validPositions[position] && (opts.placeholder !== "0" || radixPart > 0)) {
  1170. maskset.validPositions[position].input = "0";
  1171. maskset.p = opts.prefix.length + 1;
  1172. canClear = false;
  1173. }
  1174. }
  1175. } else if (matchRslt["0"].indexOf("0") === 0) {
  1176. if (bufVal.length === 3) {
  1177. maskset.validPositions = {};
  1178. canClear = false;
  1179. }
  1180. }
  1181. }
  1182. }
  1183. }
  1184. return canClear;
  1185. },
  1186. onKeyDown: function(e, buffer, caretPos, opts) {
  1187. var $input = $(this);
  1188. if (e.ctrlKey) {
  1189. switch (e.keyCode) {
  1190. case Inputmask.keyCode.UP:
  1191. $input.val(parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
  1192. $input.trigger("setvalue");
  1193. break;
  1194. case Inputmask.keyCode.DOWN:
  1195. $input.val(parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
  1196. $input.trigger("setvalue");
  1197. break;
  1198. }
  1199. }
  1200. }
  1201. },
  1202. "currency": {
  1203. prefix: "$ ",
  1204. groupSeparator: ",",
  1205. alias: "numeric",
  1206. placeholder: "0",
  1207. autoGroup: true,
  1208. digits: 2,
  1209. digitsOptional: false,
  1210. clearMaskOnLostFocus: false
  1211. },
  1212. "decimal": {
  1213. alias: "numeric"
  1214. },
  1215. "integer": {
  1216. alias: "numeric",
  1217. digits: 0,
  1218. radixPoint: ""
  1219. },
  1220. "percentage": {
  1221. alias: "numeric",
  1222. digits: 2,
  1223. radixPoint: ".",
  1224. placeholder: "0",
  1225. autoGroup: false,
  1226. min: 0,
  1227. max: 100,
  1228. suffix: " %",
  1229. allowPlus: false,
  1230. allowMinus: false
  1231. }
  1232. });
  1233. return Inputmask;
  1234. }));