jquery.inputmask.js 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /**
  2. * @license Input Mask plugin for jquery
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2014 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 0.0.0
  7. */
  8. (function ($) {
  9. if ($.fn.inputmask === undefined) {
  10. //helper functions
  11. function isInputEventSupported(eventName) {
  12. var el = document.createElement('input'),
  13. eventName = 'on' + eventName,
  14. isSupported = (eventName in el);
  15. if (!isSupported) {
  16. el.setAttribute(eventName, 'return;');
  17. isSupported = typeof el[eventName] == 'function';
  18. }
  19. el = null;
  20. return isSupported;
  21. }
  22. function resolveAlias(aliasStr, options, opts) {
  23. var aliasDefinition = opts.aliases[aliasStr];
  24. if (aliasDefinition) {
  25. if (aliasDefinition.alias) resolveAlias(aliasDefinition.alias, undefined, opts); //alias is another alias
  26. $.extend(true, opts, aliasDefinition); //merge alias definition in the options
  27. $.extend(true, opts, options); //reapply extra given options
  28. return true;
  29. }
  30. return false;
  31. }
  32. function generateMaskSet(opts) {
  33. var ms;
  34. function analyseMask(mask) {
  35. if (opts.numericInput) {
  36. mask = mask.split('').reverse().join('');
  37. }
  38. var tokenizer = /(?:[?*+]|\{[0-9]+(?:,[0-9]*)?\})\??|[^.?*+^${[]()|\\]+|./g,
  39. escaped = false;
  40. function maskToken(isGroup, isOptional, isQuantifier) {
  41. this.matches = [];
  42. this.isGroup = isGroup || false;
  43. this.isOptional = isOptional || false;
  44. this.isQuantifier = isQuantifier || false;
  45. this.quantifier = { min: 1, max: 1 };
  46. };
  47. //test definition => {fn: RegExp/function, cardinality: int, optionality: bool, newBlockMarker: bool, offset: int, casing: null/upper/lower, def: definitionSymbol}
  48. function insertTestDefinition(mtoken, element, position) {
  49. var maskdef = opts.definitions[element];
  50. position = position != undefined ? position : mtoken.matches.length;
  51. if (maskdef && !escaped) {
  52. var prevalidators = maskdef["prevalidator"], prevalidatorsL = prevalidators ? prevalidators.length : 0;
  53. for (var i = 1; i < maskdef.cardinality; i++) {
  54. var prevalidator = prevalidatorsL >= i ? prevalidators[i - 1] : [], validator = prevalidator["validator"], cardinality = prevalidator["cardinality"];
  55. mtoken.matches.splice(position++, 0, { fn: validator ? typeof validator == 'string' ? new RegExp(validator) : new function () { this.test = validator; } : new RegExp("."), cardinality: cardinality ? cardinality : 1, optionality: mtoken.isOptional, casing: maskdef["casing"], def: maskdef["definitionSymbol"] || element });
  56. }
  57. mtoken.matches.splice(position++, 0, { fn: maskdef.validator ? typeof maskdef.validator == 'string' ? new RegExp(maskdef.validator) : new function () { this.test = maskdef.validator; } : new RegExp("."), cardinality: maskdef.cardinality, optionality: mtoken.isOptional, casing: maskdef["casing"], def: maskdef["definitionSymbol"] || element });
  58. } else {
  59. mtoken.matches.splice(position++, 0, { fn: null, cardinality: 0, optionality: mtoken.isOptional, casing: null, def: element });
  60. escaped = false;
  61. }
  62. }
  63. var currentToken = new maskToken(),
  64. match, m, openenings = [], maskTokens = [];
  65. while (match = tokenizer.exec(mask)) {
  66. m = match[0];
  67. switch (m.charAt(0)) {
  68. case opts.optionalmarker.end:
  69. // optional closing
  70. case opts.groupmarker.end:
  71. // Group closing
  72. var openingToken = openenings.pop();
  73. if (openenings.length > 0) {
  74. openenings[openenings.length - 1]["matches"].push(openingToken);
  75. } else {
  76. currentToken.matches.push(openingToken);
  77. }
  78. break;
  79. case opts.optionalmarker.start:
  80. // optional opening
  81. openenings.push(new maskToken(false, true));
  82. break;
  83. case opts.groupmarker.start:
  84. // Group opening
  85. openenings.push(new maskToken(true));
  86. break;
  87. case opts.quantifiermarker.start:
  88. //Quantifier
  89. var quantifier = new maskToken(false, false, true);
  90. m = m.replace(/[{}]/g, "");
  91. var mq = m.split(","), mq0 = isNaN(mq[0]) ? mq[0] : parseInt(mq[0]), mq1 = mq.length == 1 ? mq0 : (isNaN(mq[1]) ? mq[1] : parseInt(mq[1]));
  92. quantifier.quantifier = { min: mq0, max: mq1 };
  93. if (openenings.length > 0) {
  94. var matches = openenings[openenings.length - 1]["matches"];
  95. var match = matches.pop();
  96. if (!match["isGroup"]) {
  97. var groupToken = new maskToken(true);
  98. groupToken.matches.push(match);
  99. match = groupToken;
  100. }
  101. matches.push(match);
  102. matches.push(quantifier);
  103. } else {
  104. var match = currentToken.matches.pop();
  105. if (!match["isGroup"]) {
  106. var groupToken = new maskToken(true);
  107. groupToken.matches.push(match);
  108. match = groupToken;
  109. }
  110. currentToken.matches.push(match);
  111. currentToken.matches.push(quantifier);
  112. }
  113. break;
  114. case opts.escapeChar:
  115. escaped = true;
  116. break;
  117. default:
  118. if (openenings.length > 0) {
  119. insertTestDefinition(openenings[openenings.length - 1], m);
  120. } else {
  121. if (currentToken.matches.length > 0) {
  122. var lastMatch = currentToken.matches[currentToken.matches.length - 1];
  123. if (lastMatch["isGroup"]) { //this is not a group but a normal mask => convert
  124. lastMatch.isGroup = false;
  125. insertTestDefinition(lastMatch, opts.groupmarker.start, 0);
  126. insertTestDefinition(lastMatch, opts.groupmarker.end);
  127. }
  128. }
  129. insertTestDefinition(currentToken, m);
  130. }
  131. }
  132. }
  133. if (currentToken.matches.length > 0)
  134. maskTokens.push(currentToken);
  135. if (opts.repeat > 0 || opts.repeat == "*" || opts.repeat == "+") {
  136. var groupedMaskToken = new maskToken(false, false, false);
  137. var groupToken = new maskToken(true),
  138. quantifierToken = new maskToken(false, false, true);
  139. quantifierToken.quantifier = opts.repeat == "*" ? { min: 0, max: "*" } : (opts.repeat == "+" ? { min: 1, max: "*" } : { min: opts.greedy ? opts.repeat : 1, max: opts.repeat });
  140. if (maskTokens.length > 1) {
  141. groupToken.matches = maskTokens;
  142. groupedMaskToken.matches.push(groupToken);
  143. groupedMaskToken.matches.push(quantifierToken);
  144. } else {
  145. groupToken.matches = maskTokens[0].matches;
  146. groupedMaskToken.matches.push(groupToken);
  147. groupedMaskToken.matches.push(quantifierToken);
  148. }
  149. maskTokens = [groupedMaskToken];
  150. }
  151. console.log(JSON.stringify(maskTokens));
  152. return maskTokens;
  153. }
  154. function generateMask(mask, metadata) {
  155. return mask == undefined || mask == "" ? undefined : {
  156. "mask": mask,
  157. "maskToken": analyseMask(mask),
  158. "validPositions": {},
  159. "_buffer": undefined,
  160. "buffer": undefined,
  161. "tests": {},
  162. "metadata": metadata
  163. };
  164. }
  165. if (opts.repeat == "*" || opts.repeat == "+") opts.greedy = false;
  166. if ($.isFunction(opts.mask)) { //allow mask to be a preprocessing fn - should return a valid mask
  167. opts.mask = opts.mask.call(this, opts);
  168. }
  169. if ($.isArray(opts.mask)) {
  170. $.each(opts.mask, function (ndx, msk) {
  171. if (msk["mask"] != undefined) {
  172. ms = generateMask(msk["mask"].toString(), msk);
  173. } else {
  174. ms = generateMask(msk.toString());
  175. }
  176. return false; //break after first multiple not supported for now (again)
  177. });
  178. } else {
  179. if (opts.mask.length == 1 && opts.greedy == false && opts.repeat != 0) {
  180. opts.placeholder = "";
  181. } //hide placeholder with single non-greedy mask
  182. if (opts.mask["mask"] != undefined) {
  183. ms = generateMask(opts.mask["mask"].toString(), opts.mask);
  184. } else {
  185. ms = generateMask(opts.mask.toString());
  186. }
  187. }
  188. return ms;
  189. }
  190. var msie1x = typeof ScriptEngineMajorVersion === "function"
  191. ? ScriptEngineMajorVersion() //IE11 detection
  192. : new Function("/*@cc_on return @_jscript_version; @*/")() >= 10, //conditional compilation from mickeysoft trick
  193. ua = navigator.userAgent,
  194. iphone = ua.match(new RegExp("iphone", "i")) !== null,
  195. android = ua.match(new RegExp("android.*safari.*", "i")) !== null,
  196. androidchrome = ua.match(new RegExp("android.*chrome.*", "i")) !== null,
  197. androidfirefox = ua.match(new RegExp("android.*firefox.*", "i")) !== null,
  198. kindle = /Kindle/i.test(ua) || /Silk/i.test(ua) || /KFTT/i.test(ua) || /KFOT/i.test(ua) || /KFJWA/i.test(ua) || /KFJWI/i.test(ua) || /KFSOWI/i.test(ua) || /KFTHWA/i.test(ua) || /KFTHWI/i.test(ua) || /KFAPWA/i.test(ua) || /KFAPWI/i.test(ua),
  199. PasteEventType = isInputEventSupported('paste') ? 'paste' : isInputEventSupported('input') ? 'input' : "propertychange";
  200. //if (androidchrome) {
  201. // var browser = navigator.userAgent.match(new RegExp("chrome.*", "i")),
  202. // version = parseInt(new RegExp(/[0-9]+/).exec(browser));
  203. // androidchrome32 = (version == 32);
  204. //}
  205. //masking scope
  206. //actionObj definition see below
  207. function maskScope(maskset, opts, actionObj) {
  208. var isRTL = false,
  209. valueOnFocus = getBuffer().join(''),
  210. $el,
  211. skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
  212. skipInputEvent = false, //skip when triggered from within inputmask
  213. ignorable = false;
  214. //maskset helperfunctions
  215. function getMaskTemplate(baseOnInput, minimalPos) {
  216. minimalPos = minimalPos || 0;
  217. var maskTemplate = [], ndxIntlzr, pos = 0, test;
  218. do {
  219. if (baseOnInput === true && getMaskSet()['validPositions'][pos]) {
  220. var validPos = getMaskSet()['validPositions'][pos];
  221. test = validPos["match"];
  222. ndxIntlzr = validPos["locator"].slice();
  223. maskTemplate.push(test["fn"] == null ? test["def"] : opts.placeholder.charAt(pos % opts.placeholder.length));
  224. } else {
  225. var testPos = getTests(pos, false, ndxIntlzr, pos - 1);
  226. testPos = testPos[opts.greedy || minimalPos > pos ? 0 : (testPos.length - 1)];
  227. test = testPos["match"];
  228. ndxIntlzr = testPos["locator"].slice();
  229. maskTemplate.push(test["fn"] == null ? test["def"] : opts.placeholder.charAt(pos % opts.placeholder.length));
  230. }
  231. pos++;
  232. } while (test["fn"] != null || (test["fn"] == null && test["def"] != "") || minimalPos >= pos);
  233. maskTemplate.pop(); //drop the last one which is empty
  234. return maskTemplate;
  235. }
  236. function getMaskSet() {
  237. return maskset;
  238. }
  239. function resetMaskSet() {
  240. var maskset = getMaskSet();
  241. maskset["buffer"] = undefined;
  242. maskset["_buffer"] = undefined;
  243. maskset["validPositions"] = {};
  244. maskset["p"] = -1;
  245. }
  246. function getLastValidPosition(maskset, closestTo) { //TODO implement closest to
  247. maskset = maskset || getMaskSet();
  248. var lastValidPosition = -1;
  249. for (var posNdx in maskset["validPositions"]) {
  250. var psNdx = parseInt(posNdx);
  251. if (psNdx > lastValidPosition) lastValidPosition = psNdx;
  252. }
  253. return lastValidPosition;
  254. }
  255. function setLastValidPosition(pos, maskset) {
  256. maskset = maskset || getMaskSet();
  257. for (var posNdx in maskset["validPositions"]) {
  258. if (posNdx > pos) maskset["validPositions"][posNdx] = undefined;
  259. }
  260. }
  261. function getTest(pos) {
  262. if (getMaskSet()['validPositions'][pos]) {
  263. return getMaskSet()['validPositions'][pos]["match"];
  264. }
  265. return getTests(pos)[0]["match"];
  266. }
  267. function getTests(pos, disableCache, ndxIntlzr, tstPs) {
  268. var maskTokens = getMaskSet()["maskToken"], testPos = ndxIntlzr ? tstPs : 0, ndxInitializer = ndxIntlzr || [0], matches = [], insertStop = false;
  269. function ResolveTestFromToken(maskToken, ndxInitializer, loopNdx, quantifierRecurse) { //ndxInitilizer contains a set of indexes to speedup searches in the mtokens
  270. function handleMatch(match, loopNdx, quantifierRecurse) {
  271. var currentPos = testPos;
  272. if (testPos == pos && match.matches == undefined) {
  273. matches.push({ "match": match, "locator": loopNdx.reverse() });
  274. return true;
  275. } else if (match.matches != undefined) {
  276. if (match.isGroup && quantifierRecurse !== true) { //when a group pass along to the quantifier
  277. match = handleMatch(maskToken.matches[tndx + 1], loopNdx);
  278. if (match) return true;
  279. } else if (match.isOptional) {
  280. match = ResolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
  281. if (match) {
  282. //search for next possible match
  283. testPos = currentPos;
  284. }
  285. } else if (match.isQuantifier && quantifierRecurse !== true) {
  286. var qt = match;
  287. for (var qndx = (ndxInitializer.length > 0 && quantifierRecurse !== true) ? ndxInitializer.shift() : 0; (qndx < (isNaN(qt.quantifier.max) ? qndx + 1 : qt.quantifier.max)) && testPos <= pos; qndx++) {
  288. var tokenGroup = maskToken.matches[maskToken.matches.indexOf(qt) - 1];
  289. match = handleMatch(tokenGroup, [qndx].concat(loopNdx), true);
  290. if (match) {
  291. //get latest match
  292. var latestMatch = matches[matches.length - 1]["match"];
  293. var isFirstMatch = (tokenGroup.matches.indexOf(latestMatch) == 0);
  294. if (isFirstMatch) { //search for next possible match
  295. if (qndx > qt.quantifier.min - 1) {
  296. insertStop = true;
  297. testPos = pos; //match the position after the group
  298. break; //stop quantifierloop
  299. } else return true;
  300. } else {
  301. return true;
  302. }
  303. }
  304. }
  305. } else {
  306. match = ResolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
  307. if (match)
  308. return true;
  309. }
  310. } else testPos++;
  311. }
  312. for (var tndx = (ndxInitializer.length > 0 ? ndxInitializer.shift() : 0) ; tndx < maskToken.matches.length; tndx++) {
  313. if (maskToken.matches[tndx]["isQuantifier"] !== true) {
  314. var match = handleMatch(maskToken.matches[tndx], [tndx].concat(loopNdx), quantifierRecurse);
  315. if (match && testPos == pos) {
  316. return match;
  317. } else if (testPos > pos) {
  318. break;
  319. }
  320. }
  321. }
  322. }
  323. if (disableCache !== true && getMaskSet()['tests'][pos] && !getMaskSet()['validPositions'][pos]) {
  324. return getMaskSet()['tests'][pos];
  325. }
  326. if (ndxIntlzr == undefined) {
  327. var previousPos = pos - 1, test;
  328. while ((test = getMaskSet()['validPositions'][previousPos]) == undefined && previousPos > -1) {
  329. previousPos--;
  330. }
  331. if (test != undefined && previousPos > -1) {
  332. testPos = previousPos;
  333. ndxInitializer = test["locator"].slice();
  334. } else {
  335. previousPos = pos - 1;
  336. while ((test = getMaskSet()['tests'][previousPos]) == undefined && previousPos > -1) {
  337. previousPos--;
  338. }
  339. if (test != undefined && previousPos > -1) {
  340. testPos = previousPos;
  341. ndxInitializer = test[0]["locator"].slice();
  342. }
  343. }
  344. }
  345. for (var mtndx = ndxInitializer.shift() ; mtndx < maskTokens.length; mtndx++) {
  346. var match = ResolveTestFromToken(maskTokens[mtndx], ndxInitializer, [mtndx]);
  347. if ((match && testPos == pos) || testPos > pos) {
  348. break;
  349. }
  350. }
  351. if (matches.length == 0 || insertStop)
  352. matches.push({ "match": { fn: null, cardinality: 0, optionality: true, casing: null, def: "" }, "locator": [] });
  353. getMaskSet()['tests'][pos] = matches;
  354. console.log(pos + " - " + JSON.stringify(matches));
  355. return matches;
  356. }
  357. function getBufferTemplate() {
  358. if (getMaskSet()['_buffer'] == undefined) {
  359. //generate template
  360. getMaskSet()["_buffer"] = getMaskTemplate(false, 1);
  361. }
  362. return getMaskSet()['_buffer'];
  363. }
  364. function getBuffer() {
  365. if (getMaskSet()['buffer'] == undefined) {
  366. getMaskSet()['buffer'] = getMaskTemplate(true);
  367. }
  368. return getMaskSet()['buffer'];
  369. }
  370. function isValid(pos, c, strict) { //strict true ~ no correction or autofill
  371. strict = strict === true; //always set a value to strict to prevent possible strange behavior in the extensions
  372. function _isValid(position, c, strict) {
  373. var rslt = false;
  374. $.each(getTests(position, !strict), function (ndx, tst) {
  375. var test = tst["match"];
  376. var loopend = c ? 1 : 0, chrs = '', buffer = getBuffer();
  377. for (var i = test.cardinality; i > loopend; i--) {
  378. chrs += getBufferElement(buffer, position - (i - 1), true);
  379. }
  380. if (c) {
  381. chrs += c;
  382. }
  383. //return is false or a json object => { pos: ??, c: ??} or true
  384. rslt = test.fn != null ?
  385. test.fn.test(chrs, buffer, position, strict, opts)
  386. : (c == getPlaceholder(position) || c == opts.skipOptionalPartCharacter) ?
  387. { "refresh": true, c: getPlaceholder(position), pos: position }
  388. : false;
  389. if (rslt !== false) {
  390. var elem = c;
  391. switch (test.casing) {
  392. case "upper":
  393. elem = elem.toUpperCase();
  394. break;
  395. case "lower":
  396. elem = elem.toLowerCase();
  397. break;
  398. }
  399. var validatedPos = position;
  400. if (rslt !== true && rslt["pos"] != position) {
  401. validatedPos = rslt["pos"];
  402. tst = getTests(validatedPos, !strict)[0]; //possible mismatch TODO
  403. }
  404. getMaskSet()["validPositions"][validatedPos] = $.extend({}, tst, { "input": elem });
  405. return false; //break from $.each
  406. }
  407. });
  408. return rslt;
  409. }
  410. var maskPos = pos;
  411. var result = _isValid(maskPos, c, strict);
  412. if (!strict && result === false && !isMask(maskPos)) { //does the input match on a further position?
  413. maskPos = seekNext(maskPos);
  414. result = _isValid(maskPos, c, strict);
  415. }
  416. if (result === true) result = { "pos": maskPos };
  417. return result;
  418. }
  419. function isMask(pos) {
  420. var test = getTest(pos);
  421. return test.fn != null ? test.fn : false;
  422. }
  423. function getMaskLength() {
  424. var maxLength = $el.prop('maxLength'), maskLength;
  425. if (opts.greedy == false) {
  426. var lvp = getLastValidPosition() + 1,
  427. test = getTest(lvp);
  428. while (test.fn != null && test.def != "") {
  429. var tests = getTests(++lvp);
  430. test = tests[tests.length - 1];
  431. }
  432. maskLength = getMaskTemplate(false, lvp).length;
  433. } else
  434. maskLength = getBuffer().length;
  435. return maxLength == undefined || (maskLength < maxLength && maxLength > -1) /* FF sets no defined max length to -1 */ ? maskLength : maxLength;
  436. }
  437. function seekNext(pos) {
  438. var maskL = getMaskLength();
  439. if (pos >= maskL) return maskL;
  440. var position = pos;
  441. while (++position < maskL && !isMask(position)) {
  442. }
  443. return position;
  444. }
  445. function seekPrevious(pos) {
  446. var position = pos;
  447. if (position <= 0) return 0;
  448. while (--position > 0 && !isMask(position)) {
  449. }
  450. ;
  451. return position;
  452. }
  453. function setBufferElement(buffer, position, element) {
  454. position = prepareBuffer(buffer, position);
  455. var test = getTest(position);
  456. var elem = element;
  457. if (elem != undefined && test != undefined) {
  458. switch (test.casing) {
  459. case "upper":
  460. elem = element.toUpperCase();
  461. break;
  462. case "lower":
  463. elem = element.toLowerCase();
  464. break;
  465. }
  466. }
  467. buffer[position] = elem;
  468. }
  469. function getBufferElement(buffer, position) {
  470. position = prepareBuffer(buffer, position);
  471. return buffer[position];
  472. }
  473. //needed to handle the non-greedy mask repetitions
  474. function prepareBuffer(buffer, position) { //TODO DROP BUFFER PASSING + optimize me
  475. if (buffer.length <= position) {
  476. var trbuffer = getMaskTemplate(true, position);
  477. buffer.length = trbuffer.length;
  478. for (var i = 0, bl = buffer.length; i < bl; i++) {
  479. if (buffer[i] == undefined)
  480. buffer[i] = trbuffer[i];
  481. }
  482. buffer[position] = getPlaceholder(position);
  483. }
  484. return position;
  485. }
  486. function writeBuffer(input, buffer, caretPos) {
  487. input._valueSet(buffer.join(''));
  488. if (caretPos != undefined) {
  489. caret(input, caretPos);
  490. }
  491. }
  492. function clearBuffer(buffer, start, end, stripNomasks) {
  493. for (var i = start, maskL = getMaskLength() ; i < end && i < maskL; i++) {
  494. if (stripNomasks === true) {
  495. if (!isMask(i))
  496. setBufferElement(buffer, i, "");
  497. } else
  498. setBufferElement(buffer, i, getPlaceholder(i));
  499. }
  500. }
  501. function setPlaceholder(pos) {
  502. setBufferElement(getBuffer(), pos, getPlaceholder(pos));
  503. }
  504. function getPlaceholder(pos) {
  505. var test = getTest(pos);
  506. return test["fn"] == null ? test["def"] : opts.placeholder.charAt(pos % opts.placeholder.length);
  507. }
  508. function checkVal(input, writeOut, strict, nptvl, intelliCheck) {
  509. var inputValue = nptvl != undefined ? nptvl.slice() : truncateInput(input._valueGet()).split('');
  510. resetMaskSet();
  511. if (writeOut) input._valueSet(""); //initial clear
  512. $.each(inputValue, function (ndx, charCode) {
  513. if (intelliCheck === true) {
  514. var p = getMaskSet()["p"], lvp = p == -1 ? p : seekPrevious(p),
  515. pos = lvp == -1 ? ndx : seekNext(lvp);
  516. if ($.inArray(charCode, getBufferTemplate().slice(lvp + 1, pos)) == -1) {
  517. keypressEvent.call(input, undefined, true, charCode.charCodeAt(0), writeOut, strict, ndx);
  518. }
  519. } else {
  520. keypressEvent.call(input, undefined, true, charCode.charCodeAt(0), writeOut, strict, ndx);
  521. strict = strict || (ndx > 0 && ndx > getMaskSet()["p"]);
  522. }
  523. });
  524. }
  525. function escapeRegex(str) {
  526. return $.inputmask.escapeRegex.call(this, str);
  527. }
  528. function truncateInput(inputValue) {
  529. return inputValue.replace(new RegExp("(" + escapeRegex(getBufferTemplate().join('')) + ")*$"), "");
  530. }
  531. function clearOptionalTail(input) {
  532. var buffer = getBuffer(), tmpBuffer = buffer.slice(), testPos, pos;
  533. for (var pos = tmpBuffer.length - 1; pos >= 0; pos--) {
  534. if (getTest(pos).optionality) {
  535. if (!isMask(pos) || !isValid(pos, buffer[pos], true))
  536. tmpBuffer.pop();
  537. else break;
  538. } else break;
  539. }
  540. writeBuffer(input, tmpBuffer);
  541. }
  542. function unmaskedvalue($input, skipDatepickerCheck) {
  543. if ($input.data('_inputmask') && (skipDatepickerCheck === true || !$input.hasClass('hasDatepicker'))) {
  544. var umValue = $.map(getBuffer(), function (element, index) {
  545. return isMask(index) && isValid(index, element, true) ? element : null;
  546. });
  547. var unmaskedValue = (isRTL ? umValue.reverse() : umValue).join('');
  548. return $.isFunction(opts.onUnMask) ? opts.onUnMask.call($input, getBuffer().join(''), unmaskedValue, opts) : unmaskedValue;
  549. } else {
  550. return $input[0]._valueGet();
  551. }
  552. }
  553. function TranslatePosition(pos) {
  554. if (isRTL && typeof pos == 'number' && (!opts.greedy || opts.placeholder != "")) {
  555. var bffrLght = getBuffer().length;
  556. pos = bffrLght - pos;
  557. }
  558. return pos;
  559. }
  560. function caret(input, begin, end) {
  561. var npt = input.jquery && input.length > 0 ? input[0] : input, range;
  562. if (typeof begin == 'number') {
  563. begin = TranslatePosition(begin);
  564. end = TranslatePosition(end);
  565. if (!$(npt).is(':visible')) {
  566. return;
  567. }
  568. end = (typeof end == 'number') ? end : begin;
  569. npt.scrollLeft = npt.scrollWidth;
  570. if (opts.insertMode == false && begin == end) end++; //set visualization for insert/overwrite mode
  571. if (npt.setSelectionRange) {
  572. npt.selectionStart = begin;
  573. npt.selectionEnd = end;
  574. } else if (npt.createTextRange) {
  575. range = npt.createTextRange();
  576. range.collapse(true);
  577. range.moveEnd('character', end);
  578. range.moveStart('character', begin);
  579. range.select();
  580. }
  581. } else {
  582. if (!$(input).is(':visible')) {
  583. return { "begin": 0, "end": 0 };
  584. }
  585. if (npt.setSelectionRange) {
  586. begin = npt.selectionStart;
  587. end = npt.selectionEnd;
  588. } else if (document.selection && document.selection.createRange) {
  589. range = document.selection.createRange();
  590. begin = 0 - range.duplicate().moveStart('character', -100000);
  591. end = begin + range.text.length;
  592. }
  593. begin = TranslatePosition(begin);
  594. end = TranslatePosition(end);
  595. return { "begin": begin, "end": end };
  596. }
  597. }
  598. function isComplete(buffer) { //return true / false / undefined (repeat *)
  599. if ($.isFunction(opts.isComplete)) return opts.isComplete.call($el, buffer, opts);
  600. if (opts.repeat == "*") return undefined;
  601. var complete = false,
  602. aml = seekPrevious(getMaskLength());
  603. if (getLastValidPosition() == aml) {
  604. complete = true;
  605. for (var i = 0; i <= aml; i++) {
  606. var mask = isMask(i);
  607. if ((mask && (buffer[i] == undefined || buffer[i] == getPlaceholder(i))) || (!mask && buffer[i] != getPlaceholder(i))) {
  608. complete = false;
  609. break;
  610. }
  611. }
  612. }
  613. return complete;
  614. }
  615. function isSelection(begin, end) {
  616. return isRTL ? (begin - end) > 1 || ((begin - end) == 1 && opts.insertMode) :
  617. (end - begin) > 1 || ((end - begin) == 1 && opts.insertMode);
  618. }
  619. function installEventRuler(npt) {
  620. var events = $._data(npt).events;
  621. $.each(events, function (eventType, eventHandlers) {
  622. $.each(eventHandlers, function (ndx, eventHandler) {
  623. if (eventHandler.namespace == "inputmask") {
  624. if (eventHandler.type != "setvalue") {
  625. var handler = eventHandler.handler;
  626. eventHandler.handler = function (e) {
  627. if (this.readOnly || this.disabled)
  628. e.preventDefault;
  629. else
  630. return handler.apply(this, arguments);
  631. };
  632. }
  633. }
  634. });
  635. });
  636. }
  637. function patchValueProperty(npt) {
  638. function PatchValhook(type) {
  639. if ($.valHooks[type] == undefined || $.valHooks[type].inputmaskpatch != true) {
  640. var valueGet = $.valHooks[type] && $.valHooks[type].get ? $.valHooks[type].get : function (elem) { return elem.value; };
  641. var valueSet = $.valHooks[type] && $.valHooks[type].set ? $.valHooks[type].set : function (elem, value) {
  642. elem.value = value;
  643. return elem;
  644. };
  645. $.valHooks[type] = {
  646. get: function (elem) {
  647. var $elem = $(elem);
  648. if ($elem.data('_inputmask')) {
  649. if ($elem.data('_inputmask')['opts'].autoUnmask)
  650. return $elem.inputmask('unmaskedvalue');
  651. else {
  652. var result = valueGet(elem),
  653. inputData = $elem.data('_inputmask'), maskset = inputData['maskset'],
  654. bufferTemplate = maskset['_buffer'];
  655. bufferTemplate = bufferTemplate ? bufferTemplate.join('') : '';
  656. return result != bufferTemplate ? result : '';
  657. }
  658. } else return valueGet(elem);
  659. },
  660. set: function (elem, value) {
  661. var $elem = $(elem);
  662. var result = valueSet(elem, value);
  663. if ($elem.data('_inputmask')) $elem.triggerHandler('setvalue.inputmask');
  664. return result;
  665. },
  666. inputmaskpatch: true
  667. };
  668. }
  669. }
  670. var valueProperty;
  671. if (Object.getOwnPropertyDescriptor)
  672. valueProperty = Object.getOwnPropertyDescriptor(npt, "value");
  673. if (valueProperty && valueProperty.get) {
  674. if (!npt._valueGet) {
  675. var valueGet = valueProperty.get;
  676. var valueSet = valueProperty.set;
  677. npt._valueGet = function () {
  678. return isRTL ? valueGet.call(this).split('').reverse().join('') : valueGet.call(this);
  679. };
  680. npt._valueSet = function (value) {
  681. valueSet.call(this, isRTL ? value.split('').reverse().join('') : value);
  682. };
  683. Object.defineProperty(npt, "value", {
  684. get: function () {
  685. var $self = $(this), inputData = $(this).data('_inputmask'), maskset = inputData['maskset'];
  686. return inputData && inputData['opts'].autoUnmask ? $self.inputmask('unmaskedvalue') : valueGet.call(this) != maskset['_buffer'].join('') ? valueGet.call(this) : '';
  687. },
  688. set: function (value) {
  689. valueSet.call(this, value);
  690. $(this).triggerHandler('setvalue.inputmask');
  691. }
  692. });
  693. }
  694. } else if (document.__lookupGetter__ && npt.__lookupGetter__("value")) {
  695. if (!npt._valueGet) {
  696. var valueGet = npt.__lookupGetter__("value");
  697. var valueSet = npt.__lookupSetter__("value");
  698. npt._valueGet = function () {
  699. return isRTL ? valueGet.call(this).split('').reverse().join('') : valueGet.call(this);
  700. };
  701. npt._valueSet = function (value) {
  702. valueSet.call(this, isRTL ? value.split('').reverse().join('') : value);
  703. };
  704. npt.__defineGetter__("value", function () {
  705. var $self = $(this), inputData = $(this).data('_inputmask'), maskset = inputData['maskset'];
  706. return inputData && inputData['opts'].autoUnmask ? $self.inputmask('unmaskedvalue') : valueGet.call(this) != maskset['_buffer'].join('') ? valueGet.call(this) : '';
  707. });
  708. npt.__defineSetter__("value", function (value) {
  709. valueSet.call(this, value);
  710. $(this).triggerHandler('setvalue.inputmask');
  711. });
  712. }
  713. } else {
  714. if (!npt._valueGet) {
  715. npt._valueGet = function () { return isRTL ? this.value.split('').reverse().join('') : this.value; };
  716. npt._valueSet = function (value) { this.value = isRTL ? value.split('').reverse().join('') : value; };
  717. }
  718. PatchValhook(npt.type);
  719. }
  720. }
  721. //shift chars to left from start to end and put c at end position if defined
  722. function shiftL(start, end, c, maskJumps) {
  723. var buffer = getBuffer();
  724. if (maskJumps !== false) //jumping over nonmask position
  725. while (!isMask(start) && start - 1 >= 0) start--;
  726. for (var i = start; i < end && i < getMaskLength() ; i++) {
  727. if (isMask(i)) {
  728. setPlaceholder(i);
  729. var j = seekNext(i);
  730. var p = getBufferElement(buffer, j);
  731. if (p != getPlaceholder(j)) {
  732. if (j < getMaskLength() && isValid(i, p, true) !== false && getTest(i).def == getTest(j).def) {
  733. setBufferElement(buffer, i, p);
  734. if (j < end) {
  735. setPlaceholder(j); //cleanup next position
  736. }
  737. } else {
  738. if (isMask(i))
  739. break;
  740. }
  741. }
  742. } else {
  743. setPlaceholder(i);
  744. }
  745. }
  746. if (c != undefined)
  747. setBufferElement(buffer, seekPrevious(end), c);
  748. if (opts.greedy == false) {
  749. var trbuffer = truncateInput(buffer.join('')).split('');
  750. buffer.length = trbuffer.length;
  751. for (var i = 0, bl = buffer.length; i < bl; i++) {
  752. buffer[i] = trbuffer[i];
  753. }
  754. if (buffer.length == 0) getMaskSet()["buffer"] = getBufferTemplate().slice();
  755. }
  756. return start; //return the used start position
  757. }
  758. function shiftR(start, end, c) {
  759. var buffer = getBuffer();
  760. if (getBufferElement(buffer, start) != getPlaceholder(start)) {
  761. for (var i = seekPrevious(end) ; i > start && i >= 0; i--) {
  762. if (isMask(i)) {
  763. var j = seekPrevious(i);
  764. var t = getBufferElement(buffer, j);
  765. if (t != getPlaceholder(j)) {
  766. if (isValid(i, t, true) !== false && getTest(i).def == getTest(j).def) {
  767. setBufferElement(buffer, i, t);
  768. setPlaceholder(j);
  769. } //else break;
  770. }
  771. } else
  772. setPlaceholder(i);
  773. }
  774. }
  775. if (c != undefined && getBufferElement(buffer, start) == getPlaceholder(start))
  776. setBufferElement(buffer, start, c);
  777. var lengthBefore = buffer.length;
  778. if (opts.greedy == false) {
  779. var trbuffer = truncateInput(buffer.join('')).split('');
  780. buffer.length = trbuffer.length;
  781. for (var i = 0, bl = buffer.length; i < bl; i++) {
  782. buffer[i] = trbuffer[i];
  783. }
  784. if (buffer.length == 0) getMaskSet()["buffer"] = getBufferTemplate().slice();
  785. }
  786. return end - (lengthBefore - buffer.length); //return new start position
  787. }
  788. function HandleRemove(input, k, pos) {
  789. if (opts.numericInput || isRTL) {
  790. switch (k) {
  791. case opts.keyCode.BACKSPACE:
  792. k = opts.keyCode.DELETE;
  793. break;
  794. case opts.keyCode.DELETE:
  795. k = opts.keyCode.BACKSPACE;
  796. break;
  797. }
  798. if (isRTL) {
  799. var pend = pos.end;
  800. pos.end = pos.begin;
  801. pos.begin = pend;
  802. }
  803. }
  804. var isSelection = true;
  805. if (pos.begin == pos.end) {
  806. var posBegin = k == opts.keyCode.BACKSPACE ? pos.begin - 1 : pos.begin;
  807. if (opts.isNumeric && opts.radixPoint != "" && getBuffer()[posBegin] == opts.radixPoint) {
  808. pos.begin = (getBuffer().length - 1 == posBegin) /* radixPoint is latest? delete it */ ? pos.begin : k == opts.keyCode.BACKSPACE ? posBegin : seekNext(posBegin);
  809. pos.end = pos.begin;
  810. }
  811. isSelection = false;
  812. if (k == opts.keyCode.BACKSPACE)
  813. pos.begin--;
  814. else if (k == opts.keyCode.DELETE)
  815. pos.end++;
  816. } else if (pos.end - pos.begin == 1 && !opts.insertMode) {
  817. isSelection = false;
  818. if (k == opts.keyCode.BACKSPACE)
  819. pos.begin--;
  820. }
  821. clearBuffer(getBuffer(), pos.begin, pos.end);
  822. var ml = getMaskLength();
  823. if (opts.greedy == false && (isNaN(opts.repeat) || opts.repeat > 0)) {
  824. shiftL(pos.begin, ml, undefined, !isRTL && (k == opts.keyCode.BACKSPACE && !isSelection));
  825. } else {
  826. var newpos = pos.begin;
  827. for (var i = pos.begin; i < pos.end; i++) { //seeknext to skip placeholders at start in selection
  828. if (isMask(i) || !isSelection)
  829. newpos = shiftL(pos.begin, ml, undefined, !isRTL && (k == opts.keyCode.BACKSPACE && !isSelection));
  830. }
  831. if (!isSelection) pos.begin = newpos;
  832. }
  833. var firstMaskPos = seekNext(-1);
  834. clearBuffer(getBuffer(), pos.begin, pos.end, true);
  835. checkVal(input, false, false, getBuffer());
  836. if (getLastValidPosition() < firstMaskPos) {
  837. getMaskSet()["p"] = firstMaskPos;
  838. } else {
  839. getMaskSet()["p"] = pos.begin;
  840. }
  841. }
  842. function keydownEvent(e) {
  843. //Safari 5.1.x - modal dialog fires keypress twice workaround
  844. skipKeyPressEvent = false;
  845. var input = this, $input = $(input), k = e.keyCode, pos = caret(input);
  846. //backspace, delete, and escape get special treatment
  847. if (k == opts.keyCode.BACKSPACE || k == opts.keyCode.DELETE || (iphone && k == 127) || e.ctrlKey && k == 88) { //backspace/delete
  848. e.preventDefault(); //stop default action but allow propagation
  849. if (k == 88) valueOnFocus = getBuffer().join('');
  850. HandleRemove(input, k, pos);
  851. writeBuffer(input, getBuffer(), getMaskSet()["p"]);
  852. if (input._valueGet() == getBufferTemplate().join(''))
  853. $input.trigger('cleared');
  854. if (opts.showTooltip) { //update tooltip
  855. $input.prop("title", getMaskSet()["mask"]);
  856. }
  857. } else if (k == opts.keyCode.END || k == opts.keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch
  858. setTimeout(function () {
  859. var caretPos = seekNext(getLastValidPosition());
  860. if (!opts.insertMode && caretPos == getMaskLength() && !e.shiftKey) caretPos--;
  861. caret(input, e.shiftKey ? pos.begin : caretPos, caretPos);
  862. }, 0);
  863. } else if ((k == opts.keyCode.HOME && !e.shiftKey) || k == opts.keyCode.PAGE_UP) { //Home or page_up
  864. caret(input, 0, e.shiftKey ? pos.begin : 0);
  865. } else if (k == opts.keyCode.ESCAPE || (k == 90 && e.ctrlKey)) { //escape && undo
  866. checkVal(input, true, false, valueOnFocus.split(''));
  867. $input.click();
  868. } else if (k == opts.keyCode.INSERT && !(e.shiftKey || e.ctrlKey)) { //insert
  869. opts.insertMode = !opts.insertMode;
  870. caret(input, !opts.insertMode && pos.begin == getMaskLength() ? pos.begin - 1 : pos.begin);
  871. } else if (opts.insertMode == false && !e.shiftKey) {
  872. if (k == opts.keyCode.RIGHT) {
  873. setTimeout(function () {
  874. var caretPos = caret(input);
  875. caret(input, caretPos.begin);
  876. }, 0);
  877. } else if (k == opts.keyCode.LEFT) {
  878. setTimeout(function () {
  879. var caretPos = caret(input);
  880. caret(input, caretPos.begin - 1);
  881. }, 0);
  882. }
  883. }
  884. var currentCaretPos = caret(input);
  885. if (opts.onKeyDown.call(this, e, getBuffer(), opts) === true) //extra stuff to execute on keydown
  886. caret(input, currentCaretPos.begin, currentCaretPos.end);
  887. ignorable = $.inArray(k, opts.ignorables) != -1;
  888. }
  889. function keypressEvent(e, checkval, k, writeOut, strict, ndx) {
  890. //Safari 5.1.x - modal dialog fires keypress twice workaround
  891. if (k == undefined && skipKeyPressEvent) return false;
  892. skipKeyPressEvent = true;
  893. var input = this, $input = $(input);
  894. e = e || window.event;
  895. var k = checkval ? k : (e.which || e.charCode || e.keyCode);
  896. if (checkval !== true && (!(e.ctrlKey && e.altKey) && (e.ctrlKey || e.metaKey || ignorable))) {
  897. return true;
  898. } else {
  899. if (k) {
  900. //special treat the decimal separator
  901. if (checkval !== true && k == 46 && e.shiftKey == false && opts.radixPoint == ",") k = 44;
  902. var pos, forwardPosition, c = String.fromCharCode(k);
  903. if (checkval) {
  904. var pcaret = strict ? ndx : getLastValidPosition() + 1;
  905. pos = { begin: pcaret, end: pcaret };
  906. } else {
  907. pos = caret(input);
  908. }
  909. //should we clear a possible selection??
  910. var isSlctn = isSelection(pos.begin, pos.end);
  911. if (isSlctn) {
  912. getMaskSet()["undoBuffer"] = getBuffer().join(''); //init undobuffer for recovery when not valid
  913. HandleRemove(input, opts.keyCode.DELETE, pos);
  914. if (!opts.insertMode) { //preserve some space
  915. shiftR(pos.begin, getMaskLength());
  916. }
  917. }
  918. var radixPosition = getBuffer().join('').indexOf(opts.radixPoint);
  919. if (opts.isNumeric && checkval !== true && radixPosition != -1) {
  920. if (opts.greedy && pos.begin <= radixPosition) {
  921. pos.begin = seekPrevious(pos.begin);
  922. pos.end = pos.begin;
  923. } else if (c == opts.radixPoint) {
  924. pos.begin = radixPosition;
  925. pos.end = pos.begin;
  926. }
  927. }
  928. getMaskSet()["writeOutBuffer"] = true;
  929. var p = pos.begin;
  930. var valResult = isValid(p, c, strict);
  931. if (valResult !== false) {
  932. var refresh = false, buffer = getBuffer();
  933. if (valResult !== true) {
  934. refresh = valResult["refresh"]; //only rewrite buffer from isValid
  935. p = valResult.pos != undefined ? valResult.pos : p; //set new position from isValid
  936. c = valResult.c != undefined ? valResult.c : c; //set new char from isValid
  937. }
  938. if (refresh !== true) {
  939. if (opts.insertMode == true) {
  940. var freePos = p;
  941. if (getBufferElement(buffer, p) != getPlaceholder(p)) {
  942. while (getMaskSet()["validPositions"][freePos]) {
  943. freePos = seekNext(freePos);
  944. }
  945. }
  946. if (freePos < getMaskLength()) {
  947. shiftR(p, getMaskLength(), c);
  948. } else getMaskSet()["writeOutBuffer"] = false;
  949. } else setBufferElement(buffer, p, c);
  950. }
  951. forwardPosition = seekNext(p);
  952. getMaskSet()["p"] = forwardPosition; //needed for checkval
  953. }
  954. if (writeOut !== false) {
  955. var self = this;
  956. setTimeout(function () { opts.onKeyValidation.call(self, valResult, opts); }, 0);
  957. if (getMaskSet()["writeOutBuffer"] && valResult !== false) {
  958. var buffer = getBuffer();
  959. var newCaretPosition;
  960. if (checkval) {
  961. newCaretPosition = undefined;
  962. } else if (opts.numericInput) {
  963. if (p > radixPosition) {
  964. newCaretPosition = seekPrevious(forwardPosition);
  965. } else if (c == opts.radixPoint) {
  966. newCaretPosition = forwardPosition - 1;
  967. } else newCaretPosition = seekPrevious(forwardPosition - 1);
  968. } else {
  969. newCaretPosition = forwardPosition;
  970. }
  971. writeBuffer(input, buffer, newCaretPosition);
  972. if (checkval !== true) {
  973. setTimeout(function () { //timeout needed for IE
  974. if (isComplete(buffer) === true)
  975. $input.trigger("complete");
  976. skipInputEvent = true;
  977. $input.trigger("input");
  978. }, 0);
  979. }
  980. } else if (isSlctn) { //TODO FIXME
  981. getMaskSet()["buffer"] = getMaskSet()["undoBuffer"].split('');
  982. }
  983. } else if (isSlctn) { //TODO FIXME
  984. getMaskSet()["buffer"] = getMaskSet()["undoBuffer"].split('');
  985. }
  986. if (opts.showTooltip) { //update tooltip
  987. $input.prop("title", getMaskSet()["mask"]);
  988. }
  989. //needed for IE8 and below
  990. if (e) e.preventDefault ? e.preventDefault() : e.returnValue = false;
  991. }
  992. }
  993. }
  994. function keyupEvent(e) {
  995. var $input = $(this), input = this, k = e.keyCode, buffer = getBuffer();
  996. opts.onKeyUp.call(this, e, buffer, opts); //extra stuff to execute on keyup
  997. if (k == opts.keyCode.TAB && opts.showMaskOnFocus) {
  998. if ($input.hasClass('focus.inputmask') && input._valueGet().length == 0) {
  999. buffer = getBufferTemplate().slice();
  1000. writeBuffer(input, buffer);
  1001. caret(input, 0);
  1002. valueOnFocus = getBuffer().join('');
  1003. } else {
  1004. writeBuffer(input, buffer);
  1005. if (buffer.join('') == getBufferTemplate().join('') && $.inArray(opts.radixPoint, buffer) != -1) {
  1006. caret(input, TranslatePosition(0));
  1007. $input.click();
  1008. } else
  1009. caret(input, TranslatePosition(0), TranslatePosition(getMaskLength()));
  1010. }
  1011. }
  1012. }
  1013. function pasteEvent(e) {
  1014. if (skipInputEvent === true && e.type == "input") {
  1015. skipInputEvent = false;
  1016. return true;
  1017. }
  1018. var input = this, $input = $(input);
  1019. //paste event for IE8 and lower I guess ;-)
  1020. if (e.type == "propertychange" && input._valueGet().length <= getMaskLength()) {
  1021. return true;
  1022. }
  1023. setTimeout(function () {
  1024. var pasteValue = $.isFunction(opts.onBeforePaste) ? opts.onBeforePaste.call(input, input._valueGet(), opts) : input._valueGet();
  1025. checkVal(input, false, false, pasteValue.split(''), true);
  1026. writeBuffer(input, getBuffer());
  1027. if (isComplete(getBuffer()) === true)
  1028. $input.trigger("complete");
  1029. $input.click();
  1030. }, 0);
  1031. }
  1032. //not used - attempt to support android
  1033. function mobileInputEvent(e) {
  1034. var input = this, $input = $(input);
  1035. //backspace in chrome32 only fires input event - detect & treat
  1036. var caretPos = caret(input),
  1037. currentValue = input._valueGet();
  1038. currentValue = currentValue.replace(new RegExp("(" + escapeRegex(getBufferTemplate().join('')) + ")*"), "");
  1039. //correct caretposition for chrome
  1040. if (caretPos.begin > currentValue.length) {
  1041. caret(input, currentValue.length);
  1042. caretPos = caret(input);
  1043. }
  1044. if ((getBuffer().length - currentValue.length) == 1 && currentValue.charAt(caretPos.begin) != getBuffer()[caretPos.begin]
  1045. && currentValue.charAt(caretPos.begin + 1) != getBuffer()[caretPos.begin]
  1046. && !isMask(caretPos.begin)) {
  1047. e.keyCode = opts.keyCode.BACKSPACE;
  1048. keydownEvent.call(input, e);
  1049. } else { //nonnumerics don't fire keypress
  1050. checkVal(input, false, false, currentValue.split(''));
  1051. writeBuffer(input, getBuffer());
  1052. if (isComplete(getBuffer()) === true)
  1053. $input.trigger("complete");
  1054. $input.click();
  1055. }
  1056. e.preventDefault();
  1057. }
  1058. function mask(el) {
  1059. $el = $(el);
  1060. if ($el.is(":input")) {
  1061. //store tests & original buffer in the input element - used to get the unmasked value
  1062. $el.data('_inputmask', {
  1063. 'maskset': maskset,
  1064. 'opts': opts,
  1065. 'isRTL': false
  1066. });
  1067. //show tooltip
  1068. if (opts.showTooltip) {
  1069. $el.prop("title", getMaskSet()["mask"]);
  1070. }
  1071. //correct greedy setting if needed
  1072. opts.greedy = opts.greedy ? opts.greedy : opts.repeat == 0;
  1073. patchValueProperty(el);
  1074. if (opts.numericInput) opts.isNumeric = opts.numericInput;
  1075. if (el.dir == "rtl" || (opts.numericInput && opts.rightAlignNumerics) || (opts.isNumeric && opts.rightAlignNumerics))
  1076. $el.css("text-align", "right");
  1077. if (el.dir == "rtl" || opts.numericInput) {
  1078. el.dir = "ltr";
  1079. $el.removeAttr("dir");
  1080. var inputData = $el.data('_inputmask');
  1081. inputData['isRTL'] = true;
  1082. $el.data('_inputmask', inputData);
  1083. isRTL = true;
  1084. }
  1085. //unbind all events - to make sure that no other mask will interfere when re-masking
  1086. $el.unbind(".inputmask");
  1087. $el.removeClass('focus.inputmask');
  1088. //bind events
  1089. $el.closest('form').bind("submit", function () { //trigger change on submit if any
  1090. if (valueOnFocus != getBuffer().join('')) {
  1091. $el.change();
  1092. }
  1093. }).bind('reset', function () {
  1094. setTimeout(function () {
  1095. $el.trigger("setvalue");
  1096. }, 0);
  1097. });
  1098. $el.bind("mouseenter.inputmask", function () {
  1099. var $input = $(this), input = this;
  1100. if (!$input.hasClass('focus.inputmask') && opts.showMaskOnHover) {
  1101. if (input._valueGet() != getBuffer().join('')) {
  1102. writeBuffer(input, getBuffer());
  1103. }
  1104. }
  1105. }).bind("blur.inputmask", function () {
  1106. var $input = $(this), input = this, nptValue = input._valueGet(), buffer = getBuffer();
  1107. $input.removeClass('focus.inputmask');
  1108. if (valueOnFocus != getBuffer().join('')) {
  1109. $input.change();
  1110. }
  1111. if (opts.clearMaskOnLostFocus && nptValue != '') {
  1112. if (nptValue == getBufferTemplate().join(''))
  1113. input._valueSet('');
  1114. else { //clearout optional tail of the mask
  1115. clearOptionalTail(input);
  1116. }
  1117. }
  1118. if (isComplete(buffer) === false) {
  1119. $input.trigger("incomplete");
  1120. if (opts.clearIncomplete) {
  1121. resetMaskSet();
  1122. if (opts.clearMaskOnLostFocus)
  1123. input._valueSet('');
  1124. else {
  1125. buffer = getBufferTemplate().slice();
  1126. writeBuffer(input, buffer);
  1127. }
  1128. }
  1129. }
  1130. }).bind("focus.inputmask", function () {
  1131. var $input = $(this), input = this, nptValue = input._valueGet();
  1132. if (opts.showMaskOnFocus && !$input.hasClass('focus.inputmask') && (!opts.showMaskOnHover || (opts.showMaskOnHover && nptValue == ''))) {
  1133. if (input._valueGet() != getBuffer().join('')) {
  1134. writeBuffer(input, getBuffer(), seekNext(getLastValidPosition()));
  1135. }
  1136. }
  1137. $input.addClass('focus.inputmask');
  1138. valueOnFocus = getBuffer().join('');
  1139. }).bind("mouseleave.inputmask", function () {
  1140. var $input = $(this), input = this;
  1141. if (opts.clearMaskOnLostFocus) {
  1142. if (!$input.hasClass('focus.inputmask') && input._valueGet() != $input.attr("placeholder")) {
  1143. if (input._valueGet() == getBufferTemplate().join('') || input._valueGet() == '')
  1144. input._valueSet('');
  1145. else { //clearout optional tail of the mask
  1146. clearOptionalTail(input);
  1147. }
  1148. }
  1149. }
  1150. }).bind("click.inputmask", function () {
  1151. var input = this;
  1152. setTimeout(function () {
  1153. var selectedCaret = caret(input), buffer = getBuffer();
  1154. if (selectedCaret.begin == selectedCaret.end) {
  1155. var clickPosition = isRTL ? TranslatePosition(selectedCaret.begin) : selectedCaret.begin,
  1156. lvp = getLastValidPosition(undefined, clickPosition),
  1157. lastPosition;
  1158. if (opts.isNumeric) {
  1159. lastPosition = opts.skipRadixDance === false && opts.radixPoint != "" && $.inArray(opts.radixPoint, buffer) != -1 ?
  1160. (opts.numericInput ? seekNext($.inArray(opts.radixPoint, buffer)) : $.inArray(opts.radixPoint, buffer)) :
  1161. seekNext(lvp);
  1162. } else {
  1163. lastPosition = seekNext(lvp);
  1164. }
  1165. if (clickPosition < lastPosition) {
  1166. if (isMask(clickPosition))
  1167. caret(input, clickPosition);
  1168. else caret(input, seekNext(clickPosition));
  1169. } else
  1170. caret(input, lastPosition);
  1171. }
  1172. }, 0);
  1173. }).bind('dblclick.inputmask', function () {
  1174. var input = this;
  1175. setTimeout(function () {
  1176. caret(input, 0, seekNext(getLastValidPosition()));
  1177. }, 0);
  1178. }).bind(PasteEventType + ".inputmask dragdrop.inputmask drop.inputmask", pasteEvent
  1179. ).bind('setvalue.inputmask', function () {
  1180. var input = this;
  1181. checkVal(input, true);
  1182. valueOnFocus = getBuffer().join('');
  1183. if (input._valueGet() == getBufferTemplate().join(''))
  1184. input._valueSet('');
  1185. }).bind('complete.inputmask', opts.oncomplete
  1186. ).bind('incomplete.inputmask', opts.onincomplete
  1187. ).bind('cleared.inputmask', opts.oncleared);
  1188. $el.bind("keydown.inputmask", keydownEvent
  1189. ).bind("keypress.inputmask", keypressEvent
  1190. ).bind("keyup.inputmask", keyupEvent);
  1191. // as the other inputevents aren't reliable for the moment we only base on the input event
  1192. // needs follow-up
  1193. if (android || androidfirefox || androidchrome || kindle) {
  1194. $el.attr("autocomplete", "off")
  1195. .attr("autocorrect", "off")
  1196. .attr("autocapitalize", "off")
  1197. .attr("spellcheck", false);
  1198. if (androidfirefox || kindle) {
  1199. $el.unbind("keydown.inputmask", keydownEvent
  1200. ).unbind("keypress.inputmask", keypressEvent
  1201. ).unbind("keyup.inputmask", keyupEvent);
  1202. if (PasteEventType == "input") {
  1203. $el.unbind(PasteEventType + ".inputmask");
  1204. }
  1205. $el.bind("input.inputmask", mobileInputEvent);
  1206. }
  1207. }
  1208. if (msie1x)
  1209. $el.bind("input.inputmask", pasteEvent);
  1210. //apply mask
  1211. var initialValue = $.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call(el, el._valueGet(), opts) : el._valueGet();
  1212. checkVal(el, true, false, initialValue.split(''), true);
  1213. valueOnFocus = getBuffer().join('');
  1214. // Wrap document.activeElement in a try/catch block since IE9 throw "Unspecified error" if document.activeElement is undefined when we are in an IFrame.
  1215. var activeElement;
  1216. try {
  1217. activeElement = document.activeElement;
  1218. } catch (e) {
  1219. }
  1220. if (activeElement === el) { //position the caret when in focus
  1221. $el.addClass('focus.inputmask');
  1222. caret(el, seekNext(getLastValidPosition()));
  1223. } else if (opts.clearMaskOnLostFocus) {
  1224. if (getBuffer().join('') == getBufferTemplate().join('')) {
  1225. el._valueSet('');
  1226. } else {
  1227. clearOptionalTail(el);
  1228. }
  1229. } else {
  1230. writeBuffer(el, getBuffer());
  1231. }
  1232. installEventRuler(el);
  1233. }
  1234. }
  1235. //action object
  1236. if (actionObj != undefined) {
  1237. switch (actionObj["action"]) {
  1238. case "isComplete":
  1239. return isComplete(actionObj["buffer"]);
  1240. case "unmaskedvalue":
  1241. isRTL = actionObj["$input"].data('_inputmask')['isRTL'];
  1242. return unmaskedvalue(actionObj["$input"], actionObj["skipDatepickerCheck"]);
  1243. case "mask":
  1244. mask(actionObj["el"]);
  1245. break;
  1246. case "format":
  1247. $el = $({});
  1248. $el.data('_inputmask', {
  1249. 'maskset': maskset,
  1250. 'opts': opts,
  1251. 'isRTL': opts.numericInput
  1252. });
  1253. if (opts.numericInput) {
  1254. opts.isNumeric = opts.numericInput;
  1255. isRTL = true;
  1256. }
  1257. checkVal($el, false, false, actionObj["value"].split(''), true);
  1258. return getBuffer().join('');
  1259. case "isValid":
  1260. $el = $({});
  1261. $el.data('_inputmask', {
  1262. 'maskset': maskset,
  1263. 'opts': opts,
  1264. 'isRTL': opts.numericInput
  1265. });
  1266. if (opts.numericInput) {
  1267. opts.isNumeric = opts.numericInput;
  1268. isRTL = true;
  1269. }
  1270. checkVal($el, false, true, actionObj["value"].split(''));
  1271. return isComplete(getBuffer());
  1272. }
  1273. }
  1274. };
  1275. $.inputmask = {
  1276. //options default
  1277. defaults: {
  1278. placeholder: "_",
  1279. optionalmarker: { start: "[", end: "]" },
  1280. quantifiermarker: { start: "{", end: "}" },
  1281. groupmarker: { start: "(", end: ")" },
  1282. escapeChar: "\\",
  1283. mask: null,
  1284. oncomplete: $.noop, //executes when the mask is complete
  1285. onincomplete: $.noop, //executes when the mask is incomplete and focus is lost
  1286. oncleared: $.noop, //executes when the mask is cleared
  1287. repeat: 0, //repetitions of the mask: * ~ forever, otherwise specify an integer
  1288. greedy: true, //true: allocated buffer for the mask and repetitions - false: allocate only if needed
  1289. autoUnmask: false, //automatically unmask when retrieving the value with $.fn.val or value if the browser supports __lookupGetter__ or getOwnPropertyDescriptor
  1290. clearMaskOnLostFocus: true,
  1291. insertMode: true, //insert the input or overwrite the input
  1292. clearIncomplete: false, //clear the incomplete input on blur
  1293. aliases: {}, //aliases definitions => see jquery.inputmask.extensions.js
  1294. onKeyUp: $.noop, //override to implement autocomplete on certain keys for example
  1295. onKeyDown: $.noop, //override to implement autocomplete on certain keys for example
  1296. onBeforeMask: undefined, //executes before masking the initial value to allow preprocessing of the initial value. args => initialValue, opts => return processedValue
  1297. onBeforePaste: undefined, //executes before masking the pasted value to allow preprocessing of the pasted value. args => pastedValue, opts => return processedValue
  1298. onUnMask: undefined, //executes after unmasking to allow postprocessing of the unmaskedvalue. args => maskedValue, unmaskedValue, opts
  1299. showMaskOnFocus: true, //show the mask-placeholder when the input has focus
  1300. showMaskOnHover: true, //show the mask-placeholder when hovering the empty input
  1301. onKeyValidation: $.noop, //executes on every key-press with the result of isValid. Params: result, opts
  1302. skipOptionalPartCharacter: " ", //a character which can be used to skip an optional part of a mask
  1303. showTooltip: false, //show the activemask as tooltip
  1304. numericInput: false, //numericInput input direction style (input shifts to the left while holding the caret position)
  1305. //numeric basic properties
  1306. isNumeric: false, //enable numeric features
  1307. radixPoint: "", //".", // | ","
  1308. skipRadixDance: false, //disable radixpoint caret positioning
  1309. rightAlignNumerics: true, //align numerics to the right
  1310. //numeric basic properties
  1311. definitions: {
  1312. '9': {
  1313. validator: "[0-9]",
  1314. cardinality: 1,
  1315. definitionSymbol: "*"
  1316. },
  1317. 'a': {
  1318. validator: "[A-Za-z\u0410-\u044F\u0401\u0451]",
  1319. cardinality: 1,
  1320. definitionSymbol: "*"
  1321. },
  1322. '*': {
  1323. validator: "[A-Za-z\u0410-\u044F\u0401\u04510-9]",
  1324. cardinality: 1
  1325. }
  1326. },
  1327. keyCode: {
  1328. ALT: 18, BACKSPACE: 8, CAPS_LOCK: 20, COMMA: 188, COMMAND: 91, COMMAND_LEFT: 91, COMMAND_RIGHT: 93, CONTROL: 17, DELETE: 46, DOWN: 40, END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, INSERT: 45, LEFT: 37, MENU: 93, NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108,
  1329. NUMPAD_MULTIPLY: 106, NUMPAD_SUBTRACT: 109, PAGE_DOWN: 34, PAGE_UP: 33, PERIOD: 190, RIGHT: 39, SHIFT: 16, SPACE: 32, TAB: 9, UP: 38, WINDOWS: 91
  1330. },
  1331. //specify keycodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF
  1332. ignorables: [8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123],
  1333. isComplete: undefined //override for isComplete - args => buffer, opts - return true || false
  1334. },
  1335. escapeRegex: function (str) {
  1336. var specials = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'];
  1337. return str.replace(new RegExp('(\\' + specials.join('|\\') + ')', 'gim'), '\\$1');
  1338. },
  1339. format: function (value, options) {
  1340. var opts = $.extend(true, {}, $.inputmask.defaults, options);
  1341. resolveAlias(opts.alias, options, opts);
  1342. return maskScope(generateMaskSet(opts), opts, { "action": "format", "value": value });
  1343. },
  1344. isValid: function (value, options) {
  1345. var opts = $.extend(true, {}, $.inputmask.defaults, options);
  1346. resolveAlias(opts.alias, options, opts);
  1347. return maskScope(generateMaskSet(opts), opts, { "action": "isValid", "value": value });
  1348. }
  1349. };
  1350. $.fn.inputmask = function (fn, options) {
  1351. var opts = $.extend(true, {}, $.inputmask.defaults, options),
  1352. maskset;
  1353. if (typeof fn === "string") {
  1354. switch (fn) {
  1355. case "mask":
  1356. //resolve possible aliases given by options
  1357. resolveAlias(opts.alias, options, opts);
  1358. maskset = generateMaskSet(opts);
  1359. if (maskset.length == 0) { return this; }
  1360. return this.each(function () {
  1361. maskScope($.extend(true, {}, maskset), 0, opts, { "action": "mask", "el": this });
  1362. });
  1363. case "unmaskedvalue":
  1364. var $input = $(this), input = this;
  1365. if ($input.data('_inputmask')) {
  1366. maskset = $input.data('_inputmask')['maskset'];
  1367. opts = $input.data('_inputmask')['opts'];
  1368. return maskScope(maskset, opts, { "action": "unmaskedvalue", "$input": $input });
  1369. } else return $input.val();
  1370. case "remove":
  1371. return this.each(function () {
  1372. var $input = $(this), input = this;
  1373. if ($input.data('_inputmask')) {
  1374. maskset = $input.data('_inputmask')['maskset'];
  1375. opts = $input.data('_inputmask')['opts'];
  1376. //writeout the unmaskedvalue
  1377. input._valueSet(maskScope(maskset, opts, { "action": "unmaskedvalue", "$input": $input, "skipDatepickerCheck": true }));
  1378. //clear data
  1379. $input.removeData('_inputmask');
  1380. //unbind all events
  1381. $input.unbind(".inputmask");
  1382. $input.removeClass('focus.inputmask');
  1383. //restore the value property
  1384. var valueProperty;
  1385. if (Object.getOwnPropertyDescriptor)
  1386. valueProperty = Object.getOwnPropertyDescriptor(input, "value");
  1387. if (valueProperty && valueProperty.get) {
  1388. if (input._valueGet) {
  1389. Object.defineProperty(input, "value", {
  1390. get: input._valueGet,
  1391. set: input._valueSet
  1392. });
  1393. }
  1394. } else if (document.__lookupGetter__ && input.__lookupGetter__("value")) {
  1395. if (input._valueGet) {
  1396. input.__defineGetter__("value", input._valueGet);
  1397. input.__defineSetter__("value", input._valueSet);
  1398. }
  1399. }
  1400. try { //try catch needed for IE7 as it does not supports deleting fns
  1401. delete input._valueGet;
  1402. delete input._valueSet;
  1403. } catch (e) {
  1404. input._valueGet = undefined;
  1405. input._valueSet = undefined;
  1406. }
  1407. }
  1408. });
  1409. break;
  1410. case "getemptymask": //return the default (empty) mask value, usefull for setting the default value in validation
  1411. if (this.data('_inputmask')) {
  1412. maskset = this.data('_inputmask')['maskset'];
  1413. return maskset['_buffer'].join('');
  1414. }
  1415. else return "";
  1416. case "hasMaskedValue": //check wheter the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  1417. return this.data('_inputmask') ? !this.data('_inputmask')['opts'].autoUnmask : false;
  1418. case "isComplete":
  1419. maskset = this.data('_inputmask')['maskset'];
  1420. opts = this.data('_inputmask')['opts'];
  1421. return maskScope(maskset, opts, { "action": "isComplete", "buffer": this[0]._valueGet().split('') });
  1422. case "getmetadata": //return mask metadata if exists
  1423. if (this.data('_inputmask')) {
  1424. maskset = this.data('_inputmask')['maskset'];
  1425. return maskset['metadata'];
  1426. }
  1427. else return undefined;
  1428. default:
  1429. //check if the fn is an alias
  1430. if (!resolveAlias(fn, options, opts)) {
  1431. //maybe fn is a mask so we try
  1432. //set mask
  1433. opts.mask = fn;
  1434. }
  1435. maskset = generateMaskSet(opts);
  1436. if (maskset == undefined) { return this; }
  1437. return this.each(function () {
  1438. maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this });
  1439. });
  1440. break;
  1441. }
  1442. } else if (typeof fn == "object") {
  1443. opts = $.extend(true, {}, $.inputmask.defaults, fn);
  1444. resolveAlias(opts.alias, fn, opts); //resolve aliases
  1445. maskset = generateMaskSet(opts);
  1446. if (maskset == undefined) { return this; }
  1447. return this.each(function () {
  1448. maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this });
  1449. });
  1450. } else if (fn == undefined) {
  1451. //look for data-inputmask atribute - the attribute should only contain optipns
  1452. return this.each(function () {
  1453. var attrOptions = $(this).attr("data-inputmask");
  1454. if (attrOptions && attrOptions != "") {
  1455. try {
  1456. attrOptions = attrOptions.replace(new RegExp("'", "g"), '"');
  1457. var dataoptions = $.parseJSON("{" + attrOptions + "}");
  1458. $.extend(true, dataoptions, options);
  1459. opts = $.extend(true, {}, $.inputmask.defaults, dataoptions);
  1460. resolveAlias(opts.alias, dataoptions, opts);
  1461. opts.alias = undefined;
  1462. $(this).inputmask(opts);
  1463. } catch (ex) { } //need a more relax parseJSON
  1464. }
  1465. });
  1466. }
  1467. };
  1468. }
  1469. })(jQuery);