inputmask.js 97 KB

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