inputmask.js 98 KB

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