jquery.inputmask.js 80 KB

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