inputmask.js 98 KB

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