jquery.inputmask.js 82 KB

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