inputmask.js 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  1. /*
  2. * Input Mask Core
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 0.0.0-dev
  7. */
  8. (function(factory) {
  9. if (typeof define === "function" && define.amd) {
  10. define(["dependencyLib"], factory);
  11. } else if (typeof exports === "object") {
  12. module.exports = factory(require("./dependencyLib"));
  13. } else {
  14. factory(window.dependencyLib || jQuery);
  15. }
  16. }
  17. (function($) {
  18. function Inputmask(options) {
  19. this.el = undefined;
  20. //init options
  21. this.opts = $.extend(true, {}, this.defaults, options);
  22. this.noMasksCache = options && options.definitions !== undefined;
  23. this.userOptions = options || {}; //user passed options
  24. resolveAlias(this.opts.alias, options, this.opts);
  25. }
  26. Inputmask.prototype = {
  27. //options default
  28. defaults: {
  29. placeholder: "_",
  30. optionalmarker: {
  31. start: "[",
  32. end: "]"
  33. },
  34. quantifiermarker: {
  35. start: "{",
  36. end: "}"
  37. },
  38. groupmarker: {
  39. start: "(",
  40. end: ")"
  41. },
  42. alternatormarker: "|",
  43. escapeChar: "\\",
  44. mask: null, //needs tobe null instead of undefined as the extend method does not consider props with the undefined value
  45. oncomplete: $.noop, //executes when the mask is complete
  46. onincomplete: $.noop, //executes when the mask is incomplete and focus is lost
  47. oncleared: $.noop, //executes when the mask is cleared
  48. repeat: 0, //repetitions of the mask: * ~ forever, otherwise specify an integer
  49. greedy: true, //true: allocated buffer for the mask and repetitions - false: allocate only if needed
  50. autoUnmask: false, //automatically unmask when retrieving the value with $.fn.val or value if the browser supports __lookupGetter__ or getOwnPropertyDescriptor
  51. removeMaskOnSubmit: false, //remove the mask before submitting the form.
  52. clearMaskOnLostFocus: true,
  53. insertMode: true, //insert the input or overwrite the input
  54. clearIncomplete: false, //clear the incomplete input on blur
  55. aliases: {}, //aliases definitions => see jquery.inputmask.extensions.js
  56. alias: null,
  57. onKeyDown: $.noop, //callback to implement autocomplete on certain keys for example. args => event, buffer, caretPos, opts
  58. onBeforeMask: null, //executes before masking the initial value to allow preprocessing of the initial value. args => initialValue, opts => return processedValue
  59. onBeforePaste: null, //executes before masking the pasted value to allow preprocessing of the pasted value. args => pastedValue, opts => return processedValue
  60. onBeforeWrite: null, //executes before writing to the masked element. args => event, opts
  61. onUnMask: null, //executes after unmasking to allow postprocessing of the unmaskedvalue. args => maskedValue, unmaskedValue, opts
  62. showMaskOnFocus: true, //show the mask-placeholder when the input has focus
  63. showMaskOnHover: true, //show the mask-placeholder when hovering the empty input
  64. onKeyValidation: $.noop, //executes on every key-press with the result of isValid. Params: result, opts
  65. skipOptionalPartCharacter: " ", //a character which can be used to skip an optional part of a mask
  66. showTooltip: false, //show the activemask as tooltip
  67. numericInput: false, //numericInput input direction style (input shifts to the left while holding the caret position)
  68. rightAlign: false, //align to the right
  69. undoOnEscape: true, //pressing escape reverts the value to the value before focus
  70. //numeric basic properties
  71. radixPoint: "", //".", // | ","
  72. groupSeparator: "", //",", // | "."
  73. radixFocus: false, //position caret to radixpoint on initial click
  74. //numeric basic properties
  75. nojumps: false, //do not jump over fixed parts in the mask
  76. nojumpsThreshold: 0, //start nojumps as of
  77. keepStatic: null, //try to keep the mask static while typing. Decisions to alter the mask will be posponed if possible - null see auto selection for multi masks
  78. positionCaretOnTab: false, //when enabled the caret position is set after the latest valid position on TAB
  79. tabThrough: false, //allows for tabbing through the different parts of the masked field
  80. supportsInputType: [], //allow extra inputtypes for masking, ex. number can be allowed for numeric alias without pre-/suffix and standard radixpoint, groupSeparator
  81. definitions: {
  82. "9": {
  83. validator: "[0-9]",
  84. cardinality: 1,
  85. definitionSymbol: "*"
  86. },
  87. "a": {
  88. validator: "[A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5]",
  89. cardinality: 1,
  90. definitionSymbol: "*"
  91. },
  92. "*": {
  93. validator: "[0-9A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5]",
  94. cardinality: 1
  95. }
  96. },
  97. //specify keyCodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF
  98. 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],
  99. isComplete: null, //override for isComplete - args => buffer, opts - return true || false
  100. canClearPosition: $.noop, //hook to alter the clear behavior in the stripValidPositions args => maskset, position, lastValidPosition, opts => return true|false
  101. postValidation: null //hook to postValidate the result from isValid. Usefull for validating the entry as a whole. args => buffer, opts => return true/false
  102. },
  103. masksCache: {},
  104. mask: function(el) {
  105. var scopedOpts = $.extend(true, {}, this.opts);
  106. importAttributeOptions(el, scopedOpts, $.extend(true, {}, this.userOptions));
  107. var maskset = generateMaskSet(scopedOpts, this.noMasksCache);
  108. if (maskset !== undefined) {
  109. //store inputmask instance on the input with element reference
  110. el.inputmask = el.inputmask || new Inputmask(); //in case of remasking reuse the inputmask object
  111. el.inputmask.opts = scopedOpts;
  112. el.inputmask.noMasksCache = this.noMasksCache;
  113. el.inputmask.userOptions = $.extend(true, {}, this.userOptions);
  114. el.inputmask.el = el;
  115. el.inputmask.maskset = maskset;
  116. el.inputmask.isRTL = false;
  117. $.data(el, "_inputmask_opts", scopedOpts);
  118. maskScope({
  119. "action": "mask",
  120. "el": el
  121. }, maskset, el.inputmask.opts);
  122. }
  123. return el;
  124. },
  125. option: function(options) { //set extra options || retrieve value of a current option
  126. if (typeof options === "string") {
  127. return this.opts[options];
  128. } else if (typeof options === "object") {
  129. $.extend(this.opts, options);
  130. $.extend(this.userOptions, options); //user passed options
  131. return this;
  132. }
  133. },
  134. unmaskedvalue: function() {
  135. if (this.el) {
  136. return maskScope({
  137. "action": "unmaskedvalue",
  138. "el": this.el
  139. });
  140. }
  141. },
  142. remove: function() {
  143. if (this.el) {
  144. maskScope({
  145. "action": "remove",
  146. "el": this.el
  147. });
  148. this.el.inputmask = undefined; //delete ~ undefined
  149. return this.el;
  150. }
  151. },
  152. getemptymask: function() { //return the default (empty) mask value, usefull for setting the default value in validation
  153. if (this.el) {
  154. return maskScope({
  155. "action": "getemptymask",
  156. "el": this.el
  157. });
  158. }
  159. },
  160. hasMaskedValue: function() { //check wheter the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  161. return !this.opts.autoUnmask;
  162. },
  163. isComplete: function() {
  164. if (this.el) {
  165. return maskScope({
  166. "action": "isComplete",
  167. "buffer": this.el.inputmask._valueGet().split(""),
  168. "el": this.el
  169. });
  170. }
  171. },
  172. getmetadata: function() { //return mask metadata if exists
  173. if (this.el) {
  174. return maskScope({
  175. "action": "getmetadata",
  176. "el": this.el
  177. });
  178. }
  179. }
  180. };
  181. //aply defaults, definitions, aliases
  182. Inputmask.extendDefaults = function(options) {
  183. $.extend(Inputmask.prototype.defaults, options);
  184. };
  185. Inputmask.extendDefinitions = function(definition) {
  186. $.extend(Inputmask.prototype.defaults.definitions, definition);
  187. };
  188. Inputmask.extendAliases = function(alias) {
  189. $.extend(Inputmask.prototype.defaults.aliases, alias);
  190. };
  191. //static fn on inputmask
  192. Inputmask.format = function(value, options, metadata) {
  193. var opts = $.extend(true, {}, Inputmask.prototype.defaults, options);
  194. resolveAlias(opts.alias, options, opts);
  195. return maskScope({
  196. "action": "format",
  197. "value": value,
  198. "metadata": metadata //true/false getmetadata
  199. }, generateMaskSet(opts, options && options.definitions !== undefined), opts);
  200. };
  201. Inputmask.unmask = function(value, options) {
  202. var opts = $.extend(true, {}, Inputmask.prototype.defaults, options);
  203. resolveAlias(opts.alias, options, opts);
  204. return maskScope({
  205. "action": "unmaskedvalue",
  206. "value": value
  207. }, generateMaskSet(opts, options && options.definitions !== undefined), opts);
  208. };
  209. Inputmask.isValid = function(value, options) {
  210. var opts = $.extend(true, {}, Inputmask.prototype.defaults, options);
  211. resolveAlias(opts.alias, options, opts);
  212. return maskScope({
  213. "action": "isValid",
  214. "value": value
  215. }, generateMaskSet(opts, options && options.definitions !== undefined), opts);
  216. };
  217. Inputmask.escapeRegex = function(str) {
  218. var specials = ["/", ".", "*", "+", "?", "|", "(", ")", "[", "]", "{", "}", "\\", "$", "^"];
  219. return str.replace(new RegExp("(\\" + specials.join("|\\") + ")", "gim"), "\\$1");
  220. };
  221. Inputmask.keyCode = {
  222. ALT: 18,
  223. BACKSPACE: 8,
  224. CAPS_LOCK: 20,
  225. COMMA: 188,
  226. COMMAND: 91,
  227. COMMAND_LEFT: 91,
  228. COMMAND_RIGHT: 93,
  229. CONTROL: 17,
  230. DELETE: 46,
  231. DOWN: 40,
  232. END: 35,
  233. ENTER: 13,
  234. ESCAPE: 27,
  235. HOME: 36,
  236. INSERT: 45,
  237. LEFT: 37,
  238. MENU: 93,
  239. NUMPAD_ADD: 107,
  240. NUMPAD_DECIMAL: 110,
  241. NUMPAD_DIVIDE: 111,
  242. NUMPAD_ENTER: 108,
  243. NUMPAD_MULTIPLY: 106,
  244. NUMPAD_SUBTRACT: 109,
  245. PAGE_DOWN: 34,
  246. PAGE_UP: 33,
  247. PERIOD: 190,
  248. RIGHT: 39,
  249. SHIFT: 16,
  250. SPACE: 32,
  251. TAB: 9,
  252. UP: 38,
  253. WINDOWS: 91
  254. };
  255. //helper functions
  256. function isInputEventSupported(eventName) {
  257. var el = document.createElement("input"),
  258. evName = "on" + eventName,
  259. isSupported = (evName in el);
  260. if (!isSupported) {
  261. el.setAttribute(evName, "return;");
  262. isSupported = typeof el[evName] == "function";
  263. }
  264. el = null;
  265. return isSupported;
  266. }
  267. function isInputTypeSupported(inputType) {
  268. var isSupported = inputType === "text" || inputType === "tel" || inputType === "password";
  269. if (!isSupported) {
  270. var el = document.createElement("input");
  271. el.setAttribute("type", inputType);
  272. isSupported = el.type === "text"; //apply mask only if the type is not natively supported
  273. el = null;
  274. }
  275. return isSupported;
  276. }
  277. function resolveAlias(aliasStr, options, opts) {
  278. var aliasDefinition = opts.aliases[aliasStr];
  279. if (aliasDefinition) {
  280. if (aliasDefinition.alias) resolveAlias(aliasDefinition.alias, undefined, opts); //alias is another alias
  281. $.extend(true, opts, aliasDefinition); //merge alias definition in the options
  282. $.extend(true, opts, options); //reapply extra given options
  283. return true;
  284. } else //alias not found - try as mask
  285. if (opts.mask === null) {
  286. opts.mask = aliasStr;
  287. }
  288. return false;
  289. }
  290. function importAttributeOptions(npt, opts, userOptions) {
  291. var attrOptions = npt.getAttribute("data-inputmask");
  292. function importOption(option) {
  293. var optionData = npt.getAttribute("data-inputmask-" + option.toLowerCase());
  294. if (optionData !== null) {
  295. optionData = typeof optionData == "boolean" ? optionData : optionData.toString();
  296. /*eslint-disable no-eval */
  297. if (typeof optionData === "string" && option.indexOf("on") === 0) {
  298. optionData = eval("(" + optionData + ")");
  299. }
  300. /*eslint-enable no-eval */
  301. if (option === "mask" && optionData.indexOf("[") === 0) {
  302. userOptions[option] = optionData.replace(/[\s[\]]/g, "").split(",");
  303. userOptions[option][0] = userOptions[option][0].replace("'", "");
  304. userOptions[option][userOptions[option].length - 1] = userOptions[option][userOptions[option].length - 1].replace("'", "");
  305. } else userOptions[option] = optionData;
  306. }
  307. }
  308. if (attrOptions && attrOptions !== "") {
  309. try {
  310. attrOptions = attrOptions.replace(new RegExp("'", "g"), '"');
  311. var dataoptions = $.parseJSON("{" + attrOptions + "}");
  312. $.extend(true, userOptions, dataoptions);
  313. } catch (ex) {
  314. } //need a more relax parseJSON
  315. }
  316. for (var option in opts) {
  317. importOption(option);
  318. }
  319. if (userOptions.alias) {
  320. resolveAlias(userOptions.alias, userOptions, opts);
  321. for (option in opts) {
  322. importOption(option);
  323. }
  324. }
  325. $.extend(true, opts, userOptions);
  326. return opts;
  327. }
  328. function generateMaskSet(opts, nocache) {
  329. var ms;
  330. function analyseMask(mask) {
  331. var tokenizer = /(?:[?*+]|\{[0-9\+\*]+(?:,[0-9\+\*]*)?\})|[^.?*+^${[]()|\\]+|./g,
  332. escaped = false,
  333. currentToken = new MaskToken(),
  334. match,
  335. m,
  336. openenings = [],
  337. maskTokens = [],
  338. openingToken,
  339. currentOpeningToken,
  340. alternator,
  341. lastMatch,
  342. groupToken;
  343. function MaskToken(isGroup, isOptional, isQuantifier, isAlternator) {
  344. this.matches = [];
  345. this.isGroup = isGroup || false;
  346. this.isOptional = isOptional || false;
  347. this.isQuantifier = isQuantifier || false;
  348. this.isAlternator = isAlternator || false;
  349. this.quantifier = {
  350. min: 1,
  351. max: 1
  352. };
  353. }
  354. //test definition => {fn: RegExp/function, cardinality: int, optionality: bool, newBlockMarker: bool, casing: null/upper/lower, def: definitionSymbol, placeholder: placeholder, mask: real maskDefinition}
  355. function insertTestDefinition(mtoken, element, position) {
  356. var maskdef = opts.definitions[element];
  357. position = position !== undefined ? position : mtoken.matches.length;
  358. var prevMatch = mtoken.matches[position - 1];
  359. if (maskdef && !escaped) {
  360. maskdef.placeholder = $.isFunction(maskdef.placeholder) ? maskdef.placeholder.call(this, opts) : maskdef.placeholder;
  361. var prevalidators = maskdef.prevalidator,
  362. prevalidatorsL = prevalidators ? prevalidators.length : 0;
  363. //handle prevalidators
  364. for (var i = 1; i < maskdef.cardinality; i++) {
  365. var prevalidator = prevalidatorsL >= i ? prevalidators[i - 1] : [],
  366. validator = prevalidator.validator,
  367. cardinality = prevalidator.cardinality;
  368. mtoken.matches.splice(position++, 0, {
  369. fn: validator ? typeof validator === "string" ? new RegExp(validator) : new function() {
  370. this.test = validator;
  371. } : new RegExp("."),
  372. cardinality: cardinality ? cardinality : 1,
  373. optionality: mtoken.isOptional,
  374. newBlockMarker: prevMatch === undefined || prevMatch.def !== (maskdef.definitionSymbol || element),
  375. casing: maskdef.casing,
  376. def: maskdef.definitionSymbol || element,
  377. placeholder: maskdef.placeholder,
  378. mask: element
  379. });
  380. prevMatch = mtoken.matches[position - 1];
  381. }
  382. mtoken.matches.splice(position++, 0, {
  383. fn: maskdef.validator ? typeof maskdef.validator == "string" ? new RegExp(maskdef.validator) : new function() {
  384. this.test = maskdef.validator;
  385. } : new RegExp("."),
  386. cardinality: maskdef.cardinality,
  387. optionality: mtoken.isOptional,
  388. newBlockMarker: prevMatch === undefined || prevMatch.def !== (maskdef.definitionSymbol || element),
  389. casing: maskdef.casing,
  390. def: maskdef.definitionSymbol || element,
  391. placeholder: maskdef.placeholder,
  392. mask: element
  393. });
  394. } else {
  395. mtoken.matches.splice(position++, 0, {
  396. fn: null,
  397. cardinality: 0,
  398. optionality: mtoken.isOptional,
  399. newBlockMarker: prevMatch === undefined || prevMatch.def !== element,
  400. casing: null,
  401. def: element,
  402. placeholder: undefined,
  403. mask: element
  404. });
  405. escaped = false;
  406. }
  407. }
  408. function verifyGroupMarker(lastMatch, isOpenGroup) {
  409. if (lastMatch.isGroup) { //this is not a group but a normal mask => convert
  410. lastMatch.isGroup = false;
  411. insertTestDefinition(lastMatch, opts.groupmarker.start, 0);
  412. if (isOpenGroup !== true) {
  413. insertTestDefinition(lastMatch, opts.groupmarker.end);
  414. }
  415. }
  416. }
  417. function maskCurrentToken(m, currentToken, lastMatch, extraCondition) {
  418. if (currentToken.matches.length > 0 && (extraCondition === undefined || extraCondition)) {
  419. lastMatch = currentToken.matches[currentToken.matches.length - 1];
  420. verifyGroupMarker(lastMatch);
  421. }
  422. insertTestDefinition(currentToken, m);
  423. }
  424. function defaultCase() {
  425. if (openenings.length > 0) {
  426. currentOpeningToken = openenings[openenings.length - 1];
  427. maskCurrentToken(m, currentOpeningToken, lastMatch, !currentOpeningToken.isAlternator);
  428. if (currentOpeningToken.isAlternator) { //handle alternator a | b case
  429. alternator = openenings.pop();
  430. for (var mndx = 0; mndx < alternator.matches.length; mndx++) {
  431. alternator.matches[mndx].isGroup = false; //don't mark alternate groups as group
  432. }
  433. if (openenings.length > 0) {
  434. currentOpeningToken = openenings[openenings.length - 1];
  435. currentOpeningToken.matches.push(alternator);
  436. } else {
  437. currentToken.matches.push(alternator);
  438. }
  439. }
  440. } else {
  441. maskCurrentToken(m, currentToken, lastMatch);
  442. }
  443. }
  444. function reverseTokens(maskToken) {
  445. function reverseStatic(st) {
  446. if (st === opts.optionalmarker.start) st = opts.optionalmarker.end;
  447. else if (st === opts.optionalmarker.end) st = opts.optionalmarker.start;
  448. else if (st === opts.groupmarker.start) st = opts.groupmarker.end;
  449. else if (st === opts.groupmarker.end) st = opts.groupmarker.start;
  450. return st;
  451. }
  452. maskToken.matches = maskToken.matches.reverse();
  453. for (var match in maskToken.matches) {
  454. var intMatch = parseInt(match);
  455. if (maskToken.matches[match].isQuantifier && maskToken.matches[intMatch + 1] && maskToken.matches[intMatch + 1].isGroup) { //reposition quantifier
  456. var qt = maskToken.matches[match];
  457. maskToken.matches.splice(match, 1);
  458. maskToken.matches.splice(intMatch + 1, 0, qt);
  459. }
  460. if (maskToken.matches[match].matches !== undefined) {
  461. maskToken.matches[match] = reverseTokens(maskToken.matches[match]);
  462. } else {
  463. maskToken.matches[match] = reverseStatic(maskToken.matches[match]);
  464. }
  465. }
  466. return maskToken;
  467. }
  468. while (match = tokenizer.exec(mask)) {
  469. m = match[0];
  470. if (escaped) {
  471. defaultCase();
  472. continue;
  473. }
  474. switch (m.charAt(0)) {
  475. case opts.escapeChar:
  476. escaped = true;
  477. break;
  478. case opts.optionalmarker.end:
  479. // optional closing
  480. case opts.groupmarker.end:
  481. // Group closing
  482. openingToken = openenings.pop();
  483. if (openingToken !== undefined) {
  484. if (openenings.length > 0) {
  485. currentOpeningToken = openenings[openenings.length - 1];
  486. currentOpeningToken.matches.push(openingToken);
  487. if (currentOpeningToken.isAlternator) { //handle alternator (a) | (b) case
  488. alternator = openenings.pop();
  489. for (var mndx = 0; mndx < alternator.matches.length; mndx++) {
  490. alternator.matches[mndx].isGroup = false; //don't mark alternate groups as group
  491. }
  492. if (openenings.length > 0) {
  493. currentOpeningToken = openenings[openenings.length - 1];
  494. currentOpeningToken.matches.push(alternator);
  495. } else {
  496. currentToken.matches.push(alternator);
  497. }
  498. }
  499. } else {
  500. currentToken.matches.push(openingToken);
  501. }
  502. } else defaultCase();
  503. break;
  504. case opts.optionalmarker.start:
  505. // optional opening
  506. openenings.push(new MaskToken(false, true));
  507. break;
  508. case opts.groupmarker.start:
  509. // Group opening
  510. openenings.push(new MaskToken(true));
  511. break;
  512. case opts.quantifiermarker.start:
  513. //Quantifier
  514. var quantifier = new MaskToken(false, false, true);
  515. m = m.replace(/[{}]/g, "");
  516. var mq = m.split(","),
  517. mq0 = isNaN(mq[0]) ? mq[0] : parseInt(mq[0]),
  518. mq1 = mq.length === 1 ? mq0 : (isNaN(mq[1]) ? mq[1] : parseInt(mq[1]));
  519. if (mq1 === "*" || mq1 === "+") {
  520. mq0 = mq1 === "*" ? 0 : 1;
  521. }
  522. quantifier.quantifier = {
  523. min: mq0,
  524. max: mq1
  525. };
  526. if (openenings.length > 0) {
  527. var matches = openenings[openenings.length - 1].matches;
  528. match = matches.pop();
  529. if (!match.isGroup) {
  530. groupToken = new MaskToken(true);
  531. groupToken.matches.push(match);
  532. match = groupToken;
  533. }
  534. matches.push(match);
  535. matches.push(quantifier);
  536. } else {
  537. match = currentToken.matches.pop();
  538. if (!match.isGroup) {
  539. groupToken = new MaskToken(true);
  540. groupToken.matches.push(match);
  541. match = groupToken;
  542. }
  543. currentToken.matches.push(match);
  544. currentToken.matches.push(quantifier);
  545. }
  546. break;
  547. case opts.alternatormarker:
  548. if (openenings.length > 0) {
  549. currentOpeningToken = openenings[openenings.length - 1];
  550. lastMatch = currentOpeningToken.matches.pop();
  551. } else {
  552. lastMatch = currentToken.matches.pop();
  553. }
  554. if (lastMatch.isAlternator) {
  555. openenings.push(lastMatch);
  556. } else {
  557. alternator = new MaskToken(false, false, false, true);
  558. alternator.matches.push(lastMatch);
  559. openenings.push(alternator);
  560. }
  561. break;
  562. default:
  563. defaultCase();
  564. }
  565. }
  566. while (openenings.length > 0) {
  567. openingToken = openenings.pop();
  568. verifyGroupMarker(openingToken, true);
  569. currentToken.matches.push(openingToken);
  570. }
  571. if (currentToken.matches.length > 0) {
  572. lastMatch = currentToken.matches[currentToken.matches.length - 1];
  573. verifyGroupMarker(lastMatch);
  574. maskTokens.push(currentToken);
  575. }
  576. if (opts.numericInput) {
  577. reverseTokens(maskTokens[0]);
  578. }
  579. // console.log(JSON.stringify(maskTokens));
  580. return maskTokens;
  581. }
  582. function generateMask(mask, metadata) {
  583. if (mask === null || mask === "") {
  584. return undefined;
  585. } else {
  586. if (mask.length === 1 && opts.greedy === false && opts.repeat !== 0) {
  587. opts.placeholder = "";
  588. } //hide placeholder with single non-greedy mask
  589. if (opts.repeat > 0 || opts.repeat === "*" || opts.repeat === "+") {
  590. var repeatStart = opts.repeat === "*" ? 0 : (opts.repeat === "+" ? 1 : opts.repeat);
  591. mask = opts.groupmarker.start + mask + opts.groupmarker.end + opts.quantifiermarker.start + repeatStart + "," + opts.repeat + opts.quantifiermarker.end;
  592. }
  593. // console.log(mask);
  594. var masksetDefinition;
  595. if (Inputmask.prototype.masksCache[mask] === undefined || nocache === true) {
  596. masksetDefinition = {
  597. "mask": mask,
  598. "maskToken": analyseMask(mask),
  599. "validPositions": {},
  600. "_buffer": undefined,
  601. "buffer": undefined,
  602. "tests": {},
  603. "metadata": metadata
  604. };
  605. if (nocache !== true) {
  606. Inputmask.prototype.masksCache[opts.numericInput ? mask.split("").reverse().join("") : mask] = masksetDefinition;
  607. }
  608. } else masksetDefinition = $.extend(true, {}, Inputmask.prototype.masksCache[mask]);
  609. return masksetDefinition;
  610. }
  611. }
  612. function preProcessMask(mask) {
  613. mask = mask.toString();
  614. // if (opts.numericInput) {
  615. // mask = mask.split('').reverse();
  616. // mask = mask.join('');
  617. // }
  618. return mask;
  619. }
  620. if ($.isFunction(opts.mask)) { //allow mask to be a preprocessing fn - should return a valid mask
  621. opts.mask = opts.mask.call(this, opts);
  622. }
  623. if ($.isArray(opts.mask)) {
  624. if (opts.mask.length > 1) {
  625. opts.keepStatic = opts.keepStatic === null ? true : opts.keepStatic; //enable by default when passing multiple masks when the option is not explicitly specified
  626. var altMask = "(";
  627. $.each(opts.numericInput ? opts.mask.reverse() : opts.mask, function(ndx, msk) {
  628. if (altMask.length > 1) {
  629. altMask += ")|(";
  630. }
  631. if (msk.mask !== undefined && !$.isFunction(msk.mask)) {
  632. altMask += preProcessMask(msk.mask);
  633. } else {
  634. altMask += preProcessMask(msk);
  635. }
  636. });
  637. altMask += ")";
  638. return generateMask(altMask, opts.mask);
  639. } else opts.mask = opts.mask.pop();
  640. }
  641. if (opts.mask) {
  642. if (opts.mask.mask !== undefined && !$.isFunction(opts.mask.mask)) {
  643. ms = generateMask(preProcessMask(opts.mask.mask), opts.mask);
  644. } else {
  645. ms = generateMask(preProcessMask(opts.mask), opts.mask);
  646. }
  647. }
  648. return ms;
  649. }
  650. var ua = navigator.userAgent,
  651. iphone = ua.match(new RegExp("iphone", "i")) !== null,
  652. // android = ua.match(new RegExp("android.*safari.*", "i")) !== null,
  653. androidchrome = ua.match(new RegExp("android.*chrome.*", "i")) !== null,
  654. androidfirefox = ua.match(new RegExp("android.*firefox.*", "i")) !== null,
  655. // 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),
  656. PasteEventType = isInputEventSupported("paste") ? "paste" : isInputEventSupported("input") ? "input" : "propertychange";
  657. //if (androidchrome) {
  658. // var browser = navigator.userAgent.match(new RegExp("chrome.*", "i")),
  659. // version = parseInt(new RegExp(/[0-9]+/).exec(browser));
  660. // androidchrome32 = (version == 32);
  661. //}
  662. //masking scope
  663. //actionObj definition see below
  664. function maskScope(actionObj, maskset, opts) {
  665. var isRTL = false,
  666. undoValue,
  667. // compositionValidPos,
  668. compositionCaretPos,
  669. compositionData,
  670. el, $el,
  671. skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
  672. skipInputEvent = false, //skip when triggered from within inputmask
  673. ignorable = false,
  674. maxLength,
  675. mouseEnter = true;
  676. //maskset helperfunctions
  677. function getMaskTemplate(baseOnInput, minimalPos, includeInput) {
  678. minimalPos = minimalPos || 0;
  679. var maskTemplate = [],
  680. ndxIntlzr, pos = 0,
  681. test, testPos;
  682. do {
  683. if (baseOnInput === true && getMaskSet().validPositions[pos]) {
  684. var validPos = getMaskSet().validPositions[pos];
  685. test = validPos.match;
  686. ndxIntlzr = validPos.locator.slice();
  687. maskTemplate.push(includeInput === true ? validPos.input : getPlaceholder(pos, test));
  688. } else {
  689. //console.log("getmasktemplate " + pos + " " + JSON.stringify(ndxIntlzr));
  690. testPos = getTestTemplate(pos, ndxIntlzr, pos - 1);
  691. test = testPos.match;
  692. ndxIntlzr = testPos.locator.slice();
  693. maskTemplate.push(getPlaceholder(pos, test));
  694. }
  695. pos++;
  696. } while ((maxLength === undefined || pos - 1 < maxLength) && test.fn !== null || (test.fn === null && test.def !== "") || minimalPos >= pos);
  697. maskTemplate.pop(); //drop the last one which is empty
  698. return maskTemplate;
  699. }
  700. function getMaskSet() {
  701. return maskset;
  702. }
  703. function resetMaskSet(soft) {
  704. var maskset = getMaskSet();
  705. maskset.buffer = undefined;
  706. maskset.tests = {};
  707. if (soft !== true) {
  708. maskset._buffer = undefined;
  709. maskset.validPositions = {};
  710. maskset.p = 0;
  711. }
  712. }
  713. function getLastValidPosition(closestTo, strict) {
  714. var maskset = getMaskSet(),
  715. lastValidPosition = -1,
  716. valids = maskset.validPositions;
  717. if (closestTo === undefined) closestTo = -1;
  718. var before = lastValidPosition,
  719. after = lastValidPosition;
  720. for (var posNdx in valids) {
  721. var psNdx = parseInt(posNdx);
  722. if (valids[psNdx] && (strict || valids[psNdx].match.fn !== null)) {
  723. if (psNdx <= closestTo) before = psNdx;
  724. if (psNdx >= closestTo) after = psNdx;
  725. }
  726. }
  727. lastValidPosition = (before !== -1 && (closestTo - before) > 1) || after < closestTo ? before : after;
  728. return lastValidPosition;
  729. }
  730. function setValidPosition(pos, validTest, fromSetValid) {
  731. if (opts.insertMode && getMaskSet().validPositions[pos] !== undefined && fromSetValid === undefined) {
  732. //reposition & revalidate others
  733. var positionsClone = $.extend(true, {}, getMaskSet().validPositions),
  734. lvp = getLastValidPosition(),
  735. i;
  736. for (i = pos; i <= lvp; i++) { //clear selection
  737. delete getMaskSet().validPositions[i];
  738. }
  739. getMaskSet().validPositions[pos] = validTest;
  740. var valid = true,
  741. j, vps = getMaskSet().validPositions;
  742. for (i = (j = pos); i <= lvp; i++) {
  743. var t = positionsClone[i];
  744. if (t !== undefined) {
  745. var posMatch = j,
  746. prevPosMatch = -1;
  747. while (posMatch < getMaskLength() && ((t.match.fn == null && vps[i] && (vps[i].match.optionalQuantifier === true || vps[i].match.optionality === true)) || t.match.fn != null)) {
  748. //determine next position
  749. if (t.match.fn === null || (!opts.keepStatic && vps[i] && (vps[i + 1] !== undefined && getTests(i + 1, vps[i].locator.slice(), i).length > 1 || vps[i].alternation !== undefined))) {
  750. posMatch++;
  751. } else posMatch = seekNext(j);
  752. //does it match
  753. if (positionCanMatchDefinition(posMatch, t.match.def)) {
  754. valid = isValid(posMatch, t.input, true, true) !== false;
  755. j = posMatch;
  756. break;
  757. } else {
  758. valid = t.match.fn == null;
  759. if (prevPosMatch === posMatch) break; //prevent endless loop
  760. prevPosMatch = posMatch;
  761. }
  762. }
  763. }
  764. if (!valid) break;
  765. }
  766. if (!valid) {
  767. getMaskSet().validPositions = $.extend(true, {}, positionsClone);
  768. return false;
  769. }
  770. } else getMaskSet().validPositions[pos] = validTest;
  771. return true;
  772. }
  773. function stripValidPositions(start, end, nocheck, strict) {
  774. var i, startPos = start;
  775. getMaskSet().p = start; //needed for alternated position after overtype selection
  776. for (i = startPos; i < end; i++) { //clear selection
  777. if (getMaskSet().validPositions[i] !== undefined) {
  778. if (nocheck === true || opts.canClearPosition(getMaskSet(), i, getLastValidPosition(), strict, opts) !== false) {
  779. delete getMaskSet().validPositions[i];
  780. }
  781. }
  782. }
  783. resetMaskSet(true);
  784. /*eslint-disable semi-spacing */
  785. for (i = startPos + 1; i <= getLastValidPosition();) {
  786. /*eslint-disable semi-spacing */
  787. while (getMaskSet().validPositions[startPos] !== undefined) startPos++;
  788. var s = getMaskSet().validPositions[startPos];
  789. if (i < startPos) i = startPos + 1;
  790. // while (getMaskSet().validPositions[i] == undefined) i++;
  791. var t = getMaskSet().validPositions[i];
  792. if (t !== undefined && isMask(i) && s === undefined) {
  793. if (positionCanMatchDefinition(startPos, t.match.def) && isValid(startPos, t.input, true) !== false) {
  794. delete getMaskSet().validPositions[i];
  795. i++;
  796. }
  797. startPos++;
  798. } else i++;
  799. }
  800. //remove radixpoint if needed
  801. var lvp = getLastValidPosition(),
  802. ml = getMaskLength();
  803. if (strict !== true && nocheck !== true && getMaskSet().validPositions[lvp] !== undefined && getMaskSet().validPositions[lvp].input === opts.radixPoint) {
  804. delete getMaskSet().validPositions[lvp];
  805. }
  806. for (i = lvp + 1; i <= ml; i++) {
  807. if (getMaskSet().validPositions[i]) {
  808. delete getMaskSet().validPositions[i];
  809. }
  810. }
  811. resetMaskSet(true);
  812. }
  813. function getTestTemplate(pos, ndxIntlzr, tstPs) {
  814. var testPos = getMaskSet().validPositions[pos];
  815. if (testPos === undefined) {
  816. var testPositions = getTests(pos, ndxIntlzr, tstPs),
  817. lvp = getLastValidPosition(),
  818. lvTest = getMaskSet().validPositions[lvp] || getTests(0)[0],
  819. lvTestAltArr = (lvTest.alternation !== undefined) ? lvTest.locator[lvTest.alternation].toString().split(",") : [];
  820. for (var ndx = 0; ndx < testPositions.length; ndx++) {
  821. testPos = testPositions[ndx];
  822. if (testPos.match &&
  823. (((opts.greedy && testPos.match.optionalQuantifier !== true) || (testPos.match.optionality === false || testPos.match.newBlockMarker === false) && testPos.match.optionalQuantifier !== true) &&
  824. ((lvTest.alternation === undefined || lvTest.alternation !== testPos.alternation) ||
  825. (testPos.locator[lvTest.alternation] !== undefined && checkAlternationMatch(testPos.locator[lvTest.alternation].toString().split(","), lvTestAltArr))))) {
  826. break;
  827. }
  828. }
  829. }
  830. return testPos;
  831. }
  832. function getTest(pos) {
  833. if (getMaskSet().validPositions[pos]) {
  834. return getMaskSet().validPositions[pos].match;
  835. }
  836. return getTests(pos)[0].match;
  837. }
  838. function positionCanMatchDefinition(pos, def) {
  839. var valid = false,
  840. tests = getTests(pos);
  841. for (var tndx = 0; tndx < tests.length; tndx++) {
  842. if (tests[tndx].match && tests[tndx].match.def === def) {
  843. valid = true;
  844. break;
  845. }
  846. }
  847. return valid;
  848. }
  849. function getTests(pos, ndxIntlzr, tstPs, cacheable) {
  850. var maskTokens = getMaskSet().maskToken,
  851. testPos = ndxIntlzr ? tstPs : 0,
  852. ndxInitializer = ndxIntlzr || [0],
  853. matches = [],
  854. insertStop = false,
  855. latestMatch, isFirstMatch;
  856. function resolveTestFromToken(maskToken, ndxInitializer, loopNdx, quantifierRecurse) { //ndxInitilizer contains a set of indexes to speedup searches in the mtokens
  857. function handleMatch(match, loopNdx, quantifierRecurse) {
  858. if (testPos > 10000) {
  859. throw "Inputmask: There is probably an error in your mask definition or in the code. Create an issue on github with an example of the mask you are using. " + getMaskSet().mask;
  860. }
  861. if (testPos === pos && match.matches === undefined) {
  862. matches.push({
  863. "match": match,
  864. "locator": loopNdx.reverse()
  865. });
  866. return true;
  867. } else if (match.matches !== undefined) {
  868. if (match.isGroup && quantifierRecurse !== match) { //when a group pass along to the quantifier
  869. match = handleMatch(maskToken.matches[$.inArray(match, maskToken.matches) + 1], loopNdx);
  870. if (match) return true;
  871. } else if (match.isOptional) {
  872. var optionalToken = match;
  873. match = resolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
  874. if (match) {
  875. latestMatch = matches[matches.length - 1].match;
  876. isFirstMatch = $.inArray(latestMatch, optionalToken.matches) === 0;
  877. if (isFirstMatch) {
  878. insertStop = true; //insert a stop
  879. testPos = pos; //match the position after the group
  880. } else return true;
  881. }
  882. } else if (match.isAlternator) {
  883. var alternateToken = match,
  884. malternateMatches = [],
  885. maltMatches,
  886. currentMatches = matches.slice(),
  887. loopNdxCnt = loopNdx.length;
  888. var altIndex = ndxInitializer.length > 0 ? ndxInitializer.shift() : -1;
  889. if (altIndex === -1 || typeof altIndex === "string") {
  890. var currentPos = testPos,
  891. ndxInitializerClone = ndxInitializer.slice(),
  892. altIndexArr = [];
  893. if (typeof altIndex == "string") altIndexArr = altIndex.split(",");
  894. for (var amndx = 0; amndx < alternateToken.matches.length; amndx++) {
  895. matches = [];
  896. match = handleMatch(alternateToken.matches[amndx], [amndx].concat(loopNdx), quantifierRecurse) || match;
  897. if (match !== true && match !== undefined && (altIndexArr[altIndexArr.length - 1] < alternateToken.matches.length)) { //no match in the alternations (length mismatch) => look further
  898. var ntndx = maskToken.matches.indexOf(match) + 1;
  899. if (maskToken.matches.length > ntndx) {
  900. match = handleMatch(maskToken.matches[ntndx], [ntndx].concat(loopNdx.slice(1, loopNdx.length)), quantifierRecurse);
  901. if (match) {
  902. altIndexArr.push(ntndx.toString());
  903. $.each(matches, function(ndx, lmnt) {
  904. lmnt.alternation = loopNdx.length - 1;
  905. });
  906. }
  907. }
  908. }
  909. maltMatches = matches.slice();
  910. testPos = currentPos;
  911. matches = [];
  912. //cloneback
  913. for (var i = 0; i < ndxInitializerClone.length; i++) {
  914. ndxInitializer[i] = ndxInitializerClone[i];
  915. }
  916. //fuzzy merge matches
  917. for (var ndx1 = 0; ndx1 < maltMatches.length; ndx1++) {
  918. var altMatch = maltMatches[ndx1];
  919. altMatch.alternation = altMatch.alternation || loopNdxCnt;
  920. for (var ndx2 = 0; ndx2 < malternateMatches.length; ndx2++) {
  921. var altMatch2 = malternateMatches[ndx2];
  922. //verify equality
  923. if (altMatch.match.mask === altMatch2.match.mask && (typeof altIndex !== "string" || $.inArray(altMatch.locator[altMatch.alternation].toString(), altIndexArr) !== -1)) {
  924. maltMatches.splice(ndx1, 1);
  925. ndx1--;
  926. altMatch2.locator[altMatch.alternation] = altMatch2.locator[altMatch.alternation] + "," + altMatch.locator[altMatch.alternation];
  927. altMatch2.alternation = altMatch.alternation; //we pass the alternation index => used in determineLastRequiredPosition
  928. break;
  929. }
  930. }
  931. }
  932. malternateMatches = malternateMatches.concat(maltMatches);
  933. }
  934. if (typeof altIndex == "string") { //filter matches
  935. malternateMatches = $.map(malternateMatches, function(lmnt, ndx) {
  936. if (isFinite(ndx)) {
  937. var mamatch,
  938. alternation = lmnt.alternation,
  939. altLocArr = lmnt.locator[alternation].toString().split(",");
  940. lmnt.locator[alternation] = undefined;
  941. lmnt.alternation = undefined;
  942. for (var alndx = 0; alndx < altLocArr.length; alndx++) {
  943. mamatch = $.inArray(altLocArr[alndx], altIndexArr) !== -1;
  944. if (mamatch) { //rebuild the locator with valid entries
  945. if (lmnt.locator[alternation] !== undefined) {
  946. lmnt.locator[alternation] += ",";
  947. lmnt.locator[alternation] += altLocArr[alndx];
  948. } else lmnt.locator[alternation] = parseInt(altLocArr[alndx]);
  949. lmnt.alternation = alternation;
  950. }
  951. }
  952. if (lmnt.locator[alternation] !== undefined) return lmnt;
  953. }
  954. });
  955. }
  956. matches = currentMatches.concat(malternateMatches);
  957. testPos = pos;
  958. insertStop = matches.length > 0; //insert a stopelemnt when there is an alternate
  959. } else {
  960. if (alternateToken.matches[altIndex]) { //if not in the initial alternation => look further
  961. match = handleMatch(alternateToken.matches[altIndex], [altIndex].concat(loopNdx), quantifierRecurse);
  962. } else match = false;
  963. }
  964. if (match) return true;
  965. } else if (match.isQuantifier && quantifierRecurse !== maskToken.matches[$.inArray(match, maskToken.matches) - 1]) {
  966. var qt = match;
  967. for (var qndx = (ndxInitializer.length > 0) ? ndxInitializer.shift() : 0;
  968. (qndx < (isNaN(qt.quantifier.max) ? qndx + 1 : qt.quantifier.max)) && testPos <= pos; qndx++) {
  969. var tokenGroup = maskToken.matches[$.inArray(qt, maskToken.matches) - 1];
  970. match = handleMatch(tokenGroup, [qndx].concat(loopNdx), tokenGroup); //set the tokenGroup as quantifierRecurse marker
  971. if (match) {
  972. //get latest match
  973. latestMatch = matches[matches.length - 1].match;
  974. latestMatch.optionalQuantifier = qndx > (qt.quantifier.min - 1);
  975. isFirstMatch = $.inArray(latestMatch, tokenGroup.matches) === 0;
  976. if (isFirstMatch) { //search for next possible match
  977. if (qndx > (qt.quantifier.min - 1)) {
  978. insertStop = true;
  979. testPos = pos; //match the position after the group
  980. break; //stop quantifierloop
  981. } else return true;
  982. } else {
  983. return true;
  984. }
  985. }
  986. }
  987. } else {
  988. match = resolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse);
  989. if (match) return true;
  990. }
  991. } else testPos++;
  992. }
  993. for (var tndx = (ndxInitializer.length > 0 ? ndxInitializer.shift() : 0); tndx < maskToken.matches.length; tndx++) {
  994. if (maskToken.matches[tndx].isQuantifier !== true) {
  995. var match = handleMatch(maskToken.matches[tndx], [tndx].concat(loopNdx), quantifierRecurse);
  996. if (match && testPos === pos) {
  997. return match;
  998. } else if (testPos > pos) {
  999. break;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. if (cacheable === true && getMaskSet().tests[pos]) {
  1005. return getMaskSet().tests[pos];
  1006. }
  1007. if (ndxIntlzr === undefined) {
  1008. var previousPos = pos - 1,
  1009. test;
  1010. while ((test = getMaskSet().validPositions[previousPos]) === undefined && previousPos > -1) {
  1011. if (getMaskSet().tests[previousPos] && (test = getMaskSet().tests[previousPos][0]) !== undefined) {
  1012. break;
  1013. }
  1014. previousPos--;
  1015. }
  1016. if (test !== undefined && previousPos > -1) {
  1017. testPos = previousPos;
  1018. ndxInitializer = test.locator.slice();
  1019. }
  1020. }
  1021. for (var mtndx = ndxInitializer.shift(); mtndx < maskTokens.length; mtndx++) {
  1022. var match = resolveTestFromToken(maskTokens[mtndx], ndxInitializer, [mtndx]);
  1023. if ((match && testPos === pos) || testPos > pos) {
  1024. break;
  1025. }
  1026. }
  1027. if (matches.length === 0 || insertStop) {
  1028. matches.push({
  1029. "match": {
  1030. fn: null,
  1031. cardinality: 0,
  1032. optionality: true,
  1033. casing: null,
  1034. def: ""
  1035. },
  1036. "locator": []
  1037. });
  1038. }
  1039. getMaskSet().tests[pos] = $.extend(true, [], matches); //set a clone to prevent overwriting some props
  1040. // console.log(pos + " - " + JSON.stringify(matches));
  1041. return getMaskSet().tests[pos];
  1042. }
  1043. function getBufferTemplate() {
  1044. if (getMaskSet()._buffer === undefined) {
  1045. //generate template
  1046. getMaskSet()._buffer = getMaskTemplate(false, 1);
  1047. }
  1048. return getMaskSet()._buffer;
  1049. }
  1050. function getBuffer() {
  1051. if (getMaskSet().buffer === undefined) {
  1052. getMaskSet().buffer = getMaskTemplate(true, getLastValidPosition(), true);
  1053. }
  1054. return getMaskSet().buffer;
  1055. }
  1056. function refreshFromBuffer(start, end, buffer) {
  1057. var i;
  1058. buffer = buffer || getBuffer().slice(); //pass or work on clone
  1059. if (start === true) {
  1060. resetMaskSet();
  1061. start = 0;
  1062. end = buffer.length;
  1063. } else {
  1064. for (i = start; i < end; i++) {
  1065. delete getMaskSet().validPositions[i];
  1066. delete getMaskSet().tests[i];
  1067. }
  1068. }
  1069. for (i = start; i < end; i++) {
  1070. resetMaskSet(true); //prevents clobber from the buffer
  1071. if (buffer[i] !== opts.skipOptionalPartCharacter) {
  1072. isValid(i, buffer[i], true, true);
  1073. }
  1074. }
  1075. }
  1076. function casing(elem, test) {
  1077. switch (test.casing) {
  1078. case "upper":
  1079. elem = elem.toUpperCase();
  1080. break;
  1081. case "lower":
  1082. elem = elem.toLowerCase();
  1083. break;
  1084. }
  1085. return elem;
  1086. }
  1087. function checkAlternationMatch(altArr1, altArr2) {
  1088. var altArrC = opts.greedy ? altArr2 : altArr2.slice(0, 1),
  1089. isMatch = false;
  1090. for (var alndx = 0; alndx < altArr1.length; alndx++) {
  1091. if ($.inArray(altArr1[alndx], altArrC) !== -1) {
  1092. isMatch = true;
  1093. break;
  1094. }
  1095. }
  1096. return isMatch;
  1097. }
  1098. function isValid(pos, c, strict, fromSetValid) { //strict true ~ no correction or autofill
  1099. strict = strict === true; //always set a value to strict to prevent possible strange behavior in the extensions
  1100. function _isValid(position, c, strict, fromSetValid) {
  1101. var rslt = false;
  1102. $.each(getTests(position), function(ndx, tst) {
  1103. var test = tst.match;
  1104. var loopend = c ? 1 : 0,
  1105. chrs = "";
  1106. for (var i = test.cardinality; i > loopend; i--) {
  1107. chrs += getBufferElement(position - (i - 1));
  1108. }
  1109. if (c) {
  1110. chrs += c;
  1111. }
  1112. //return is false or a json object => { pos: ??, c: ??} or true
  1113. rslt = test.fn != null ?
  1114. test.fn.test(chrs, getMaskSet(), position, strict, opts) : (c === test.def || c === opts.skipOptionalPartCharacter) && test.def !== "" ? //non mask
  1115. {
  1116. c: test.def,
  1117. pos: position
  1118. } : false;
  1119. if (rslt !== false) {
  1120. var elem = rslt.c !== undefined ? rslt.c : c;
  1121. elem = (elem === opts.skipOptionalPartCharacter && test.fn === null) ? test.def : elem;
  1122. var validatedPos = position,
  1123. possibleModifiedBuffer = getBuffer();
  1124. if (rslt.remove !== undefined) { //remove position(s)
  1125. if (!$.isArray(rslt.remove)) rslt.remove = [rslt.remove];
  1126. $.each(rslt.remove.sort(function(a, b) {
  1127. return b - a;
  1128. }), function(ndx, lmnt) {
  1129. stripValidPositions(lmnt, lmnt + 1, true);
  1130. });
  1131. }
  1132. if (rslt.insert !== undefined) { //insert position(s)
  1133. if (!$.isArray(rslt.insert)) rslt.insert = [rslt.insert];
  1134. $.each(rslt.insert.sort(function(a, b) {
  1135. return a - b;
  1136. }), function(ndx, lmnt) {
  1137. isValid(lmnt.pos, lmnt.c, true);
  1138. });
  1139. }
  1140. if (rslt.refreshFromBuffer) {
  1141. var refresh = rslt.refreshFromBuffer;
  1142. strict = true;
  1143. refreshFromBuffer(refresh === true ? refresh : refresh.start, refresh.end, possibleModifiedBuffer);
  1144. if (rslt.pos === undefined && rslt.c === undefined) {
  1145. rslt.pos = getLastValidPosition();
  1146. return false; //breakout if refreshFromBuffer && nothing to insert
  1147. }
  1148. validatedPos = rslt.pos !== undefined ? rslt.pos : position;
  1149. if (validatedPos !== position) {
  1150. rslt = $.extend(rslt, isValid(validatedPos, elem, true)); //revalidate new position strict
  1151. return false;
  1152. }
  1153. } else if (rslt !== true && rslt.pos !== undefined && rslt.pos !== position) { //their is a position offset
  1154. validatedPos = rslt.pos;
  1155. refreshFromBuffer(position, validatedPos);
  1156. if (validatedPos !== position) {
  1157. rslt = $.extend(rslt, isValid(validatedPos, elem, true)); //revalidate new position strict
  1158. return false;
  1159. }
  1160. }
  1161. if (rslt !== true && rslt.pos === undefined && rslt.c === undefined) {
  1162. return false; //breakout if nothing to insert
  1163. }
  1164. if (ndx > 0) {
  1165. resetMaskSet(true);
  1166. }
  1167. if (!setValidPosition(validatedPos, $.extend({}, tst, {
  1168. "input": casing(elem, test)
  1169. }), fromSetValid)) {
  1170. rslt = false;
  1171. }
  1172. return false; //break from $.each
  1173. }
  1174. });
  1175. return rslt;
  1176. }
  1177. function alternate(pos, c, strict, fromSetValid) {
  1178. var validPsClone = $.extend(true, {}, getMaskSet().validPositions),
  1179. lastAlt,
  1180. alternation,
  1181. isValidRslt,
  1182. altPos, i, validPos;
  1183. //find last modified alternation
  1184. for (var lAlt = getLastValidPosition(); lAlt >= 0; lAlt--) {
  1185. altPos = getMaskSet().validPositions[lAlt];
  1186. if (altPos && altPos.alternation !== undefined) {
  1187. lastAlt = lAlt;
  1188. alternation = getMaskSet().validPositions[lastAlt].alternation;
  1189. if (getTestTemplate(lastAlt).locator[altPos.alternation] !== altPos.locator[altPos.alternation]) {
  1190. break;
  1191. }
  1192. }
  1193. }
  1194. if (alternation !== undefined) {
  1195. //find first decision making position
  1196. lastAlt = parseInt(lastAlt);
  1197. for (var decisionPos in getMaskSet().validPositions) {
  1198. decisionPos = parseInt(decisionPos);
  1199. altPos = getMaskSet().validPositions[decisionPos];
  1200. if (decisionPos >= lastAlt && altPos.alternation !== undefined) {
  1201. var altNdxs = getMaskSet().validPositions[lastAlt].locator[alternation].toString().split(","),
  1202. decisionTaker = altPos.locator[alternation] || altNdxs[0]; //no match in the alternations (length mismatch)
  1203. if (decisionTaker.length > 0) { //no decision taken ~ take first one as decider
  1204. decisionTaker = decisionTaker.split(",")[0];
  1205. }
  1206. for (var mndx = 0; mndx < altNdxs.length; mndx++) {
  1207. if (decisionTaker < altNdxs[mndx]) {
  1208. var possibilityPos, possibilities;
  1209. for (var dp = decisionPos; dp >= 0; dp--) {
  1210. possibilityPos = getMaskSet().validPositions[dp];
  1211. if (possibilityPos !== undefined) {
  1212. possibilities = possibilityPos.locator[alternation]; //store to reset
  1213. //possibilityPos.alternation = undefined;
  1214. possibilityPos.locator[alternation] = parseInt(altNdxs[mndx]);
  1215. break;
  1216. }
  1217. }
  1218. if (decisionTaker !== possibilityPos.locator[alternation]) {
  1219. var validInputs = [],
  1220. staticInputsBeforePos = 0;
  1221. for (i = decisionPos + 1; i < getLastValidPosition() + 1; i++) {
  1222. validPos = getMaskSet().validPositions[i];
  1223. if (validPos) {
  1224. if (validPos.match.fn != null) {
  1225. validInputs.push(validPos.input);
  1226. } else if (i < pos) staticInputsBeforePos++;
  1227. }
  1228. delete getMaskSet().validPositions[i];
  1229. delete getMaskSet().tests[i];
  1230. }
  1231. resetMaskSet(true); //clear getbuffer
  1232. opts.keepStatic = !opts.keepStatic; //disable keepStatic on getMaskLength
  1233. isValidRslt = true;
  1234. while (validInputs.length > 0) {
  1235. var input = validInputs.shift();
  1236. if (input !== opts.skipOptionalPartCharacter) {
  1237. if (!(isValidRslt = isValid(getLastValidPosition() + 1, input, false, true))) {
  1238. break;
  1239. }
  1240. }
  1241. }
  1242. possibilityPos.alternation = alternation;
  1243. possibilityPos.locator[alternation] = possibilities; //reset forceddecision ~ needed for proper delete
  1244. if (isValidRslt) {
  1245. var targetLvp = getLastValidPosition(pos) + 1;
  1246. var staticInputsBeforePosAlternate = 0;
  1247. for (i = decisionPos + 1; i < getLastValidPosition() + 1; i++) {
  1248. validPos = getMaskSet().validPositions[i];
  1249. if (validPos && validPos.match.fn == null && i < pos) {
  1250. staticInputsBeforePosAlternate++;
  1251. }
  1252. }
  1253. pos = pos + (staticInputsBeforePosAlternate - staticInputsBeforePos);
  1254. isValidRslt = isValid(pos > targetLvp ? targetLvp : pos, c, strict, fromSetValid);
  1255. }
  1256. opts.keepStatic = !opts.keepStatic; //enable keepStatic on getMaskLength
  1257. if (!isValidRslt) {
  1258. resetMaskSet();
  1259. getMaskSet().validPositions = $.extend(true, {}, validPsClone);
  1260. } else return isValidRslt;
  1261. }
  1262. }
  1263. }
  1264. break;
  1265. }
  1266. }
  1267. }
  1268. return false;
  1269. }
  1270. //set alternator choice on previous skipped placeholder positions
  1271. function trackbackAlternations(originalPos, newPos) {
  1272. var vp = getMaskSet().validPositions[newPos],
  1273. targetLocator = vp.locator,
  1274. tll = targetLocator.length;
  1275. for (var ps = originalPos; ps < newPos; ps++) {
  1276. if (!isMask(ps)) {
  1277. var tests = getTests(ps),
  1278. bestMatch = tests[0],
  1279. equality = -1;
  1280. $.each(tests, function(ndx, tst) {
  1281. for (var i = 0; i < tll; i++) {
  1282. if (tst.locator[i] && checkAlternationMatch(tst.locator[i].toString().split(","), targetLocator[i].toString().split(",")) && equality < i) {
  1283. equality = i;
  1284. bestMatch = tst;
  1285. }
  1286. }
  1287. });
  1288. setValidPosition(ps, $.extend({}, bestMatch, {
  1289. "input": bestMatch.match.def
  1290. }), true);
  1291. }
  1292. }
  1293. }
  1294. //Check for a nonmask before the pos
  1295. var buffer = getBuffer();
  1296. //find previous valid
  1297. for (var pndx = pos - 1; pndx > -1; pndx--) {
  1298. if (getMaskSet().validPositions[pndx]) {
  1299. break;
  1300. }
  1301. }
  1302. ////fill missing nonmask and valid placeholders
  1303. pndx++;
  1304. for (; pndx < pos; pndx++) {
  1305. //console.log("missing " + pndx + " " + buffer[pndx] + " ismask " + isMask(pndx) + " plchldr " + getPlaceholder(pndx) + " nrt " + getTests(pndx).len);
  1306. if (getMaskSet().validPositions[pndx] === undefined && (((!isMask(pndx) || buffer[pndx] !== getPlaceholder(pndx)) && getTests(pndx).length > 1) || (buffer[pndx] === opts.radixPoint || buffer[pndx] === "0" && $.inArray(opts.radixPoint, buffer) < pndx))) //special case for decimals ~ = placeholder but yet valid input
  1307. {
  1308. //console.log("inject " + pndx + " " + buffer[pndx]);
  1309. _isValid(pndx, buffer[pndx], true);
  1310. }
  1311. }
  1312. var maskPos = pos,
  1313. result = false,
  1314. positionsClone = $.extend(true, {}, getMaskSet().validPositions); //clone the currentPositions
  1315. //if (fromSetValid && maskPos >= getMaskLength()) {
  1316. // resetMaskSet(true); //masklenght can be altered on the process => reset to get the actual length
  1317. //}
  1318. if (maskPos < getMaskLength()) {
  1319. getBuffer(); //make sure the buffer
  1320. result = _isValid(maskPos, c, strict, fromSetValid);
  1321. if ((!strict || fromSetValid) && result === false) {
  1322. var currentPosValid = getMaskSet().validPositions[maskPos];
  1323. if (currentPosValid && currentPosValid.match.fn === null && (currentPosValid.match.def === c || c === opts.skipOptionalPartCharacter)) {
  1324. result = {
  1325. "caret": seekNext(maskPos)
  1326. };
  1327. } else if ((opts.insertMode || getMaskSet().validPositions[seekNext(maskPos)] === undefined) && !isMask(maskPos)) { //does the input match on a further position?
  1328. for (var nPos = maskPos + 1, snPos = seekNext(maskPos); nPos <= snPos; nPos++) {
  1329. result = _isValid(nPos, c, strict, fromSetValid);
  1330. if (result !== false) {
  1331. trackbackAlternations(maskPos, nPos);
  1332. maskPos = nPos;
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. }
  1338. }
  1339. if (result === false && opts.keepStatic && isComplete(buffer)) { //try fuzzy alternator logic
  1340. result = alternate(pos, c, strict, fromSetValid);
  1341. }
  1342. if (result === true) {
  1343. result = {
  1344. "pos": maskPos
  1345. };
  1346. }
  1347. if ($.isFunction(opts.postValidation) && result !== false && !strict) {
  1348. resetMaskSet(true);
  1349. var postValidResult = opts.postValidation(getBuffer(), opts);
  1350. if (!postValidResult) {
  1351. resetMaskSet(true);
  1352. getMaskSet().validPositions = $.extend(true, {}, positionsClone); //revert validation changes
  1353. result = false;
  1354. } else if (postValidResult.refreshFromBuffer) {
  1355. var refresh = postValidResult.refreshFromBuffer;
  1356. refreshFromBuffer(refresh === true ? refresh : refresh.start, refresh.end, postValidResult.buffer);
  1357. resetMaskSet(true);
  1358. result = postValidResult;
  1359. }
  1360. }
  1361. return result;
  1362. }
  1363. function isMask(pos) {
  1364. var test = getTest(pos);
  1365. if (test.fn != null) {
  1366. return test.fn;
  1367. } else if (!opts.keepStatic && getMaskSet().validPositions[pos] === undefined) {
  1368. var tests = getTests(pos),
  1369. staticAlternations = true;
  1370. for (var i = 0; i < tests.length; i++) {
  1371. if (tests[i].match.def !== "" && ( /*tests[i].match.fn !== null || */ (tests[i].alternation === undefined || tests[i].locator[tests[i].alternation].length > 1))) {
  1372. staticAlternations = false;
  1373. break;
  1374. }
  1375. }
  1376. return staticAlternations;
  1377. }
  1378. return false;
  1379. }
  1380. function getMaskLength() {
  1381. var maskLength;
  1382. maxLength = $el.prop("maxLength");
  1383. if (maxLength === -1) maxLength = undefined; /* FF sets no defined max length to -1 */
  1384. var pos, lvp = getLastValidPosition(),
  1385. testPos = getMaskSet().validPositions[lvp],
  1386. ndxIntlzr = testPos !== undefined ? testPos.locator.slice() : undefined;
  1387. for (pos = lvp + 1; testPos === undefined || (testPos.match.fn !== null || (testPos.match.fn === null && testPos.match.def !== "")); pos++) {
  1388. testPos = getTestTemplate(pos, ndxIntlzr, pos - 1);
  1389. ndxIntlzr = testPos.locator.slice();
  1390. }
  1391. var lastTest = getTest(pos - 1);
  1392. maskLength = (lastTest.def !== "") ? pos : pos - 1;
  1393. return (maxLength === undefined || maskLength < maxLength) ? maskLength : maxLength;
  1394. }
  1395. function seekNext(pos, newBlock) {
  1396. var maskL = getMaskLength();
  1397. if (pos >= maskL) return maskL;
  1398. var position = pos;
  1399. while (++position < maskL && ((newBlock === true && (getTest(position).newBlockMarker !== true || !isMask(position))) || (newBlock !== true && !isMask(position) && (opts.nojumps !== true || opts.nojumpsThreshold > position)))) {}
  1400. return position;
  1401. }
  1402. function seekPrevious(pos, newBlock) {
  1403. var position = pos;
  1404. if (position <= 0) return 0;
  1405. while (--position > 0 && ((newBlock === true && getTest(position).newBlockMarker !== true) || (newBlock !== true && !isMask(position)))) {}
  1406. return position;
  1407. }
  1408. function getBufferElement(position) {
  1409. return getMaskSet().validPositions[position] === undefined ? getPlaceholder(position) : getMaskSet().validPositions[position].input;
  1410. }
  1411. function writeBuffer(input, buffer, caretPos, event, triggerInputEvent) {
  1412. if (event && $.isFunction(opts.onBeforeWrite)) {
  1413. var result = opts.onBeforeWrite.call(input, event, buffer, caretPos, opts);
  1414. if (result) {
  1415. if (result.refreshFromBuffer) {
  1416. var refresh = result.refreshFromBuffer;
  1417. refreshFromBuffer(refresh === true ? refresh : refresh.start, refresh.end, result.buffer || buffer);
  1418. resetMaskSet(true);
  1419. buffer = getBuffer();
  1420. }
  1421. caretPos = result.caret !== undefined ? result.caret : caretPos;
  1422. }
  1423. }
  1424. input.inputmask._valueSet(buffer.join(""));
  1425. if (caretPos !== undefined && (event === undefined || event.type !== "blur")) {
  1426. caret(input, caretPos);
  1427. }
  1428. if (triggerInputEvent === true) {
  1429. skipInputEvent = true;
  1430. $(input).trigger("input");
  1431. }
  1432. }
  1433. function getPlaceholder(pos, test) {
  1434. test = test || getTest(pos);
  1435. if (test.placeholder !== undefined) {
  1436. return test.placeholder;
  1437. } else if (test.fn === null) {
  1438. if (!opts.keepStatic && getMaskSet().validPositions[pos] === undefined) {
  1439. var tests = getTests(pos),
  1440. hasAlternations = false,
  1441. prevTest;
  1442. for (var i = 0; i < tests.length; i++) {
  1443. if (prevTest && tests[i].match.def !== "" && (tests[i].match.def !== prevTest.match.def && (tests[i].alternation === undefined || tests[i].alternation === prevTest.alternation))) {
  1444. hasAlternations = true;
  1445. break;
  1446. }
  1447. if (tests[i].match.optionality !== true && tests[i].match.optionalQuantifier !== true) {
  1448. prevTest = tests[i];
  1449. }
  1450. }
  1451. if (hasAlternations) {
  1452. return opts.placeholder.charAt(pos % opts.placeholder.length);
  1453. }
  1454. }
  1455. return test.def;
  1456. } else {
  1457. return opts.placeholder.charAt(pos % opts.placeholder.length);
  1458. }
  1459. }
  1460. function checkVal(input, writeOut, strict, nptvl) {
  1461. var inputValue = nptvl !== undefined ? nptvl.slice() : input.inputmask._valueGet().split(""),
  1462. charCodes = "",
  1463. initialNdx = 0;
  1464. function isTemplateMatch() {
  1465. var isMatch = false;
  1466. var charCodeNdx = getBufferTemplate().slice(initialNdx, seekNext(initialNdx)).join("").indexOf(charCodes);
  1467. if (charCodeNdx !== -1 && !isMask(initialNdx)) {
  1468. isMatch = true;
  1469. var bufferTemplateArr = getBufferTemplate().slice(initialNdx, initialNdx + charCodeNdx);
  1470. for (var i = 0; i < bufferTemplateArr.length; i++) {
  1471. if (bufferTemplateArr[i] !== " ") {
  1472. isMatch = false;
  1473. break;
  1474. }
  1475. }
  1476. }
  1477. return isMatch;
  1478. }
  1479. resetMaskSet();
  1480. getMaskSet().p = seekNext(-1);
  1481. if (writeOut) input.inputmask._valueSet(""); //initial clear
  1482. if (!strict) {
  1483. if (opts.autoUnmask !== true) {
  1484. var staticInput = getBufferTemplate().slice(0, seekNext(-1)).join(""),
  1485. matches = inputValue.join("").match(new RegExp("^" + Inputmask.escapeRegex(staticInput), "g"));
  1486. if (matches && matches.length > 0) {
  1487. inputValue.splice(0, matches.length * staticInput.length);
  1488. initialNdx = seekNext(initialNdx);
  1489. }
  1490. } else {
  1491. initialNdx = seekNext(initialNdx);
  1492. }
  1493. }
  1494. $.each(inputValue, function(ndx, charCode) {
  1495. var keypress = $.Event("keypress");
  1496. keypress.which = charCode.charCodeAt(0);
  1497. charCodes += charCode;
  1498. var lvp = getLastValidPosition(undefined, true),
  1499. lvTest = getMaskSet().validPositions[lvp],
  1500. nextTest = getTestTemplate(lvp + 1, lvTest ? lvTest.locator.slice() : undefined, lvp);
  1501. if (!isTemplateMatch() || strict || opts.autoUnmask) {
  1502. var pos = strict ? ndx : (nextTest.match.fn == null && nextTest.match.optionality && (lvp + 1) < getMaskSet().p ? lvp + 1 : getMaskSet().p);
  1503. keypressEvent.call(input, keypress, true, false, strict, pos);
  1504. initialNdx = pos + 1;
  1505. charCodes = "";
  1506. } else {
  1507. keypressEvent.call(input, keypress, true, false, true, lvp + 1);
  1508. }
  1509. });
  1510. if (writeOut) {
  1511. writeBuffer(input, getBuffer(), document.activeElement === input ? seekNext(getLastValidPosition(0)) : undefined, $.Event("checkval"));
  1512. }
  1513. }
  1514. function unmaskedvalue($input) {
  1515. if ($input[0].inputmask && !$input.hasClass("hasDatepicker")) {
  1516. var umValue = [],
  1517. vps = getMaskSet().validPositions;
  1518. for (var pndx in vps) {
  1519. if (vps[pndx].match && vps[pndx].match.fn != null) {
  1520. umValue.push(vps[pndx].input);
  1521. }
  1522. }
  1523. var unmaskedValue = umValue.length === 0 ? null : (isRTL ? umValue.reverse() : umValue).join("");
  1524. if (unmaskedValue !== null) {
  1525. var bufferValue = (isRTL ? getBuffer().slice().reverse() : getBuffer()).join("");
  1526. if ($.isFunction(opts.onUnMask)) {
  1527. unmaskedValue = (opts.onUnMask.call($input, bufferValue, unmaskedValue, opts) || unmaskedValue);
  1528. }
  1529. }
  1530. return unmaskedValue;
  1531. } else {
  1532. return $input[0].inputmask._valueGet();
  1533. }
  1534. }
  1535. function caret(input, begin, end) {
  1536. function translatePosition(pos) {
  1537. if (isRTL && typeof pos === "number" && (!opts.greedy || opts.placeholder !== "")) {
  1538. var bffrLght = getBuffer().join("").length; //join is needed because sometimes we get an empty buffer element which must not be counted for the caret position (numeric alias)
  1539. pos = bffrLght - pos;
  1540. }
  1541. return pos;
  1542. }
  1543. var range;
  1544. if (typeof begin === "number") {
  1545. begin = translatePosition(begin);
  1546. end = translatePosition(end);
  1547. end = (typeof end == "number") ? end : begin;
  1548. if (!$(input).is(":visible")) {
  1549. return;
  1550. }
  1551. var scrollCalc = input.style.fontSize.replace("px", "") * end;
  1552. input.scrollLeft = scrollCalc > input.scrollWidth ? scrollCalc : 0;
  1553. if (!androidchrome && opts.insertMode === false && begin === end) end++; //set visualization for insert/overwrite mode
  1554. if (input.setSelectionRange) {
  1555. input.selectionStart = begin;
  1556. input.selectionEnd = end;
  1557. } else if (window.getSelection) {
  1558. range = document.createRange();
  1559. if (input.firstChild === undefined) {
  1560. var textNode = document.createTextNode("");
  1561. input.appendChild(textNode);
  1562. }
  1563. range.setStart(input.firstChild, begin < input.inputmask._valueGet().length ? begin : input.inputmask._valueGet().length);
  1564. range.setEnd(input.firstChild, end < input.inputmask._valueGet().length ? end : input.inputmask._valueGet().length);
  1565. range.collapse(true);
  1566. var sel = window.getSelection();
  1567. sel.removeAllRanges();
  1568. sel.addRange(range);
  1569. //input.focus();
  1570. } else if (input.createTextRange) {
  1571. range = input.createTextRange();
  1572. range.collapse(true);
  1573. range.moveEnd("character", end);
  1574. range.moveStart("character", begin);
  1575. range.select();
  1576. }
  1577. } else {
  1578. if (input.setSelectionRange) {
  1579. begin = input.selectionStart;
  1580. end = input.selectionEnd;
  1581. } else if (window.getSelection) {
  1582. range = window.getSelection().getRangeAt(0);
  1583. if (range.commonAncestorContainer.parentNode === input || range.commonAncestorContainer === input) {
  1584. begin = range.startOffset;
  1585. end = range.endOffset;
  1586. }
  1587. } else if (document.selection && document.selection.createRange) {
  1588. range = document.selection.createRange();
  1589. begin = 0 - range.duplicate().moveStart("character", -100000);
  1590. end = begin + range.text.length;
  1591. }
  1592. /*eslint-disable consistent-return */
  1593. return {
  1594. "begin": translatePosition(begin),
  1595. "end": translatePosition(end)
  1596. };
  1597. /*eslint-enable consistent-return */
  1598. }
  1599. }
  1600. function determineLastRequiredPosition(returnDefinition) {
  1601. var buffer = getBuffer(),
  1602. bl = buffer.length,
  1603. pos, lvp = getLastValidPosition(),
  1604. positions = {},
  1605. lvTest = getMaskSet().validPositions[lvp],
  1606. ndxIntlzr = lvTest !== undefined ? lvTest.locator.slice() : undefined,
  1607. testPos;
  1608. for (pos = lvp + 1; pos < buffer.length; pos++) {
  1609. testPos = getTestTemplate(pos, ndxIntlzr, pos - 1);
  1610. ndxIntlzr = testPos.locator.slice();
  1611. positions[pos] = $.extend(true, {}, testPos);
  1612. }
  1613. var lvTestAlt = lvTest && lvTest.alternation !== undefined ? lvTest.locator[lvTest.alternation] : undefined;
  1614. for (pos = bl - 1; pos > lvp; pos--) {
  1615. testPos = positions[pos];
  1616. if ((testPos.match.optionality ||
  1617. testPos.match.optionalQuantifier ||
  1618. (lvTestAlt && ((lvTestAlt !== positions[pos].locator[lvTest.alternation] && testPos.match.fn != null) ||
  1619. (testPos.match.fn === null && testPos.locator[lvTest.alternation] && checkAlternationMatch(testPos.locator[lvTest.alternation].toString().split(","), lvTestAlt.toString().split(",")) && getTests(pos)[0].def !== "")))) && buffer[pos] === getPlaceholder(pos, testPos.match)) {
  1620. bl--;
  1621. } else break;
  1622. }
  1623. return returnDefinition ? {
  1624. "l": bl,
  1625. "def": positions[bl] ? positions[bl].match : undefined
  1626. } : bl;
  1627. }
  1628. function clearOptionalTail(buffer) {
  1629. var rl = determineLastRequiredPosition(),
  1630. lmib = buffer.length - 1;
  1631. for (; lmib > rl; lmib--) {
  1632. if (isMask(lmib)) break; //fixme ismask is not good enough
  1633. }
  1634. buffer.splice(rl, lmib + 1 - rl);
  1635. return buffer;
  1636. }
  1637. function isComplete(buffer) { //return true / false / undefined (repeat *)
  1638. if ($.isFunction(opts.isComplete)) return opts.isComplete.call($el, buffer, opts);
  1639. if (opts.repeat === "*") return undefined;
  1640. var complete = false,
  1641. lrp = determineLastRequiredPosition(true),
  1642. aml = seekPrevious(lrp.l);
  1643. if (lrp.def === undefined || lrp.def.newBlockMarker || lrp.def.optionality || lrp.def.optionalQuantifier) {
  1644. complete = true;
  1645. for (var i = 0; i <= aml; i++) {
  1646. var test = getTestTemplate(i).match;
  1647. if ((test.fn !== null && getMaskSet().validPositions[i] === undefined && test.optionality !== true && test.optionalQuantifier !== true) || (test.fn === null && buffer[i] !== getPlaceholder(i, test))) {
  1648. complete = false;
  1649. break;
  1650. }
  1651. }
  1652. }
  1653. return complete;
  1654. }
  1655. function isSelection(begin, end) {
  1656. return isRTL ? (begin - end) > 1 || ((begin - end) === 1 && opts.insertMode) :
  1657. (end - begin) > 1 || ((end - begin) === 1 && opts.insertMode);
  1658. }
  1659. function installEventRuler(npt) {
  1660. var events = $._data(npt).events,
  1661. inComposition = false;
  1662. $.each(events, function(eventType, eventHandlers) {
  1663. $.each(eventHandlers, function(ndx, eventHandler) {
  1664. if (eventHandler.namespace === "inputmask") {
  1665. var handler = eventHandler.handler;
  1666. eventHandler.handler = function(e) {
  1667. // console.log("triggered " + e.type);
  1668. if (this.inputmask === undefined) { //happens when cloning an object with jquery.clone
  1669. var imOpts = $.data(this, "_inputmask_opts");
  1670. if (imOpts)(new Inputmask(imOpts)).mask(this);
  1671. else $(this).unbind(".inputmask");
  1672. } else if (e.type !== "setvalue" && (this.disabled || (this.readOnly && !(e.type === "keydown" && (e.ctrlKey && e.keyCode === 67) || (opts.tabThrough === false && e.keyCode === Inputmask.keyCode.TAB))))) {
  1673. e.preventDefault();
  1674. } else {
  1675. switch (e.type) {
  1676. case "input":
  1677. if (skipInputEvent === true || inComposition === true) {
  1678. skipInputEvent = false;
  1679. return e.preventDefault();
  1680. }
  1681. break;
  1682. case "keydown":
  1683. //Safari 5.1.x - modal dialog fires keypress twice workaround
  1684. skipKeyPressEvent = false;
  1685. inComposition = false;
  1686. break;
  1687. case "keypress":
  1688. if (skipKeyPressEvent === true) {
  1689. return e.preventDefault();
  1690. }
  1691. skipKeyPressEvent = true;
  1692. break;
  1693. case "compositionstart":
  1694. inComposition = true;
  1695. break;
  1696. case "compositionupdate":
  1697. skipInputEvent = true;
  1698. break;
  1699. case "compositionend":
  1700. inComposition = false;
  1701. break;
  1702. }
  1703. //console.log("executed " + e.type);
  1704. return handler.apply(this, arguments);
  1705. }
  1706. };
  1707. }
  1708. });
  1709. });
  1710. }
  1711. function patchValueProperty(npt) {
  1712. var valueGet;
  1713. var valueSet;
  1714. function patchValhook(type) {
  1715. if ($.valHooks && $.valHooks[type] === undefined || $.valHooks[type].inputmaskpatch !== true) {
  1716. var valhookGet = $.valHooks[type] && $.valHooks[type].get ? $.valHooks[type].get : function(elem) {
  1717. return elem.value;
  1718. };
  1719. var valhookSet = $.valHooks[type] && $.valHooks[type].set ? $.valHooks[type].set : function(elem, value) {
  1720. elem.value = value;
  1721. return elem;
  1722. };
  1723. $.valHooks[type] = {
  1724. get: function(elem) {
  1725. if (elem.inputmask) {
  1726. if (elem.inputmask.opts.autoUnmask) {
  1727. return elem.inputmask.unmaskedvalue();
  1728. } else {
  1729. var result = valhookGet(elem),
  1730. maskset = elem.inputmask.maskset,
  1731. bufferTemplate = maskset._buffer;
  1732. bufferTemplate = bufferTemplate ? bufferTemplate.join("") : "";
  1733. return result !== bufferTemplate ? result : "";
  1734. }
  1735. } else return valhookGet(elem);
  1736. },
  1737. set: function(elem, value) {
  1738. var $elem = $(elem),
  1739. result;
  1740. result = valhookSet(elem, value);
  1741. if (elem.inputmask) {
  1742. $elem.triggerHandler("setvalue.inputmask");
  1743. }
  1744. return result;
  1745. },
  1746. inputmaskpatch: true
  1747. };
  1748. }
  1749. }
  1750. function getter() {
  1751. if (this.inputmask) {
  1752. return this.inputmask.opts.autoUnmask ? this.inputmask.unmaskedvalue() : (valueGet.call(this) !== getBufferTemplate().join("") ? valueGet.call(this) : "");
  1753. } else return valueGet.call(this);
  1754. }
  1755. function setter(value) {
  1756. valueSet.call(this, value);
  1757. if (this.inputmask) {
  1758. $(this).triggerHandler("setvalue.inputmask");
  1759. }
  1760. }
  1761. function installNativeValueSetFallback(npt) {
  1762. $(npt).bind("mouseenter.inputmask", function(event) {
  1763. var $input = $(this),
  1764. input = this,
  1765. value = input.inputmask._valueGet();
  1766. if (value !== "" && value !== getBuffer().join("")) {
  1767. $input.triggerHandler("setvalue.inputmask");
  1768. }
  1769. });
  1770. //!! the bound handlers are executed in the order they where bound
  1771. //reorder the events - the mouseenter event is internally mapped to the mouseover event
  1772. var events = $._data(npt).events;
  1773. var handlers = events.mouseover;
  1774. if (handlers) {
  1775. var ourHandler = handlers[handlers.length - 1];
  1776. for (var i = handlers.length - 1; i > 0; i--) {
  1777. handlers[i] = handlers[i - 1];
  1778. }
  1779. handlers[0] = ourHandler;
  1780. }
  1781. }
  1782. if (!npt.inputmask.__valueGet) {
  1783. if (Object.getOwnPropertyDescriptor && npt.value === undefined) { // && npt.isContentEditable) {
  1784. valueGet = function() {
  1785. return this.textContent;
  1786. };
  1787. valueSet = function(value) {
  1788. this.textContent = value;
  1789. };
  1790. Object.defineProperty(npt, "value", {
  1791. get: getter,
  1792. set: setter
  1793. });
  1794. } else if (document.__lookupGetter__ && npt.__lookupGetter__("value")) {
  1795. valueGet = npt.__lookupGetter__("value");
  1796. valueSet = npt.__lookupSetter__("value");
  1797. npt.__defineGetter__("value", getter);
  1798. npt.__defineSetter__("value", setter);
  1799. } else { //jquery.val
  1800. valueGet = function() {
  1801. return npt.value;
  1802. };
  1803. valueSet = function(value) {
  1804. npt.value = value;
  1805. };
  1806. patchValhook(npt.type);
  1807. installNativeValueSetFallback(npt);
  1808. }
  1809. npt.inputmask.__valueGet = valueGet; //store native property getter
  1810. npt.inputmask._valueGet = function(overruleRTL) {
  1811. return isRTL && overruleRTL !== true ? valueGet.call(this.el).split("").reverse().join("") : valueGet.call(this.el);
  1812. };
  1813. npt.inputmask.__valueSet = valueSet; //store native property setter
  1814. npt.inputmask._valueSet = function(value, overruleRTL) {
  1815. valueSet.call(this.el, (overruleRTL !== true && isRTL) ? value.split("").reverse().join("") : value);
  1816. };
  1817. }
  1818. }
  1819. function handleRemove(input, k, pos, strict) {
  1820. function generalize() {
  1821. if (opts.keepStatic) {
  1822. resetMaskSet(true);
  1823. var validInputs = [],
  1824. lastAlt, positionsClone = $.extend(true, {}, getMaskSet().validPositions);
  1825. //find last alternation
  1826. for (lastAlt = getLastValidPosition(); lastAlt >= 0; lastAlt--) {
  1827. var validPos = getMaskSet().validPositions[lastAlt];
  1828. if (validPos) {
  1829. if (validPos.match.fn != null) {
  1830. validInputs.push(validPos.input);
  1831. }
  1832. delete getMaskSet().validPositions[lastAlt];
  1833. if (validPos.alternation !== undefined && validPos.locator[validPos.alternation] === getTestTemplate(lastAlt).locator[validPos.alternation]) {
  1834. break;
  1835. }
  1836. }
  1837. }
  1838. if (lastAlt > -1) {
  1839. while (validInputs.length > 0) {
  1840. getMaskSet().p = seekNext(getLastValidPosition());
  1841. var keypress = $.Event("keypress");
  1842. keypress.which = validInputs.pop().charCodeAt(0);
  1843. keypressEvent.call(input, keypress, true, false, false, getMaskSet().p);
  1844. }
  1845. } else getMaskSet().validPositions = $.extend(true, {}, positionsClone); //restore original positions
  1846. }
  1847. }
  1848. if (opts.numericInput || isRTL) {
  1849. if (k === Inputmask.keyCode.BACKSPACE) {
  1850. k = Inputmask.keyCode.DELETE;
  1851. } else if (k === Inputmask.keyCode.DELETE) {
  1852. k = Inputmask.keyCode.BACKSPACE;
  1853. }
  1854. if (isRTL) {
  1855. var pend = pos.end;
  1856. pos.end = pos.begin;
  1857. pos.begin = pend;
  1858. }
  1859. }
  1860. if (k === Inputmask.keyCode.BACKSPACE && (pos.end - pos.begin < 1 || opts.insertMode === false)) {
  1861. pos.begin = seekPrevious(pos.begin);
  1862. if (getMaskSet().validPositions[pos.begin] !== undefined && (getMaskSet().validPositions[pos.begin].input === opts.groupSeparator || getMaskSet().validPositions[pos.begin].input === opts.radixPoint)) {
  1863. pos.begin--;
  1864. }
  1865. } else if (k === Inputmask.keyCode.DELETE && pos.begin === pos.end) {
  1866. pos.end = isMask(pos.end) ? pos.end + 1 : seekNext(pos.end) + 1;
  1867. if (getMaskSet().validPositions[pos.begin] !== undefined && (getMaskSet().validPositions[pos.begin].input === opts.groupSeparator || getMaskSet().validPositions[pos.begin].input === opts.radixPoint)) {
  1868. pos.end++;
  1869. }
  1870. }
  1871. stripValidPositions(pos.begin, pos.end, false, strict);
  1872. if (strict !== true) {
  1873. generalize(); //revert the alternation
  1874. }
  1875. var lvp = getLastValidPosition(pos.begin);
  1876. if (lvp < pos.begin) {
  1877. if (lvp === -1) resetMaskSet();
  1878. getMaskSet().p = seekNext(lvp);
  1879. } else if (strict !== true) {
  1880. getMaskSet().p = pos.begin;
  1881. }
  1882. }
  1883. function keydownEvent(e) {
  1884. var input = this,
  1885. $input = $(input),
  1886. k = e.keyCode,
  1887. pos = caret(input);
  1888. //backspace, delete, and escape get special treatment
  1889. if (k === Inputmask.keyCode.BACKSPACE || k === Inputmask.keyCode.DELETE || (iphone && k === 127) || (e.ctrlKey && k === 88 && !isInputEventSupported("cut"))) { //backspace/delete
  1890. e.preventDefault(); //stop default action but allow propagation
  1891. if (k === 88) undoValue = getBuffer().join("");
  1892. handleRemove(input, k, pos);
  1893. writeBuffer(input, getBuffer(), getMaskSet().p, e, undoValue !== getBuffer().join(""));
  1894. if (input.inputmask._valueGet() === getBufferTemplate().join("")) {
  1895. $input.trigger("cleared");
  1896. } else if (isComplete(getBuffer()) === true) {
  1897. $input.trigger("complete");
  1898. }
  1899. if (opts.showTooltip) { //update tooltip
  1900. $input.prop("title", getMaskSet().mask);
  1901. }
  1902. } else if (k === Inputmask.keyCode.END || k === Inputmask.keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch
  1903. setTimeout(function() {
  1904. var caretPos = seekNext(getLastValidPosition());
  1905. if (!opts.insertMode && caretPos === getMaskLength() && !e.shiftKey) caretPos--;
  1906. caret(input, e.shiftKey ? pos.begin : caretPos, caretPos);
  1907. }, 0);
  1908. } else if ((k === Inputmask.keyCode.HOME && !e.shiftKey) || k === Inputmask.keyCode.PAGE_UP) { //Home or page_up
  1909. caret(input, 0, e.shiftKey ? pos.begin : 0);
  1910. } else if (((opts.undoOnEscape && k === Inputmask.keyCode.ESCAPE) || (k === 90 && e.ctrlKey)) && e.altKey !== true) { //escape && undo && #762
  1911. checkVal(input, true, false, undoValue.split(""));
  1912. $input.click();
  1913. } else if (k === Inputmask.keyCode.INSERT && !(e.shiftKey || e.ctrlKey)) { //insert
  1914. opts.insertMode = !opts.insertMode;
  1915. caret(input, !opts.insertMode && pos.begin === getMaskLength() ? pos.begin - 1 : pos.begin);
  1916. } else if (opts.tabThrough === true && k === Inputmask.keyCode.TAB) {
  1917. if (e.shiftKey === true) {
  1918. if (getTest(pos.begin).fn === null) {
  1919. pos.begin = seekNext(pos.begin);
  1920. }
  1921. pos.end = seekPrevious(pos.begin, true);
  1922. pos.begin = seekPrevious(pos.end, true);
  1923. } else {
  1924. pos.begin = seekNext(pos.begin, true);
  1925. pos.end = seekNext(pos.begin, true);
  1926. if (pos.end < getMaskLength()) pos.end--;
  1927. }
  1928. if (pos.begin < getMaskLength()) {
  1929. e.preventDefault();
  1930. caret(input, pos.begin, pos.end);
  1931. }
  1932. } else if (opts.insertMode === false && !e.shiftKey) {
  1933. if (k === Inputmask.keyCode.RIGHT) {
  1934. setTimeout(function() {
  1935. var caretPos = caret(input);
  1936. caret(input, caretPos.begin);
  1937. }, 0);
  1938. } else if (k === Inputmask.keyCode.LEFT) {
  1939. setTimeout(function() {
  1940. var caretPos = caret(input);
  1941. caret(input, isRTL ? caretPos.begin + 1 : caretPos.begin - 1);
  1942. }, 0);
  1943. }
  1944. }
  1945. opts.onKeyDown.call(this, e, getBuffer(), caret(input).begin, opts);
  1946. ignorable = $.inArray(k, opts.ignorables) !== -1;
  1947. }
  1948. function keypressEvent(e, checkval, writeOut, strict, ndx) {
  1949. var input = this,
  1950. $input = $(input),
  1951. k = e.which || e.charCode || e.keyCode;
  1952. if (checkval !== true && (!(e.ctrlKey && e.altKey) && (e.ctrlKey || e.metaKey || ignorable))) {
  1953. if (k === Inputmask.keyCode.ENTER && undoValue !== getBuffer().join("")) {
  1954. setTimeout(function() {
  1955. $input.change();
  1956. undoValue = getBuffer().join("");
  1957. }, 0);
  1958. }
  1959. return true;
  1960. } else {
  1961. if (k) {
  1962. //special treat the decimal separator
  1963. if (k === 46 && e.shiftKey === false && opts.radixPoint === ",") k = 44;
  1964. var pos = checkval ? {
  1965. begin: ndx,
  1966. end: ndx
  1967. } : caret(input),
  1968. forwardPosition, c = String.fromCharCode(k);
  1969. //should we clear a possible selection??
  1970. var isSlctn = isSelection(pos.begin, pos.end);
  1971. if (isSlctn) {
  1972. getMaskSet().undoPositions = $.extend(true, {}, getMaskSet().validPositions); //init undobuffer for recovery when not valid
  1973. handleRemove(input, Inputmask.keyCode.DELETE, pos, true);
  1974. pos.begin = getMaskSet().p;
  1975. if (!opts.insertMode) { //preserve some space
  1976. opts.insertMode = !opts.insertMode;
  1977. setValidPosition(pos.begin, strict);
  1978. opts.insertMode = !opts.insertMode;
  1979. }
  1980. isSlctn = !opts.multi;
  1981. }
  1982. getMaskSet().writeOutBuffer = true;
  1983. var p = isRTL && !isSlctn ? pos.end : pos.begin;
  1984. var valResult = isValid(p, c, strict);
  1985. if (valResult !== false) {
  1986. if (valResult !== true) {
  1987. p = valResult.pos !== undefined ? valResult.pos : p; //set new position from isValid
  1988. c = valResult.c !== undefined ? valResult.c : c; //set new char from isValid
  1989. }
  1990. resetMaskSet(true);
  1991. if (valResult.caret !== undefined) {
  1992. forwardPosition = valResult.caret;
  1993. } else {
  1994. var vps = getMaskSet().validPositions;
  1995. if (!opts.keepStatic && (vps[p + 1] !== undefined && getTests(p + 1, vps[p].locator.slice(), p).length > 1 || vps[p].alternation !== undefined)) {
  1996. forwardPosition = p + 1;
  1997. } else forwardPosition = seekNext(p);
  1998. }
  1999. getMaskSet().p = forwardPosition; //needed for checkval
  2000. }
  2001. if (writeOut !== false) {
  2002. var self = this;
  2003. setTimeout(function() {
  2004. opts.onKeyValidation.call(self, valResult, opts);
  2005. }, 0);
  2006. if (getMaskSet().writeOutBuffer && valResult !== false) {
  2007. var buffer = getBuffer();
  2008. writeBuffer(input, buffer, checkval ? undefined : opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition, e, checkval !== true);
  2009. if (checkval !== true) {
  2010. setTimeout(function() { //timeout needed for IE
  2011. if (isComplete(buffer) === true) $input.trigger("complete");
  2012. }, 0);
  2013. }
  2014. } else if (isSlctn) {
  2015. getMaskSet().buffer = undefined;
  2016. getMaskSet().validPositions = getMaskSet().undoPositions;
  2017. }
  2018. } else if (isSlctn) {
  2019. getMaskSet().buffer = undefined;
  2020. getMaskSet().validPositions = getMaskSet().undoPositions;
  2021. }
  2022. if (opts.showTooltip) { //update tooltip
  2023. $input.prop("title", getMaskSet().mask);
  2024. }
  2025. if (checkval && $.isFunction(opts.onBeforeWrite)) {
  2026. var result = opts.onBeforeWrite.call(this, e, getBuffer(), forwardPosition, opts);
  2027. if (result && result.refreshFromBuffer) {
  2028. var refresh = result.refreshFromBuffer;
  2029. refreshFromBuffer(refresh === true ? refresh : refresh.start, refresh.end, result.buffer);
  2030. resetMaskSet(true);
  2031. if (result.caret) {
  2032. getMaskSet().p = result.caret;
  2033. }
  2034. }
  2035. }
  2036. e.preventDefault();
  2037. if (checkval) {
  2038. return valResult;
  2039. }
  2040. }
  2041. }
  2042. }
  2043. function pasteEvent(e) {
  2044. var input = this,
  2045. $input = $(input),
  2046. inputValue = input.inputmask._valueGet(true),
  2047. caretPos = caret(input);
  2048. //paste event for IE8 and lower I guess ;-)
  2049. if (e.type === "propertychange" && input.inputmask._valueGet().length <= getMaskLength()) {
  2050. return true;
  2051. } else if (e.type === "paste") {
  2052. var valueBeforeCaret = inputValue.substr(0, caretPos.begin),
  2053. valueAfterCaret = inputValue.substr(caretPos.end, inputValue.length);
  2054. if (valueBeforeCaret === getBufferTemplate().slice(0, caretPos.begin).join("")) valueBeforeCaret = "";
  2055. if (valueAfterCaret === getBufferTemplate().slice(caretPos.end).join("")) valueAfterCaret = "";
  2056. if (window.clipboardData && window.clipboardData.getData) { // IE
  2057. inputValue = valueBeforeCaret + window.clipboardData.getData("Text") + valueAfterCaret;
  2058. } else if (e.originalEvent && e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData) {
  2059. inputValue = valueBeforeCaret + e.originalEvent.clipboardData.getData("text/plain") + valueAfterCaret;
  2060. }
  2061. }
  2062. var pasteValue = inputValue;
  2063. if ($.isFunction(opts.onBeforePaste)) {
  2064. pasteValue = opts.onBeforePaste.call(input, inputValue, opts);
  2065. if (pasteValue === false) {
  2066. e.preventDefault();
  2067. return false;
  2068. }
  2069. if (!pasteValue) {
  2070. pasteValue = inputValue;
  2071. }
  2072. }
  2073. checkVal(input, false, false, isRTL ? pasteValue.split("").reverse() : pasteValue.toString().split(""));
  2074. writeBuffer(input, getBuffer(), undefined, e, true);
  2075. $input.click();
  2076. if (isComplete(getBuffer()) === true) {
  2077. $input.trigger("complete");
  2078. }
  2079. return false;
  2080. }
  2081. function inputFallBackEvent(e) { //fallback when keypress & compositionevents fail
  2082. var input = this;
  2083. checkVal(input, true, false);
  2084. if (isComplete(getBuffer()) === true) {
  2085. $(input).trigger("complete");
  2086. }
  2087. e.preventDefault();
  2088. }
  2089. function compositionStartEvent(e) {
  2090. var input = this;
  2091. undoValue = getBuffer().join("");
  2092. if (compositionData === "" || e.originalEvent.data.indexOf(compositionData) !== 0) {
  2093. compositionCaretPos = caret(input);
  2094. }
  2095. }
  2096. function compositionUpdateEvent(e) {
  2097. var input = this,
  2098. caretPos = caret(input);
  2099. if (e.originalEvent.data.indexOf(compositionData) === 0) {
  2100. resetMaskSet();
  2101. caretPos = compositionCaretPos;
  2102. }
  2103. var newData = e.originalEvent.data;
  2104. caret(input, caretPos.begin, caretPos.end);
  2105. for (var i = 0; i < newData.length; i++) {
  2106. var keypress = $.Event("keypress");
  2107. keypress.which = newData.charCodeAt(i);
  2108. skipKeyPressEvent = false;
  2109. ignorable = false;
  2110. keypressEvent.call(input, keypress); //needs update
  2111. }
  2112. setTimeout(function() {
  2113. var forwardPosition = getMaskSet().p;
  2114. writeBuffer(input, getBuffer(), opts.numericInput ? seekPrevious(forwardPosition) : forwardPosition);
  2115. }, 0);
  2116. compositionData = e.originalEvent.data;
  2117. }
  2118. function compositionEndEvent(e) {
  2119. //pickup by inputfallback
  2120. }
  2121. function setValueEvent(e) {
  2122. var input = this,
  2123. value = input.inputmask._valueGet();
  2124. input.inputmask._valueSet($.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call(input, value, opts) || value) : value);
  2125. checkVal(input, true, false);
  2126. undoValue = getBuffer().join("");
  2127. if ((opts.clearMaskOnLostFocus || opts.clearIncomplete) && input.inputmask._valueGet() === getBufferTemplate().join("")) {
  2128. input.inputmask._valueSet("");
  2129. }
  2130. }
  2131. function focusEvent(e) {
  2132. var input = this,
  2133. nptValue = input.inputmask._valueGet();
  2134. if (opts.showMaskOnFocus && (!opts.showMaskOnHover || (opts.showMaskOnHover && nptValue === ""))) {
  2135. if (input.inputmask._valueGet() !== getBuffer().join("")) {
  2136. writeBuffer(input, getBuffer(), seekNext(getLastValidPosition()));
  2137. }
  2138. } else if (mouseEnter === false) { //only executed on focus without mouseenter
  2139. caret(input, seekNext(getLastValidPosition()));
  2140. }
  2141. if (opts.positionCaretOnTab === true) {
  2142. setTimeout(function() {
  2143. caret(input, seekNext(getLastValidPosition()));
  2144. }, 0);
  2145. }
  2146. undoValue = getBuffer().join("");
  2147. }
  2148. function mouseleaveEvent(e) {
  2149. var $input = $(this),
  2150. input = this;
  2151. mouseEnter = false;
  2152. if (opts.clearMaskOnLostFocus) {
  2153. var buffer = getBuffer().slice(),
  2154. nptValue = input.inputmask._valueGet();
  2155. if (document.activeElement !== input && nptValue !== $input.attr("placeholder") && nptValue !== "") {
  2156. if (getLastValidPosition() === -1 && nptValue === getBufferTemplate().join("")) {
  2157. buffer = [];
  2158. } else { //clearout optional tail of the mask
  2159. clearOptionalTail(buffer);
  2160. }
  2161. writeBuffer(input, buffer);
  2162. }
  2163. }
  2164. }
  2165. function clickEvent(e) {
  2166. function doRadixFocus(clickPos) {
  2167. if (opts.radixFocus && opts.radixPoint !== "") {
  2168. var vps = getMaskSet().validPositions;
  2169. if (vps[clickPos] === undefined || (vps[clickPos].input === getPlaceholder(clickPos))) {
  2170. if (clickPos < seekNext(-1)) return true;
  2171. var radixPos = $.inArray(opts.radixPoint, getBuffer());
  2172. if (radixPos !== -1) {
  2173. for (var vp in vps) {
  2174. if (radixPos < vp && vps[vp].input !== getPlaceholder(vp)) {
  2175. return false;
  2176. }
  2177. }
  2178. return true;
  2179. }
  2180. }
  2181. }
  2182. return false;
  2183. }
  2184. var input = this;
  2185. if (document.activeElement === input) {
  2186. var selectedCaret = caret(input);
  2187. if (selectedCaret.begin === selectedCaret.end) {
  2188. if (doRadixFocus(selectedCaret.begin)) {
  2189. caret(input, $.inArray(opts.radixPoint, getBuffer()));
  2190. } else {
  2191. var clickPosition = selectedCaret.begin,
  2192. lastPosition = seekNext(getLastValidPosition(clickPosition));
  2193. if (clickPosition < lastPosition) {
  2194. caret(input, !isMask(clickPosition) && !isMask(clickPosition - 1) ? seekNext(clickPosition) : clickPosition);
  2195. } else {
  2196. caret(input, opts.numericInput ? 0 : lastPosition);
  2197. }
  2198. }
  2199. }
  2200. }
  2201. }
  2202. function dblclickEvent(e) {
  2203. var input = this;
  2204. setTimeout(function() {
  2205. caret(input, 0, seekNext(getLastValidPosition()));
  2206. }, 0);
  2207. }
  2208. function cutEvent(e) {
  2209. skipInputEvent = true; //stop inputFallback
  2210. var input = this,
  2211. $input = $(input),
  2212. pos = caret(input);
  2213. //correct clipboardData
  2214. if (isRTL) {
  2215. var clipboardData = window.clipboardData || e.originalEvent.clipboardData,
  2216. clipData = clipboardData.getData("text").split("").reverse().join("");
  2217. clipboardData.setData("text", clipData);
  2218. }
  2219. handleRemove(input, Inputmask.keyCode.DELETE, pos);
  2220. writeBuffer(input, getBuffer(), getMaskSet().p, e, undoValue !== getBuffer().join(""));
  2221. if (input.inputmask._valueGet() === getBufferTemplate().join("")) {
  2222. $input.trigger("cleared");
  2223. }
  2224. if (opts.showTooltip) { //update tooltip
  2225. input.title = getMaskSet().mask;
  2226. }
  2227. }
  2228. function blurEvent(e) {
  2229. var $input = $(this),
  2230. input = this;
  2231. if (input.inputmask) {
  2232. var nptValue = input.inputmask._valueGet(),
  2233. buffer = getBuffer().slice();
  2234. if (undoValue !== buffer.join("")) {
  2235. setTimeout(function() { //change event should be triggered after the other buffer manipulations on blur
  2236. $input.change();
  2237. undoValue = buffer.join("");
  2238. }, 0);
  2239. }
  2240. if (nptValue !== "") {
  2241. if (opts.clearMaskOnLostFocus) {
  2242. if (getLastValidPosition() === -1 && nptValue === getBufferTemplate().join("")) {
  2243. buffer = [];
  2244. } else { //clearout optional tail of the mask
  2245. clearOptionalTail(buffer);
  2246. }
  2247. }
  2248. if (isComplete(buffer) === false) {
  2249. setTimeout(function() {
  2250. $input.trigger("incomplete");
  2251. }, 0);
  2252. if (opts.clearIncomplete) {
  2253. resetMaskSet();
  2254. if (opts.clearMaskOnLostFocus) {
  2255. buffer = [];
  2256. } else {
  2257. buffer = getBufferTemplate().slice();
  2258. }
  2259. }
  2260. }
  2261. writeBuffer(input, buffer, undefined, e);
  2262. }
  2263. }
  2264. }
  2265. function mouseenterEvent(e) {
  2266. var input = this;
  2267. mouseEnter = true;
  2268. if (document.activeElement !== input && opts.showMaskOnHover) {
  2269. if (input.inputmask._valueGet() !== getBuffer().join("")) {
  2270. writeBuffer(input, getBuffer());
  2271. }
  2272. }
  2273. }
  2274. function mask(el) {
  2275. $el = $(el);
  2276. //show tooltip
  2277. if (opts.showTooltip) {
  2278. $el.prop("title", getMaskSet().mask);
  2279. }
  2280. if (el.dir === "rtl" || opts.rightAlign) {
  2281. el.style.textAlign = "right";
  2282. }
  2283. if (el.dir === "rtl" || opts.numericInput) {
  2284. el.dir = "ltr";
  2285. el.removeAttribute("dir");
  2286. el.inputmask.isRTL = true;
  2287. isRTL = true;
  2288. }
  2289. //unbind all events - to make sure that no other mask will interfere when re-masking
  2290. $el.unbind(".inputmask");
  2291. if ((el.tagName === "INPUT" && isInputTypeSupported(el.getAttribute("type"))) || el.isContentEditable) {
  2292. //bind events
  2293. $el.closest("form").bind("submit", function() { //trigger change on submit if any
  2294. if (undoValue !== getBuffer().join("")) {
  2295. $el.change();
  2296. }
  2297. if (opts.clearMaskOnLostFocus && getLastValidPosition() === -1 && el.inputmask._valueGet && el.inputmask._valueGet() === getBufferTemplate().join("")) {
  2298. el.inputmask._valueSet(""); //clear masktemplete on submit and still has focus
  2299. }
  2300. if (opts.removeMaskOnSubmit) {
  2301. el.inputmask._valueSet(el.inputmask.unmaskedvalue(), true);
  2302. setTimeout(function() {
  2303. writeBuffer(el, getBuffer());
  2304. }, 0);
  2305. }
  2306. }).bind("reset", function() {
  2307. setTimeout(function() {
  2308. $el.triggerHandler("setvalue.inputmask");
  2309. }, 0);
  2310. });
  2311. $el.bind("mouseenter.inputmask", mouseenterEvent)
  2312. .bind("blur.inputmask", blurEvent)
  2313. .bind("focus.inputmask", focusEvent)
  2314. .bind("mouseleave.inputmask", mouseleaveEvent)
  2315. .bind("click.inputmask", clickEvent)
  2316. .bind("dblclick.inputmask", dblclickEvent)
  2317. .bind(PasteEventType + ".inputmask dragdrop.inputmask drop.inputmask", pasteEvent)
  2318. .bind("cut.inputmask", cutEvent)
  2319. .bind("complete.inputmask", opts.oncomplete)
  2320. .bind("incomplete.inputmask", opts.onincomplete)
  2321. .bind("cleared.inputmask", opts.oncleared)
  2322. .bind("keydown.inputmask", keydownEvent)
  2323. .bind("keypress.inputmask", keypressEvent);
  2324. if (!androidfirefox) {
  2325. $el.bind("compositionstart.inputmask", compositionStartEvent).bind("compositionupdate.inputmask", compositionUpdateEvent).bind("compositionend.inputmask", compositionEndEvent);
  2326. }
  2327. if (PasteEventType === "paste") {
  2328. $el.bind("input.inputmask", inputFallBackEvent);
  2329. }
  2330. //if (android || androidfirefox || androidchrome || kindle) {
  2331. // $el.unbind("input.inputmask");
  2332. // $el.bind("input.inputmask", mobileInputEvent);
  2333. //}
  2334. }
  2335. $el.bind("setvalue.inputmask", setValueEvent);
  2336. patchValueProperty(el);
  2337. //apply mask
  2338. var initialValue = $.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call(el, el.inputmask._valueGet(), opts) || el.inputmask._valueGet()) : el.inputmask._valueGet();
  2339. checkVal(el, true, false, initialValue.split(""));
  2340. var buffer = getBuffer().slice();
  2341. undoValue = buffer.join("");
  2342. // Wrap document.activeElement in a try/catch block since IE9 throw "Unspecified error" if document.activeElement is undefined when we are in an IFrame.
  2343. var activeElement;
  2344. try {
  2345. activeElement = document.activeElement;
  2346. } catch (e) {}
  2347. if (isComplete(buffer) === false) {
  2348. if (opts.clearIncomplete) {
  2349. resetMaskSet();
  2350. }
  2351. }
  2352. if (opts.clearMaskOnLostFocus) {
  2353. if (buffer.join("") === getBufferTemplate().join("")) {
  2354. buffer = [];
  2355. } else {
  2356. clearOptionalTail(buffer);
  2357. }
  2358. }
  2359. writeBuffer(el, buffer);
  2360. if (activeElement === el) { //position the caret when in focus
  2361. caret(el, seekNext(getLastValidPosition()));
  2362. }
  2363. installEventRuler(el);
  2364. }
  2365. //action object
  2366. var valueBuffer;
  2367. if (actionObj !== undefined) {
  2368. switch (actionObj.action) {
  2369. case "isComplete":
  2370. el = actionObj.el;
  2371. $el = $(el);
  2372. maskset = el.inputmask.maskset;
  2373. opts = el.inputmask.opts;
  2374. return isComplete(actionObj.buffer);
  2375. case "unmaskedvalue":
  2376. el = actionObj.el;
  2377. if (el === undefined) {
  2378. //store inputmask instance on the input with element reference
  2379. $el = $({});
  2380. el = $el[0];
  2381. el.inputmask = new Inputmask();
  2382. el.inputmask.opts = opts;
  2383. el.inputmask.el = el;
  2384. el.inputmask.maskset = maskset;
  2385. el.inputmask.isRTL = opts.numericInput;
  2386. if (opts.numericInput) {
  2387. isRTL = true;
  2388. }
  2389. valueBuffer = ($.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call($el, actionObj.value, opts) || actionObj.value) : actionObj.value).split("");
  2390. checkVal($el, false, false, isRTL ? valueBuffer.reverse() : valueBuffer);
  2391. if ($.isFunction(opts.onBeforeWrite)) opts.onBeforeWrite.call(this, undefined, getBuffer(), 0, opts);
  2392. } else $el = $(el);
  2393. maskset = el.inputmask.maskset;
  2394. opts = el.inputmask.opts;
  2395. isRTL = el.inputmask.isRTL;
  2396. return unmaskedvalue($el);
  2397. case "mask":
  2398. undoValue = getBuffer().join("");
  2399. mask(actionObj.el);
  2400. break;
  2401. case "format":
  2402. $el = $({});
  2403. //store inputmask instance on the input with element reference
  2404. $el[0].inputmask = new Inputmask();
  2405. $el[0].inputmask.opts = opts;
  2406. $el[0].inputmask.el = $el[0];
  2407. $el[0].inputmask.maskset = maskset;
  2408. $el[0].inputmask.isRTL = opts.numericInput;
  2409. if (opts.numericInput) {
  2410. isRTL = true;
  2411. }
  2412. valueBuffer = ($.isFunction(opts.onBeforeMask) ? (opts.onBeforeMask.call($el, actionObj.value, opts) || actionObj.value) : actionObj.value).split("");
  2413. checkVal($el, false, false, isRTL ? valueBuffer.reverse() : valueBuffer);
  2414. if ($.isFunction(opts.onBeforeWrite)) opts.onBeforeWrite.call(this, undefined, getBuffer(), 0, opts);
  2415. if (actionObj.metadata) {
  2416. return {
  2417. value: isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join(""),
  2418. metadata: $el.inputmask("getmetadata")
  2419. };
  2420. }
  2421. return isRTL ? getBuffer().slice().reverse().join("") : getBuffer().join("");
  2422. case "isValid":
  2423. $el = $({});
  2424. //store inputmask instance on the input with element reference
  2425. $el[0].inputmask = new Inputmask();
  2426. $el[0].inputmask.opts = opts;
  2427. $el[0].inputmask.el = $el[0];
  2428. $el[0].inputmask.maskset = maskset;
  2429. $el[0].inputmask.isRTL = opts.numericInput;
  2430. if (opts.numericInput) {
  2431. isRTL = true;
  2432. }
  2433. valueBuffer = actionObj.value.split("");
  2434. checkVal($el, false, true, isRTL ? valueBuffer.reverse() : valueBuffer);
  2435. var buffer = getBuffer();
  2436. var rl = determineLastRequiredPosition(),
  2437. lmib = buffer.length - 1;
  2438. for (; lmib > rl; lmib--) {
  2439. if (isMask(lmib)) break;
  2440. }
  2441. buffer.splice(rl, lmib + 1 - rl);
  2442. return isComplete(buffer) && actionObj.value === buffer.join("");
  2443. case "getemptymask":
  2444. el = actionObj.el;
  2445. $el = $(el);
  2446. maskset = el.inputmask.maskset;
  2447. opts = el.inputmask.opts;
  2448. return getBufferTemplate();
  2449. case "remove":
  2450. el = actionObj.el;
  2451. $el = $(el);
  2452. maskset = el.inputmask.maskset;
  2453. opts = el.inputmask.opts;
  2454. //writeout the unmaskedvalue
  2455. el.inputmask._valueSet(unmaskedvalue($el));
  2456. //unbind all events
  2457. $el.unbind(".inputmask");
  2458. //restore the value property
  2459. var valueProperty;
  2460. if (Object.getOwnPropertyDescriptor) {
  2461. valueProperty = Object.getOwnPropertyDescriptor(el, "value");
  2462. }
  2463. if (valueProperty && valueProperty.get) {
  2464. if (el.inputmask.__valueGet) {
  2465. Object.defineProperty(el, "value", {
  2466. get: el.inputmask.__valueGet,
  2467. set: el.inputmask.__valueSet
  2468. });
  2469. }
  2470. } else if (document.__lookupGetter__ && el.__lookupGetter__("value")) {
  2471. if (el.inputmask.__valueGet) {
  2472. el.__defineGetter__("value", el.inputmask.__valueGet);
  2473. el.__defineSetter__("value", el.inputmask.__valueSet);
  2474. }
  2475. }
  2476. //clear data
  2477. el.inputmask = undefined;
  2478. break;
  2479. case "getmetadata":
  2480. el = actionObj.el;
  2481. $el = $(el);
  2482. maskset = el.inputmask.maskset;
  2483. opts = el.inputmask.opts;
  2484. if ($.isArray(maskset.metadata)) {
  2485. //find last alternation
  2486. var alternation, lvp = getLastValidPosition();
  2487. for (var firstAlt = lvp; firstAlt >= 0; firstAlt--) {
  2488. if (getMaskSet().validPositions[firstAlt] && getMaskSet().validPositions[firstAlt].alternation !== undefined) {
  2489. alternation = getMaskSet().validPositions[firstAlt].alternation;
  2490. break;
  2491. }
  2492. }
  2493. if (alternation !== undefined) {
  2494. return maskset.metadata[getMaskSet().validPositions[lvp].locator[alternation]];
  2495. } else return maskset.metadata[0];
  2496. }
  2497. return maskset.metadata;
  2498. }
  2499. }
  2500. }
  2501. //make inputmask available
  2502. window.Inputmask = Inputmask;
  2503. return Inputmask;
  2504. }));