inputmask.numeric.extensions.js 50 KB

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