jquery.inputmask.bundle.js 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /**
  2. * @license Input Mask plugin for jquery
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2013 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 1.3.14
  7. */
  8. (function ($) {
  9. if ($.fn.inputmask == undefined) {
  10. $.inputmask = {
  11. //options default
  12. defaults: {
  13. placeholder: "_",
  14. optionalmarker: {
  15. start: "[",
  16. end: "]"
  17. },
  18. escapeChar: "\\",
  19. mask: null,
  20. oncomplete: $.noop, //executes when the mask is complete
  21. onincomplete: $.noop, //executes when the mask is incomplete and focus is lost
  22. oncleared: $.noop, //executes when the mask is cleared
  23. repeat: 0, //repetitions of the mask
  24. greedy: true, //true: allocated buffer for the mask and repetitions - false: allocate only if needed
  25. autoUnmask: false, //automatically unmask when retrieving the value with $.fn.val or value if the browser supports __lookupGetter__ or getOwnPropertyDescriptor
  26. clearMaskOnLostFocus: true,
  27. insertMode: true, //insert the input or overwrite the input
  28. clearIncomplete: false, //clear the incomplete input on blur
  29. aliases: {}, //aliases definitions => see jquery.inputmask.extensions.js
  30. onKeyUp: $.noop, //override to implement autocomplete on certain keys for example
  31. onKeyDown: $.noop, //override to implement autocomplete on certain keys for example
  32. showMaskOnFocus: true, //show the mask-placeholder when the input has focus
  33. showMaskOnHover: true, //show the mask-placeholder when hovering the empty input
  34. onKeyValidation: $.noop, //executes on every key-press with the result of isValid
  35. //numeric basic properties
  36. numericInput: false, //numericInput input direction style (input shifts to the left while holding the caret position)
  37. radixPoint: "", //".", // | ","
  38. //numeric basic properties
  39. definitions: {
  40. '9': {
  41. validator: "[0-9]",
  42. cardinality: 1
  43. },
  44. 'a': {
  45. validator: "[A-Za-z\u0410-\u044F\u0401\u0451]",
  46. cardinality: 1
  47. },
  48. '*': {
  49. validator: "[A-Za-z\u0410-\u044F\u0401\u04510-9]",
  50. cardinality: 1
  51. }
  52. },
  53. keyCode: {
  54. ALT: 18, BACKSPACE: 8, CAPS_LOCK: 20, COMMA: 188, COMMAND: 91, COMMAND_LEFT: 91, COMMAND_RIGHT: 93, CONTROL: 17, DELETE: 46, DOWN: 40, END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, INSERT: 45, LEFT: 37, MENU: 93, NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108,
  55. NUMPAD_MULTIPLY: 106, NUMPAD_SUBTRACT: 109, PAGE_DOWN: 34, PAGE_UP: 33, PERIOD: 190, RIGHT: 39, SHIFT: 16, SPACE: 32, TAB: 9, UP: 38, WINDOWS: 91
  56. },
  57. //specify keycodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF
  58. ignorables: [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],
  59. getMaskLength: function (buffer, greedy, repeat, currentBuffer, opts) {
  60. var calculatedLength = buffer.length;
  61. if (!greedy && repeat > 1) {
  62. calculatedLength += (buffer.length * (repeat - 1));
  63. }
  64. return calculatedLength;
  65. }
  66. },
  67. val: $.fn.val, //store the original jquery val function
  68. escapeRegex: function (str) {
  69. var specials = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'];
  70. return str.replace(new RegExp('(\\' + specials.join('|\\') + ')', 'gim'), '\\$1');
  71. }
  72. };
  73. $.fn.inputmask = function (fn, options) {
  74. var opts = $.extend(true, {}, $.inputmask.defaults, options);
  75. var pasteEvent = isInputEventSupported('paste') ? 'paste' : 'input';
  76. var iphone = navigator.userAgent.match(/iphone/i) != null;
  77. var android = navigator.userAgent.match(/android.*safari.*/i) != null,
  78. android534;
  79. if (android) {
  80. var browser = navigator.userAgent.match(/safari.*/i);
  81. var version = parseInt(new RegExp(/[0-9]+/).exec(browser));
  82. android = (version <= 533);
  83. android534 = (533 < version <= 534);
  84. }
  85. var caretposCorrection = null;
  86. if (typeof fn == "string") {
  87. switch (fn) {
  88. case "mask":
  89. //resolve possible aliases given by options
  90. resolveAlias(opts.alias, options);
  91. //init buffer
  92. var _buffer = getMaskTemplate();
  93. var tests = getTestingChain();
  94. return this.each(function () {
  95. mask(this);
  96. });
  97. break;
  98. case "unmaskedvalue":
  99. var tests = this.data('inputmask')['tests'];
  100. var _buffer = this.data('inputmask')['_buffer'];
  101. opts.greedy = this.data('inputmask')['greedy'];
  102. opts.repeat = this.data('inputmask')['repeat'];
  103. opts.definitions = this.data('inputmask')['definitions'];
  104. return unmaskedvalue(this);
  105. break;
  106. case "remove":
  107. var tests, _buffer;
  108. return this.each(function () {
  109. var $input = $(this), input = this;
  110. setTimeout(function () {
  111. if ($input.data('inputmask')) {
  112. tests = $input.data('inputmask')['tests'];
  113. _buffer = $input.data('inputmask')['_buffer'];
  114. opts.greedy = $input.data('inputmask')['greedy'];
  115. opts.repeat = $input.data('inputmask')['repeat'];
  116. opts.definitions = $input.data('inputmask')['definitions'];
  117. //writeout the unmaskedvalue
  118. input._valueSet(unmaskedvalue($input, true));
  119. //clear data
  120. $input.removeData('inputmask');
  121. //unbind all events
  122. $input.unbind(".inputmask");
  123. $input.removeClass('focus.inputmask');
  124. //restore the value property
  125. var valueProperty;
  126. if (Object.getOwnPropertyDescriptor)
  127. valueProperty = Object.getOwnPropertyDescriptor(input, "value");
  128. if (valueProperty && valueProperty.get) {
  129. if (input._valueGet) {
  130. Object.defineProperty(input, "value", {
  131. get: input._valueGet,
  132. set: input._valueSet
  133. });
  134. }
  135. } else if (document.__lookupGetter__ && input.__lookupGetter__("value")) {
  136. if (input._valueGet) {
  137. input.__defineGetter__("value", input._valueGet);
  138. input.__defineSetter__("value", input._valueSet);
  139. }
  140. }
  141. delete input._valueGet;
  142. delete input._valueSet;
  143. }
  144. }, 0);
  145. });
  146. break;
  147. case "getemptymask": //return the default (empty) mask value, usefull for setting the default value in validation
  148. if (this.data('inputmask'))
  149. return this.data('inputmask')['_buffer'].join('');
  150. else return "";
  151. case "hasMaskedValue": //check wheter the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  152. return this.data('inputmask') ? !this.data('inputmask')['autoUnmask'] : false;
  153. case "isComplete":
  154. var tests = this.data('inputmask')['tests'];
  155. var _buffer = this.data('inputmask')['_buffer'];
  156. opts.greedy = this.data('inputmask')['greedy'];
  157. opts.repeat = this.data('inputmask')['repeat'];
  158. opts.definitions = this.data('inputmask')['definitions'];
  159. return isComplete(this[0]);
  160. default:
  161. //check if the fn is an alias
  162. if (!resolveAlias(fn, options)) {
  163. //maybe fn is a mask so we try
  164. //set mask
  165. opts.mask = fn;
  166. }
  167. //init buffer
  168. var _buffer = getMaskTemplate();
  169. var tests = getTestingChain();
  170. return this.each(function () {
  171. mask(this);
  172. });
  173. break;
  174. }
  175. } else if (typeof fn == "object") {
  176. opts = $.extend(true, {}, $.inputmask.defaults, fn);
  177. resolveAlias(opts.alias, fn); //resolve aliases
  178. //init buffer
  179. var _buffer = getMaskTemplate();
  180. var tests = getTestingChain();
  181. return this.each(function () {
  182. mask(this);
  183. });
  184. } else if (fn == undefined) {
  185. //look for data-inputmask atribute - the attribute should only contain optipns
  186. return this.each(function () {
  187. var attrOptions = $(this).attr("data-inputmask");
  188. if (attrOptions && attrOptions != "") {
  189. try {
  190. attrOptions = attrOptions.replace(new RegExp("'", "g"), '"');
  191. var options = $.parseJSON("{" + attrOptions + "}");
  192. opts = $.extend(true, {}, $.inputmask.defaults, options);
  193. resolveAlias(opts.alias, options);
  194. opts.alias = undefined;
  195. $(this).inputmask(opts);
  196. } catch (ex) { } //need a more relax parseJSON
  197. }
  198. });
  199. }
  200. //helper functions
  201. function isInputEventSupported(eventName) {
  202. var el = document.createElement('input'),
  203. eventName = 'on' + eventName,
  204. isSupported = (eventName in el);
  205. if (!isSupported) {
  206. el.setAttribute(eventName, 'return;');
  207. isSupported = typeof el[eventName] == 'function';
  208. }
  209. el = null;
  210. return isSupported;
  211. }
  212. function resolveAlias(aliasStr, options) {
  213. var aliasDefinition = opts.aliases[aliasStr];
  214. if (aliasDefinition) {
  215. if (aliasDefinition.alias) resolveAlias(aliasDefinition.alias); //alias is another alias
  216. $.extend(true, opts, aliasDefinition); //merge alias definition in the options
  217. $.extend(true, opts, options); //reapply extra given options
  218. return true;
  219. }
  220. return false;
  221. }
  222. function getMaskTemplate() {
  223. var escaped = false, outCount = 0, mask = opts.mask.toString();
  224. if (mask.length == 1 && opts.greedy == false) { opts.placeholder = ""; } //hide placeholder with single non-greedy mask
  225. var singleMask = $.map(mask.split(""), function (element, index) {
  226. var outElem = [];
  227. if (element == opts.escapeChar) {
  228. escaped = true;
  229. }
  230. else if ((element != opts.optionalmarker.start && element != opts.optionalmarker.end) || escaped) {
  231. var maskdef = opts.definitions[element];
  232. if (maskdef && !escaped) {
  233. for (var i = 0; i < maskdef.cardinality; i++) {
  234. outElem.push(getPlaceHolder(outCount + i));
  235. }
  236. } else {
  237. outElem.push(element);
  238. escaped = false;
  239. }
  240. outCount += outElem.length;
  241. return outElem;
  242. }
  243. });
  244. //allocate repetitions
  245. var repeatedMask = singleMask.slice();
  246. for (var i = 1; i < opts.repeat && opts.greedy; i++) {
  247. repeatedMask = repeatedMask.concat(singleMask.slice());
  248. }
  249. return repeatedMask;
  250. }
  251. //test definition => {fn: RegExp/function, cardinality: int, optionality: bool, newBlockMarker: bool, offset: int, casing: null/upper/lower, def: definitionSymbol}
  252. function getTestingChain() {
  253. var isOptional = false, escaped = false, mask = opts.mask.toString();
  254. var newBlockMarker = false; //indicates wheter the begin/ending of a block should be indicated
  255. return $.map(mask.split(""), function (element, index) {
  256. var outElem = [];
  257. if (element == opts.escapeChar) {
  258. escaped = true;
  259. } else if (element == opts.optionalmarker.start && !escaped) {
  260. isOptional = true;
  261. newBlockMarker = true;
  262. }
  263. else if (element == opts.optionalmarker.end && !escaped) {
  264. isOptional = false;
  265. newBlockMarker = true;
  266. }
  267. else {
  268. var maskdef = opts.definitions[element];
  269. if (maskdef && !escaped) {
  270. var prevalidators = maskdef["prevalidator"], prevalidatorsL = prevalidators ? prevalidators.length : 0;
  271. for (var i = 1; i < maskdef.cardinality; i++) {
  272. var prevalidator = prevalidatorsL >= i ? prevalidators[i - 1] : [], validator = prevalidator["validator"], cardinality = prevalidator["cardinality"];
  273. outElem.push({ fn: validator ? typeof validator == 'string' ? new RegExp(validator) : new function () { this.test = validator; } : new RegExp("."), cardinality: cardinality ? cardinality : 1, optionality: isOptional, newBlockMarker: isOptional == true ? newBlockMarker : false, offset: 0, casing: maskdef["casing"], def: element });
  274. if (isOptional == true) //reset newBlockMarker
  275. newBlockMarker = false;
  276. }
  277. outElem.push({ fn: maskdef.validator ? typeof maskdef.validator == 'string' ? new RegExp(maskdef.validator) : new function () { this.test = maskdef.validator; } : new RegExp("."), cardinality: maskdef.cardinality, optionality: isOptional, newBlockMarker: newBlockMarker, offset: 0, casing: maskdef["casing"], def: element });
  278. } else {
  279. outElem.push({ fn: null, cardinality: 0, optionality: isOptional, newBlockMarker: newBlockMarker, offset: 0, casing: null, def: element });
  280. escaped = false;
  281. }
  282. //reset newBlockMarker
  283. newBlockMarker = false;
  284. return outElem;
  285. }
  286. });
  287. }
  288. function isValid(pos, c, buffer, strict) { //strict true ~ no correction or autofill
  289. var result = false;
  290. if (pos >= 0 && pos < getMaskLength(buffer)) {
  291. var testPos = determineTestPosition(pos), loopend = c ? 1 : 0, chrs = '';
  292. for (var i = tests[testPos].cardinality; i > loopend; i--) {
  293. chrs += getBufferElement(buffer, testPos - (i - 1));
  294. }
  295. if (c) {
  296. chrs += c;
  297. }
  298. //return is false or a json object => { pos: ??, c: ??}
  299. result = tests[testPos].fn != null ? tests[testPos].fn.test(chrs, buffer, pos, strict, opts) : false;
  300. }
  301. setTimeout(function () { opts.onKeyValidation.call(this, result, opts); }, 0); //extra stuff to execute on keydown
  302. return result;
  303. }
  304. function isMask(pos) {
  305. var testPos = determineTestPosition(pos);
  306. var test = tests[testPos];
  307. return test != undefined ? test.fn : false;
  308. }
  309. function determineTestPosition(pos) {
  310. return pos % tests.length;
  311. }
  312. function getPlaceHolder(pos) {
  313. return opts.placeholder.charAt(pos % opts.placeholder.length);
  314. }
  315. function getMaskLength(currentBuffer) {
  316. return opts.getMaskLength(_buffer, opts.greedy, opts.repeat, currentBuffer, opts);
  317. }
  318. //pos: from position
  319. function seekNext(buffer, pos) {
  320. var maskL = getMaskLength(buffer);
  321. if (pos >= maskL) return maskL;
  322. var position = pos;
  323. while (++position < maskL && !isMask(position)) { };
  324. return position;
  325. }
  326. //pos: from position
  327. function seekPrevious(buffer, pos) {
  328. var position = pos;
  329. if (position <= 0) return 0;
  330. while (--position > 0 && !isMask(position)) { };
  331. return position;
  332. }
  333. function setBufferElement(buffer, position, element) {
  334. //position = prepareBuffer(buffer, position);
  335. var test = tests[determineTestPosition(position)];
  336. var elem = element;
  337. if (elem != undefined) {
  338. switch (test.casing) {
  339. case "upper":
  340. elem = element.toUpperCase();
  341. break;
  342. case "lower":
  343. elem = element.toLowerCase();
  344. break;
  345. }
  346. }
  347. buffer[position] = elem;
  348. }
  349. function getBufferElement(buffer, position, autoPrepare) {
  350. if (autoPrepare) position = prepareBuffer(buffer, position);
  351. return buffer[position];
  352. }
  353. //needed to handle the non-greedy mask repetitions
  354. function prepareBuffer(buffer, position, isRTL) {
  355. var j;
  356. if (isRTL) {
  357. while (position < 0 && buffer.length < getMaskLength(buffer)) {
  358. j = _buffer.length - 1;
  359. position = _buffer.length;
  360. while (_buffer[j] !== undefined) {
  361. buffer.unshift(_buffer[j--]);
  362. }
  363. }
  364. } else {
  365. while (buffer[position] == undefined && buffer.length < getMaskLength(buffer)) {
  366. j = 0;
  367. while (_buffer[j] !== undefined) { //add a new buffer
  368. buffer.push(_buffer[j++]);
  369. }
  370. }
  371. }
  372. return position;
  373. }
  374. function writeBuffer(input, buffer, caretPos) {
  375. input._valueSet(buffer.join(''));
  376. if (caretPos != undefined) {
  377. if (android) {
  378. setTimeout(function () {
  379. caret(input, caretPos);
  380. }, 100);
  381. }
  382. else caret(input, caretPos);
  383. }
  384. };
  385. function clearBuffer(buffer, start, end) {
  386. for (var i = start, maskL = getMaskLength(buffer) ; i < end && i < maskL; i++) {
  387. setBufferElement(buffer, i, getBufferElement(_buffer.slice(), i));
  388. }
  389. };
  390. function setReTargetPlaceHolder(buffer, pos) {
  391. var testPos = determineTestPosition(pos);
  392. setBufferElement(buffer, pos, getBufferElement(_buffer, testPos));
  393. }
  394. function checkVal(input, buffer, clearInvalid, skipRadixHandling) {
  395. var isRTL = $(input).data('inputmask')['isRTL'],
  396. inputValue = truncateInput(input._valueGet(), isRTL).split('');
  397. var maskL = getMaskLength(buffer);
  398. if (isRTL) { //align inputValue for RTL/numeric input
  399. var inputValueRev = inputValue.reverse(); inputValueRev.length = maskL;
  400. for (var i = 0; i < maskL; i++) {
  401. var targetPosition = determineTestPosition(maskL - (i + 1));
  402. if (tests[targetPosition].fn == null && inputValueRev[i] != getBufferElement(_buffer, targetPosition)) {
  403. inputValueRev.splice(i, 0, getBufferElement(_buffer, targetPosition));
  404. inputValueRev.length = maskL;
  405. } else {
  406. inputValueRev[i] = inputValueRev[i] || getBufferElement(_buffer, targetPosition);
  407. }
  408. }
  409. inputValue = inputValueRev.reverse();
  410. }
  411. clearBuffer(buffer, 0, buffer.length);
  412. buffer.length = _buffer.length;
  413. var lastMatch = -1, checkPosition = -1, np, ivl = inputValue.length, rtlMatch = ivl == 0 ? maskL : -1;
  414. for (var i = 0; i < ivl; i++) {
  415. for (var pos = checkPosition + 1; pos < maskL; pos++) {
  416. if (isMask(pos)) {
  417. var c = inputValue[i];
  418. if ((np = isValid(pos, c, buffer, !clearInvalid)) !== false) {
  419. if (np !== true) {
  420. pos = np.pos != undefined ? np.pos : pos; //set new position from isValid
  421. c = np.c != undefined ? np.c : c; //set new char from isValid
  422. }
  423. setBufferElement(buffer, pos, c);
  424. lastMatch = checkPosition = pos;
  425. } else {
  426. setReTargetPlaceHolder(buffer, pos);
  427. if (c == getPlaceHolder(pos)) {
  428. checkPosition = pos;
  429. rtlMatch = pos;
  430. }
  431. }
  432. break;
  433. } else { //nonmask
  434. setReTargetPlaceHolder(buffer, pos);
  435. if (lastMatch == checkPosition) //once outsync the nonmask cannot be the lastmatch
  436. lastMatch = pos;
  437. checkPosition = pos;
  438. if (inputValue[i] == getBufferElement(buffer, pos))
  439. break;
  440. }
  441. }
  442. }
  443. //Truncate buffer when using non-greedy masks
  444. if (opts.greedy == false) {
  445. var newBuffer = truncateInput(buffer.join(''), isRTL).split('');
  446. while (buffer.length != newBuffer.length) { //map changes into the original buffer
  447. isRTL ? buffer.shift() : buffer.pop();
  448. }
  449. }
  450. if (clearInvalid) {
  451. writeBuffer(input, buffer);
  452. }
  453. return isRTL ? (opts.numericInput ? (opts.radixPoint != "" && $.inArray(opts.radixPoint, buffer) != -1 && skipRadixHandling !== true ? $.inArray(opts.radixPoint, buffer) : seekNext(buffer, maskL)) : seekNext(buffer, rtlMatch)) : seekNext(buffer, lastMatch);
  454. }
  455. function escapeRegex(str) {
  456. return $.inputmask.escapeRegex.call(this, str);
  457. }
  458. function truncateInput(inputValue, rtl) {
  459. return rtl ? inputValue.replace(new RegExp("^(" + escapeRegex(_buffer.join('')) + ")*"), "") : inputValue.replace(new RegExp("(" + escapeRegex(_buffer.join('')) + ")*$"), "");
  460. }
  461. function clearOptionalTail(input, buffer) {
  462. checkVal(input, buffer, false);
  463. var tmpBuffer = buffer.slice(), testPos, pos;
  464. if ($(input).data('inputmask')['isRTL']) {
  465. for (pos = 0; pos <= tmpBuffer.length - 1; pos++) {
  466. testPos = determineTestPosition(pos);
  467. if (tests[testPos].optionality) {
  468. if (!isMask(pos) || !isValid(pos, buffer[pos], buffer, true))
  469. tmpBuffer.splice(0, 1);
  470. else break;
  471. } else break;
  472. }
  473. } else {
  474. for (pos = tmpBuffer.length - 1; pos >= 0; pos--) {
  475. testPos = determineTestPosition(pos);
  476. if (tests[testPos].optionality) {
  477. if (!isMask(pos) || !isValid(pos, buffer[pos], buffer, true))
  478. tmpBuffer.pop();
  479. else break;
  480. } else break;
  481. }
  482. }
  483. writeBuffer(input, tmpBuffer);
  484. }
  485. //functionality fn
  486. function unmaskedvalue($input, skipDatepickerCheck) {
  487. var input = $input[0];
  488. if (tests && (skipDatepickerCheck === true || !$input.hasClass('hasDatepicker'))) {
  489. var buffer = _buffer.slice();
  490. checkVal(input, buffer);
  491. return $.map(buffer, function (element, index) {
  492. return isMask(index) && isValid(index, element, buffer, true) ? element : null;
  493. }).join('');
  494. }
  495. else {
  496. return input._valueGet();
  497. }
  498. }
  499. function caret(input, begin, end) {
  500. var npt = input.jquery && input.length > 0 ? input[0] : input;
  501. if (typeof begin == 'number') {
  502. if (!$(input).is(':visible')) {
  503. return;
  504. }
  505. end = (typeof end == 'number') ? end : begin;
  506. if (opts.insertMode == false && begin == end) end++; //set visualization for insert/overwrite mode
  507. if (npt.setSelectionRange) {
  508. if (end == begin) {
  509. npt.focus();
  510. npt.setSelectionRange(begin, end);
  511. } else {
  512. npt.select();
  513. npt.selectionStart = begin;
  514. npt.selectionEnd = android534 ? begin : end;
  515. }
  516. } else if (npt.createTextRange) {
  517. var range = npt.createTextRange();
  518. range.collapse(true);
  519. range.moveEnd('character', end);
  520. range.moveStart('character', begin);
  521. range.select();
  522. }
  523. npt.focus();
  524. if (android && end != npt.selectionEnd) caretposCorrection = { begin: begin, end: end };
  525. } else {
  526. if (!$(input).is(':visible')) {
  527. return { begin: 0, end: 0 };
  528. }
  529. var caretpos = android ? caretposCorrection : null, caretposCorrection = null;
  530. if (caretpos == null) {
  531. if (npt.setSelectionRange) {
  532. begin = npt.selectionStart;
  533. end = npt.selectionEnd;
  534. } else if (document.selection && document.selection.createRange) {
  535. var range = document.selection.createRange();
  536. begin = 0 - range.duplicate().moveStart('character', -100000);
  537. end = begin + range.text.length;
  538. }
  539. caretpos = { begin: begin, end: end };
  540. }
  541. return caretpos;
  542. }
  543. };
  544. function isComplete(npt) {
  545. var complete = true, nptValue = npt._valueGet(), ml = nptValue.length;
  546. for (var i = 0; i < ml; i++) {
  547. if (isMask(i) && nptValue.charAt(i) == getPlaceHolder(i)) {
  548. complete = false;
  549. break;
  550. }
  551. }
  552. return complete;
  553. }
  554. function mask(el) {
  555. var $input = $(el);
  556. if (!$input.is(":input")) return;
  557. var buffer = _buffer.slice();
  558. //correct greedy setting if needed
  559. opts.greedy = opts.greedy ? opts.greedy : opts.repeat == 0;
  560. //handle maxlength attribute
  561. var maxLength = $input.prop('maxLength');
  562. if (getMaskLength(buffer) > maxLength && maxLength > -1) { //FF sets no defined max length to -1
  563. if (maxLength < _buffer.length) _buffer.length = maxLength;
  564. if (opts.greedy == false) {
  565. opts.repeat = Math.round(maxLength / _buffer.length);
  566. }
  567. $input.prop('maxLength', getMaskLength(buffer) * 2);
  568. }
  569. //store tests & original buffer in the input element - used to get the unmasked value
  570. $input.data('inputmask', {
  571. 'tests': tests,
  572. '_buffer': _buffer,
  573. 'greedy': opts.greedy,
  574. 'repeat': opts.repeat,
  575. 'autoUnmask': opts.autoUnmask,
  576. 'definitions': opts.definitions,
  577. 'isRTL': false
  578. });
  579. patchValueProperty(el);
  580. //init vars
  581. var undoBuffer = el._valueGet(),
  582. skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
  583. ignorable = false,
  584. lastPosition = -1,
  585. firstMaskPos = seekNext(buffer, -1),
  586. lastMaskPos = seekPrevious(buffer, getMaskLength(buffer)),
  587. isRTL = false;
  588. if (el.dir == "rtl" || opts.numericInput) {
  589. el.dir = "ltr"
  590. $input.css("text-align", "right");
  591. $input.removeAttr("dir");
  592. var inputData = $input.data('inputmask');
  593. inputData['isRTL'] = true;
  594. $input.data('inputmask', inputData);
  595. isRTL = true;
  596. }
  597. //unbind all events - to make sure that no other mask will interfere when re-masking
  598. $input.unbind(".inputmask");
  599. $input.removeClass('focus.inputmask');
  600. //bind events
  601. $input.bind("mouseenter.inputmask", function () {
  602. var $input = $(this), input = this;
  603. if (!$input.hasClass('focus.inputmask') && opts.showMaskOnHover) {
  604. var nptL = input._valueGet().length;
  605. if (nptL < buffer.length) {
  606. if (nptL == 0)
  607. buffer = _buffer.slice();
  608. writeBuffer(input, buffer);
  609. }
  610. }
  611. }).bind("blur.inputmask", function () {
  612. var $input = $(this), input = this, nptValue = input._valueGet();
  613. $input.removeClass('focus.inputmask');
  614. if (nptValue != undoBuffer) {
  615. $input.change();
  616. }
  617. if (opts.clearMaskOnLostFocus && nptValue != '') {
  618. if (nptValue == _buffer.join(''))
  619. input._valueSet('');
  620. else { //clearout optional tail of the mask
  621. clearOptionalTail(input, buffer);
  622. }
  623. }
  624. if (!isComplete(input)) {
  625. $input.trigger("incomplete");
  626. if (opts.clearIncomplete) {
  627. if (opts.clearMaskOnLostFocus)
  628. input._valueSet('');
  629. else {
  630. buffer = _buffer.slice();
  631. writeBuffer(input, buffer);
  632. }
  633. }
  634. }
  635. }).bind("focus.inputmask", function () {
  636. var $input = $(this), input = this, nptValue = input._valueGet();
  637. if (opts.showMaskOnFocus && !$input.hasClass('focus.inputmask') && (!opts.showMaskOnHover || (opts.showMaskOnHover && nptValue == ''))) {
  638. var nptL = nptValue.length;
  639. if (nptL < buffer.length) {
  640. if (nptL == 0)
  641. buffer = _buffer.slice();
  642. caret(input, checkVal(input, buffer, true));
  643. }
  644. }
  645. $input.addClass('focus.inputmask');
  646. undoBuffer = input._valueGet();
  647. }).bind("mouseleave.inputmask", function () {
  648. var $input = $(this), input = this;
  649. if (opts.clearMaskOnLostFocus) {
  650. if (!$input.hasClass('focus.inputmask')) {
  651. if (input._valueGet() == _buffer.join('') || input._valueGet() == '')
  652. input._valueSet('');
  653. else { //clearout optional tail of the mask
  654. clearOptionalTail(input, buffer);
  655. }
  656. }
  657. }
  658. }).bind("click.inputmask", function () {
  659. var input = this;
  660. setTimeout(function () {
  661. var selectedCaret = caret(input);
  662. if (selectedCaret.begin == selectedCaret.end) {
  663. var clickPosition = selectedCaret.begin;
  664. lastPosition = checkVal(input, buffer, false);
  665. determineInputDirection(input, selectedCaret);
  666. if (isRTL)
  667. caret(input, clickPosition > lastPosition && (isValid(clickPosition, buffer[clickPosition], buffer, true) !== false || !isMask(clickPosition)) ? clickPosition : lastPosition);
  668. else
  669. caret(input, clickPosition < lastPosition && (isValid(clickPosition, buffer[clickPosition], buffer, true) !== false || !isMask(clickPosition)) ? clickPosition : lastPosition);
  670. }
  671. }, 0);
  672. }).bind('dblclick.inputmask', function () {
  673. var input = this;
  674. setTimeout(function () {
  675. caret(input, 0, lastPosition);
  676. }, 0);
  677. }).bind("keydown.inputmask", keydownEvent
  678. ).bind("keypress.inputmask", keypressEvent
  679. ).bind("keyup.inputmask", keyupEvent
  680. ).bind(pasteEvent + ".inputmask dragdrop.inputmask drop.inputmask", function () {
  681. var input = this;
  682. setTimeout(function () {
  683. caret(input, checkVal(input, buffer, true));
  684. }, 0);
  685. }).bind('setvalue.inputmask', function () {
  686. var input = this;
  687. undoBuffer = input._valueGet();
  688. checkVal(input, buffer, true);
  689. if (input._valueGet() == _buffer.join(''))
  690. input._valueSet('');
  691. }).bind('complete.inputmask', opts.oncomplete)
  692. .bind('incomplete.inputmask', opts.onincomplete)
  693. .bind('cleared.inputmask', opts.oncleared);
  694. //apply mask
  695. lastPosition = checkVal(el, buffer, true);
  696. // Wrap document.activeElement in a try/catch block since IE9 throw "Unspecified error" if document.activeElement is undefined when we are in an IFrame.
  697. var activeElement;
  698. try {
  699. activeElement = document.activeElement;
  700. } catch (e) { }
  701. if (activeElement === el) { //position the caret when in focus
  702. $input.addClass('focus.inputmask');
  703. caret(el, lastPosition);
  704. } else if (opts.clearMaskOnLostFocus) {
  705. if (el._valueGet() == _buffer.join('')) {
  706. el._valueSet('');
  707. } else {
  708. clearOptionalTail(el, buffer);
  709. }
  710. }
  711. installEventRuler(el);
  712. //private functions
  713. function installEventRuler(npt) {
  714. var events = $._data(npt).events;
  715. $.each(events, function (eventType, eventHandlers) {
  716. $.each(eventHandlers, function (ndx, eventHandler) {
  717. if (eventHandler.namespace == "inputmask") {
  718. var handler = eventHandler.handler;
  719. eventHandler.handler = function () {
  720. if (this.readOnly || this.disabled)
  721. return false;
  722. return handler.apply(this, arguments);
  723. };
  724. }
  725. });
  726. });
  727. }
  728. function patchValueProperty(npt) {
  729. var valueProperty;
  730. if (Object.getOwnPropertyDescriptor)
  731. valueProperty = Object.getOwnPropertyDescriptor(npt, "value");
  732. if (valueProperty && valueProperty.get) {
  733. if (!npt._valueGet) {
  734. npt._valueGet = valueProperty.get;
  735. npt._valueSet = valueProperty.set;
  736. Object.defineProperty(npt, "value", {
  737. get: function () {
  738. var $self = $(this), inputData = $(this).data('inputmask');
  739. return inputData && inputData['autoUnmask'] ? $self.inputmask('unmaskedvalue') : this._valueGet() != inputData['_buffer'].join('') ? this._valueGet() : '';
  740. },
  741. set: function (value) {
  742. this._valueSet(value); $(this).triggerHandler('setvalue.inputmask');
  743. }
  744. });
  745. }
  746. } else if (document.__lookupGetter__ && npt.__lookupGetter__("value")) {
  747. if (!npt._valueGet) {
  748. npt._valueGet = npt.__lookupGetter__("value");
  749. npt._valueSet = npt.__lookupSetter__("value");
  750. npt.__defineGetter__("value", function () {
  751. var $self = $(this), inputData = $(this).data('inputmask');
  752. return inputData && inputData['autoUnmask'] ? $self.inputmask('unmaskedvalue') : this._valueGet() != inputData['_buffer'].join('') ? this._valueGet() : '';
  753. });
  754. npt.__defineSetter__("value", function (value) {
  755. this._valueSet(value); $(this).triggerHandler('setvalue.inputmask');
  756. });
  757. }
  758. } else {
  759. if (!npt._valueGet) {
  760. npt._valueGet = function () { return this.value; }
  761. npt._valueSet = function (value) { this.value = value; }
  762. }
  763. if ($.fn.val.inputmaskpatch != true) {
  764. $.fn.val = function () {
  765. if (arguments.length == 0) {
  766. var $self = $(this);
  767. if ($self.data('inputmask')) {
  768. if ($self.data('inputmask')['autoUnmask'])
  769. return $self.inputmask('unmaskedvalue');
  770. else {
  771. var result = $.inputmask.val.apply($self);
  772. return result != $self.data('inputmask')['_buffer'].join('') ? result : '';
  773. }
  774. } else return $.inputmask.val.apply($self);
  775. } else {
  776. var args = arguments;
  777. return this.each(function () {
  778. var $self = $(this);
  779. var result = $.inputmask.val.apply($self, args);
  780. if ($self.data('inputmask')) $self.triggerHandler('setvalue.inputmask');
  781. return result;
  782. });
  783. }
  784. };
  785. $.extend($.fn.val, {
  786. inputmaskpatch: true
  787. });
  788. }
  789. }
  790. }
  791. function determineInputDirection(input, pos) {
  792. //set input direction according the position to the radixPoint
  793. if (opts.numericInput && opts.radixPoint != "") {
  794. var nptStr = input._valueGet();
  795. var radixPosition = nptStr.indexOf(opts.radixPoint);
  796. isRTL = pos.begin <= radixPosition || pos.end <= radixPosition || radixPosition == -1;
  797. }
  798. }
  799. //shift chars to left from start to end and put c at end position if defined
  800. function shiftL(start, end, c) {
  801. while (!isMask(start) && start - 1 >= 0) start--;
  802. for (var i = start; i < end && i < getMaskLength(buffer) ; i++) {
  803. if (isMask(i)) {
  804. setReTargetPlaceHolder(buffer, i);
  805. var j = seekNext(buffer, i);
  806. var p = getBufferElement(buffer, j);
  807. if (p != getPlaceHolder(j)) {
  808. if (j < getMaskLength(buffer) && isValid(i, p, buffer, true) !== false && tests[determineTestPosition(i)].def == tests[determineTestPosition(j)].def) {
  809. setBufferElement(buffer, i, getBufferElement(buffer, j));
  810. setReTargetPlaceHolder(buffer, j); //cleanup next position
  811. } else {
  812. if (isMask(i))
  813. break;
  814. }
  815. } else if (c == undefined) break;
  816. } else {
  817. setReTargetPlaceHolder(buffer, i);
  818. }
  819. }
  820. if (c != undefined)
  821. setBufferElement(buffer, isRTL ? end : seekPrevious(buffer, end), c);
  822. buffer = truncateInput(buffer.join(''), isRTL).split('');
  823. if (buffer.length == 0) buffer = _buffer.slice();
  824. return start; //return the used start position
  825. }
  826. function shiftR(start, end, c, full) { //full => behave like a push right ~ do not stop on placeholders
  827. for (var i = start; i <= end && i < getMaskLength(buffer) ; i++) {
  828. if (isMask(i)) {
  829. var t = getBufferElement(buffer, i);
  830. setBufferElement(buffer, i, c);
  831. if (t != getPlaceHolder(i)) {
  832. var j = seekNext(buffer, i);
  833. if (j < getMaskLength(buffer)) {
  834. if (isValid(j, t, buffer, true) !== false && tests[determineTestPosition(i)].def == tests[determineTestPosition(j)].def)
  835. c = t;
  836. else {
  837. if (isMask(j))
  838. break;
  839. else c = t;
  840. }
  841. } else break;
  842. } else if (full !== true) break;
  843. } else
  844. setReTargetPlaceHolder(buffer, i);
  845. }
  846. var lengthBefore = buffer.length;
  847. buffer = truncateInput(buffer.join(''), isRTL).split('');
  848. if (buffer.length == 0) buffer = _buffer.slice();
  849. return end - (lengthBefore - buffer.length); //return new start position
  850. };
  851. function keydownEvent(e) {
  852. //Safari 5.1.x - modal dialog fires keypress twice workaround
  853. skipKeyPressEvent = false;
  854. var input = this, k = e.keyCode, pos = caret(input);
  855. determineInputDirection(input, pos);
  856. //backspace, delete, and escape get special treatment
  857. if (k == opts.keyCode.BACKSPACE || k == opts.keyCode.DELETE || (iphone && k == 127)) {//backspace/delete
  858. var maskL = getMaskLength(buffer);
  859. if (pos.begin == 0 && pos.end == maskL) { //remove full selection
  860. buffer = _buffer.slice();
  861. writeBuffer(input, buffer);
  862. caret(input, checkVal(input, buffer, false));
  863. } else if ((pos.end - pos.begin) > 1 || ((pos.end - pos.begin) == 1 && opts.insertMode)) { //partial selection
  864. clearBuffer(buffer, pos.begin, pos.end);
  865. writeBuffer(input, buffer, isRTL ? checkVal(input, buffer, false) : pos.begin);
  866. } else { //handle delete
  867. var beginPos = pos.begin;
  868. if (k == opts.keyCode.DELETE) {
  869. if (beginPos < firstMaskPos)
  870. beginPos = firstMaskPos;
  871. if (beginPos < maskL) {
  872. if (opts.numericInput && opts.radixPoint != "" && buffer[beginPos] == opts.radixPoint) {
  873. beginPos = (buffer.length - 1 == beginPos) /* radixPoint is latest? delete it */ ? beginPos : seekNext(buffer, beginPos);
  874. beginPos = shiftL(beginPos, maskL);
  875. } else {
  876. if (isRTL) {
  877. beginPos = shiftR(firstMaskPos, beginPos, getPlaceHolder(beginPos), true);
  878. beginPos = seekNext(buffer, beginPos);
  879. } else {
  880. beginPos = shiftL(beginPos, maskL);
  881. }
  882. }
  883. writeBuffer(input, buffer, beginPos);
  884. }
  885. } else if (k == opts.keyCode.BACKSPACE) { //handle backspace
  886. if (beginPos > firstMaskPos) {
  887. beginPos -= 1;
  888. if (opts.numericInput && opts.radixPoint != "" && buffer[beginPos] == opts.radixPoint) {
  889. beginPos = shiftR(firstMaskPos, (buffer.length - 1 == beginPos) /* radixPoint is latest? delete it */ ? beginPos : beginPos - 1, getPlaceHolder(beginPos), true);
  890. beginPos++;
  891. } else {
  892. if (isRTL) {
  893. beginPos = shiftR(firstMaskPos, beginPos, getPlaceHolder(beginPos), true);
  894. beginPos = buffer[beginPos + 1] == opts.radixPoint ? beginPos + 1 : seekNext(buffer, beginPos);
  895. } else {
  896. beginPos = shiftL(beginPos, maskL);
  897. }
  898. }
  899. writeBuffer(input, buffer, beginPos);
  900. }
  901. }
  902. }
  903. if (input._valueGet() == _buffer.join(''))
  904. $(input).trigger('cleared');
  905. e.preventDefault(); //stop default action but allow propagation
  906. } else if (k == opts.keyCode.END || k == opts.keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch
  907. setTimeout(function () {
  908. var caretPos = checkVal(input, buffer, false, true);
  909. if (!opts.insertMode && caretPos == getMaskLength(buffer) && !e.shiftKey) caretPos--;
  910. caret(input, e.shiftKey ? pos.begin : caretPos, caretPos);
  911. }, 0);
  912. } else if (k == opts.keyCode.HOME || k == opts.keyCode.PAGE_UP) {//Home or page_up
  913. caret(input, 0, e.shiftKey ? pos.begin : 0);
  914. }
  915. else if (k == opts.keyCode.ESCAPE) {//escape
  916. input._valueSet(undoBuffer);
  917. caret(input, 0, checkVal(input, buffer));
  918. } else if (k == opts.keyCode.INSERT) {//insert
  919. opts.insertMode = !opts.insertMode;
  920. caret(input, !opts.insertMode && pos.begin == getMaskLength(buffer) ? pos.begin - 1 : pos.begin);
  921. } else if (e.ctrlKey && k == 88) {
  922. setTimeout(function () {
  923. caret(input, checkVal(input, buffer, true));
  924. }, 0);
  925. } else if (!opts.insertMode) { //overwritemode
  926. if (k == opts.keyCode.RIGHT) {//right
  927. var caretPos = pos.begin == pos.end ? pos.end + 1 : pos.end;
  928. caretPos = caretPos < getMaskLength(buffer) ? caretPos : pos.end;
  929. caret(input, e.shiftKey ? pos.begin : caretPos, e.shiftKey ? caretPos + 1 : caretPos);
  930. } else if (k == opts.keyCode.LEFT) {//left
  931. var caretPos = pos.begin - 1;
  932. caretPos = caretPos > 0 ? caretPos : 0;
  933. caret(input, caretPos, e.shiftKey ? pos.end : caretPos);
  934. }
  935. }
  936. opts.onKeyDown.call(this, e, buffer, opts); //extra stuff to execute on keydown
  937. ignorable = $.inArray(k, opts.ignorables) != -1;
  938. }
  939. function keypressEvent(e) {
  940. //Safari 5.1.x - modal dialog fires keypress twice workaround
  941. if (skipKeyPressEvent) return false;
  942. skipKeyPressEvent = true;
  943. var input = this, $input = $(input);
  944. e = e || window.event;
  945. var k = e.which || e.charCode || e.keyCode,
  946. c = String.fromCharCode(k);
  947. if (opts.numericInput && c == opts.radixPoint) {
  948. var nptStr = input._valueGet();
  949. var radixPosition = nptStr.indexOf(opts.radixPoint);
  950. caret(input, seekNext(buffer, radixPosition != -1 ? radixPosition : getMaskLength(buffer)));
  951. }
  952. if (e.ctrlKey || e.altKey || e.metaKey || ignorable) {
  953. return true;
  954. } else {
  955. if (k) {
  956. $input.trigger('input');
  957. var pos = caret(input), maskL = getMaskLength(buffer), writeOutBuffer = true;
  958. clearBuffer(buffer, pos.begin, pos.end);
  959. if (isRTL) {
  960. var p = seekPrevious(buffer, pos.end), np;
  961. if ((np = isValid(p == maskL || getBufferElement(buffer, p) == opts.radixPoint ? seekPrevious(buffer, p) : p, c, buffer, false)) !== false) {
  962. if (np !== true) {
  963. p = np.pos != undefined ? np.pos : p; //set new position from isValid
  964. c = np.c != undefined ? np.c : c; //set new char from isValid
  965. }
  966. maskL = getMaskLength(buffer); //update masklength to include possible groupSeparator offset
  967. var firstUnmaskedPosition = firstMaskPos;
  968. if (opts.insertMode == true) {
  969. if (opts.greedy == true) {
  970. var bfrClone = buffer.slice();
  971. while (getBufferElement(bfrClone, firstUnmaskedPosition, true) != getPlaceHolder(firstUnmaskedPosition) && firstUnmaskedPosition <= p) {
  972. firstUnmaskedPosition = firstUnmaskedPosition == maskL ? (maskL + 1) : seekNext(buffer, firstUnmaskedPosition);
  973. }
  974. }
  975. if (firstUnmaskedPosition <= p && (opts.greedy || buffer.length < maskL)) {
  976. if (buffer[firstMaskPos] != getPlaceHolder(firstMaskPos) && buffer.length < maskL) {
  977. var offset = prepareBuffer(buffer, -1, isRTL);
  978. if (pos.end != 0) p = p + offset;
  979. maskL = buffer.length;
  980. }
  981. shiftL(firstUnmaskedPosition, p, c);
  982. } else writeOutBuffer = false;
  983. } else setBufferElement(buffer, p, c);
  984. if (writeOutBuffer) {
  985. writeBuffer(input, buffer, opts.numericInput ? p + 1 : p);
  986. setTimeout(function () { //timeout needed for IE
  987. if (isComplete(input))
  988. $input.trigger("complete");
  989. }, 0);
  990. }
  991. } else if (android) writeBuffer(input, buffer, pos.begin);
  992. }
  993. else {
  994. var p = seekNext(buffer, pos.begin - 1), np;
  995. prepareBuffer(buffer, p, isRTL);
  996. if ((np = isValid(p, c, buffer, false)) !== false) {
  997. if (np !== true) {
  998. p = np.pos != undefined ? np.pos : p; //set new position from isValid
  999. c = np.c != undefined ? np.c : c; //set new char from isValid
  1000. }
  1001. if (opts.insertMode == true) {
  1002. var lastUnmaskedPosition = getMaskLength(buffer);
  1003. var bfrClone = buffer.slice();
  1004. while (getBufferElement(bfrClone, lastUnmaskedPosition, true) != getPlaceHolder(lastUnmaskedPosition) && lastUnmaskedPosition >= p) {
  1005. lastUnmaskedPosition = lastUnmaskedPosition == 0 ? -1 : seekPrevious(buffer, lastUnmaskedPosition);
  1006. }
  1007. if (lastUnmaskedPosition >= p)
  1008. shiftR(p, buffer.length, c);
  1009. else writeOutBuffer = false;
  1010. }
  1011. else setBufferElement(buffer, p, c);
  1012. if (writeOutBuffer) {
  1013. var next = seekNext(buffer, p);
  1014. writeBuffer(input, buffer, next);
  1015. setTimeout(function () { //timeout needed for IE
  1016. if (isComplete(input))
  1017. $input.trigger("complete");
  1018. }, 0);
  1019. }
  1020. } else if (android) writeBuffer(input, buffer, pos.begin);
  1021. }
  1022. e.preventDefault();
  1023. }
  1024. }
  1025. }
  1026. function keyupEvent(e) {
  1027. var $input = $(this), input = this;
  1028. var k = e.keyCode;
  1029. opts.onKeyUp.call(this, e, buffer, opts); //extra stuff to execute on keyup
  1030. if (k == opts.keyCode.TAB && $input.hasClass('focus.inputmask') && input._valueGet().length == 0 && opts.showMaskOnFocus) {
  1031. buffer = _buffer.slice();
  1032. writeBuffer(input, buffer);
  1033. if (!isRTL) caret(input, 0);
  1034. undoBuffer = input._valueGet();
  1035. }
  1036. }
  1037. }
  1038. return this; //return this to expose publics
  1039. };
  1040. }
  1041. })(jQuery);
  1042. /*
  1043. Input Mask plugin extensions
  1044. http://github.com/RobinHerbots/jquery.inputmask
  1045. Copyright (c) 2010 - 2013 Robin Herbots
  1046. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  1047. Version: 1.3.14
  1048. Optional extensions on the jquery.inputmask base
  1049. */
  1050. (function ($) {
  1051. //extra definitions
  1052. $.extend($.inputmask.defaults.definitions, {
  1053. 'A': { //auto uppercasing
  1054. validator: "[A-Za-z]",
  1055. cardinality: 1,
  1056. casing: "upper"
  1057. },
  1058. '#': {
  1059. validator: "[A-Za-z\u0410-\u044F\u0401\u04510-9]",
  1060. cardinality: 1,
  1061. casing: "upper"
  1062. }
  1063. });
  1064. $.extend($.inputmask.defaults.aliases, {
  1065. 'url': {
  1066. mask: "ir",
  1067. placeholder: "",
  1068. separator: "",
  1069. defaultPrefix: "http://",
  1070. regex: {
  1071. urlpre1: new RegExp("[fh]"),
  1072. urlpre2: new RegExp("(ft|ht)"),
  1073. urlpre3: new RegExp("(ftp|htt)"),
  1074. urlpre4: new RegExp("(ftp:|http|ftps)"),
  1075. urlpre5: new RegExp("(ftp:/|ftps:|http:|https)"),
  1076. urlpre6: new RegExp("(ftp://|ftps:/|http:/|https:)"),
  1077. urlpre7: new RegExp("(ftp://|ftps://|http://|https:/)"),
  1078. urlpre8: new RegExp("(ftp://|ftps://|http://|https://)")
  1079. },
  1080. definitions: {
  1081. 'i': {
  1082. validator: function (chrs, buffer, pos, strict, opts) {
  1083. return true;
  1084. },
  1085. cardinality: 8,
  1086. prevalidator: (function () {
  1087. var result = [], prefixLimit = 8;
  1088. for (var i = 0; i < prefixLimit; i++) {
  1089. result[i] = (function () {
  1090. var j = i;
  1091. return {
  1092. validator: function (chrs, buffer, pos, strict, opts) {
  1093. if (opts.regex["urlpre" + (j + 1)]) {
  1094. var tmp = chrs, k;
  1095. if (((j + 1) - chrs.length) > 0) {
  1096. tmp = buffer.join('').substring(0, ((j + 1) - chrs.length)) + "" + tmp;
  1097. }
  1098. var isValid = opts.regex["urlpre" + (j + 1)].test(tmp);
  1099. if (!strict && !isValid) {
  1100. pos = pos - j;
  1101. for (k = 0; k < opts.defaultPrefix.length; k++) {
  1102. buffer[pos] = opts.defaultPrefix[k]; pos++;
  1103. }
  1104. for (k = 0; k < tmp.length - 1; k++) {
  1105. buffer[pos] = tmp[k]; pos++;
  1106. }
  1107. return { "pos": pos };
  1108. }
  1109. return isValid;
  1110. } else {
  1111. return false;
  1112. }
  1113. }, cardinality: j
  1114. };
  1115. })();
  1116. }
  1117. return result;
  1118. })()
  1119. }
  1120. },
  1121. insertMode: false,
  1122. autoUnmask: false
  1123. },
  1124. "ip": {
  1125. mask: "i.i.i.i",
  1126. definitions: {
  1127. 'i': {
  1128. validator: "25[0-5]|2[0-4][0-9]|[01][0-9][0-9]",
  1129. cardinality: 3,
  1130. prevalidator: [
  1131. { validator: "[0-2]", cardinality: 1 },
  1132. { validator: "2[0-5]|[01][0-9]", cardinality: 2 }
  1133. ]
  1134. }
  1135. }
  1136. }
  1137. });
  1138. })(jQuery);/*
  1139. Input Mask plugin extensions
  1140. http://github.com/RobinHerbots/jquery.inputmask
  1141. Copyright (c) 2010 - 2012 Robin Herbots
  1142. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  1143. Version: 1.3.14
  1144. Optional extensions on the jquery.inputmask base
  1145. */
  1146. (function ($) {
  1147. //date & time aliases
  1148. $.extend($.inputmask.defaults.definitions, {
  1149. 'h': { //hours
  1150. validator: "[01][0-9]|2[0-3]",
  1151. cardinality: 2,
  1152. prevalidator: [{ validator: "[0-2]", cardinality: 1 }]
  1153. },
  1154. 's': { //seconds || minutes
  1155. validator: "[0-5][0-9]",
  1156. cardinality: 2,
  1157. prevalidator: [{ validator: "[0-5]", cardinality: 1 }]
  1158. },
  1159. 'd': { //basic day
  1160. validator: "0[1-9]|[12][0-9]|3[01]",
  1161. cardinality: 2,
  1162. prevalidator: [{ validator: "[0-3]", cardinality: 1 }]
  1163. },
  1164. 'm': { //basic month
  1165. validator: "0[1-9]|1[012]",
  1166. cardinality: 2,
  1167. prevalidator: [{ validator: "[01]", cardinality: 1 }]
  1168. },
  1169. 'y': { //basic year
  1170. validator: "(19|20)\\d{2}",
  1171. cardinality: 4,
  1172. prevalidator: [
  1173. { validator: "[12]", cardinality: 1 },
  1174. { validator: "(19|20)", cardinality: 2 },
  1175. { validator: "(19|20)\\d", cardinality: 3 }
  1176. ]
  1177. }
  1178. });
  1179. $.extend($.inputmask.defaults.aliases, {
  1180. 'dd/mm/yyyy': {
  1181. mask: "1/2/y",
  1182. placeholder: "dd/mm/yyyy",
  1183. regex: {
  1184. val1pre: new RegExp("[0-3]"), //daypre
  1185. val1: new RegExp("0[1-9]|[12][0-9]|3[01]"), //day
  1186. val2pre: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[1-9]|[12][0-9]|3[01])" + escapedSeparator + "[01])"); }, //monthpre
  1187. val2: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[1-9]|[12][0-9])" + escapedSeparator + "(0[1-9]|1[012]))|(30" + escapedSeparator + "(0[13-9]|1[012]))|(31" + escapedSeparator + "(0[13578]|1[02]))"); }//month
  1188. },
  1189. leapday: "29/02/",
  1190. separator: '/',
  1191. yearrange: { minyear: 1900, maxyear: 2099 },
  1192. isInYearRange: function (chrs, minyear, maxyear) {
  1193. var enteredyear = parseInt(chrs.concat(minyear.toString().slice(chrs.length)));
  1194. var enteredyear2 = parseInt(chrs.concat(maxyear.toString().slice(chrs.length)));
  1195. return (enteredyear != NaN ? minyear <= enteredyear && enteredyear <= maxyear : false) ||
  1196. (enteredyear2 != NaN ? minyear <= enteredyear2 && enteredyear2 <= maxyear : false);
  1197. },
  1198. determinebaseyear: function (minyear, maxyear) {
  1199. var currentyear = (new Date()).getFullYear();
  1200. if (minyear > currentyear) return minyear;
  1201. if (maxyear < currentyear) return maxyear;
  1202. return currentyear;
  1203. },
  1204. onKeyUp: function (e, buffer, opts) {
  1205. var $input = $(this);
  1206. if (e.ctrlKey && e.keyCode == opts.keyCode.RIGHT) {
  1207. var today = new Date();
  1208. $input.val(today.getDate().toString() + (today.getMonth() + 1).toString() + today.getFullYear().toString());
  1209. }
  1210. },
  1211. definitions: {
  1212. '1': { //val1 ~ day or month
  1213. validator: function (chrs, buffer, pos, strict, opts) {
  1214. var isValid = opts.regex.val1.test(chrs);
  1215. if (!strict && !isValid) {
  1216. if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
  1217. isValid = opts.regex.val1.test("0" + chrs.charAt(0));
  1218. if (isValid) {
  1219. buffer[pos - 1] = "0";
  1220. return { "pos": pos, "c": chrs.charAt(0) };
  1221. }
  1222. }
  1223. }
  1224. return isValid;
  1225. },
  1226. cardinality: 2,
  1227. prevalidator: [{
  1228. validator: function (chrs, buffer, pos, strict, opts) {
  1229. var isValid = opts.regex.val1pre.test(chrs);
  1230. if (!strict && !isValid) {
  1231. isValid = opts.regex.val1.test("0" + chrs);
  1232. if (isValid) {
  1233. buffer[pos] = "0";
  1234. pos++;
  1235. return { "pos": pos };
  1236. }
  1237. }
  1238. return isValid;
  1239. }, cardinality: 1
  1240. }]
  1241. },
  1242. '2': { //val2 ~ day or month
  1243. validator: function (chrs, buffer, pos, strict, opts) {
  1244. var frontValue = buffer.join('').substr(0, 3);
  1245. var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
  1246. if (!strict && !isValid) {
  1247. if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
  1248. isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0));
  1249. if (isValid) {
  1250. buffer[pos - 1] = "0";
  1251. return { "pos": pos, "c": chrs.charAt(0) };
  1252. }
  1253. }
  1254. }
  1255. return isValid;
  1256. },
  1257. cardinality: 2,
  1258. prevalidator: [{
  1259. validator: function (chrs, buffer, pos, strict, opts) {
  1260. var frontValue = buffer.join('').substr(0, 3);
  1261. var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
  1262. if (!strict && !isValid) {
  1263. isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
  1264. if (isValid) {
  1265. buffer[pos] = "0";
  1266. pos++;
  1267. return { "pos": pos };
  1268. }
  1269. }
  1270. return isValid;
  1271. }, cardinality: 1
  1272. }]
  1273. },
  1274. 'y': { //year
  1275. validator: function (chrs, buffer, pos, strict, opts) {
  1276. if (opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) {
  1277. var dayMonthValue = buffer.join('').substr(0, 6);
  1278. if (dayMonthValue != opts.leapday)
  1279. return true;
  1280. else {
  1281. var year = parseInt(chrs, 10);//detect leap year
  1282. if (year % 4 === 0)
  1283. if (year % 100 === 0)
  1284. if (year % 400 === 0)
  1285. return true;
  1286. else return false;
  1287. else return true;
  1288. else return false;
  1289. }
  1290. } else return false;
  1291. },
  1292. cardinality: 4,
  1293. prevalidator: [
  1294. {
  1295. validator: function (chrs, buffer, pos, strict, opts) {
  1296. var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  1297. if (!strict && !isValid) {
  1298. var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear).toString().slice(0, 1);
  1299. isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  1300. if (isValid) {
  1301. buffer[pos++] = yearPrefix[0];
  1302. return { "pos": pos };
  1303. }
  1304. yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear).toString().slice(0, 2);
  1305. isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  1306. if (isValid) {
  1307. buffer[pos++] = yearPrefix[0];
  1308. buffer[pos++] = yearPrefix[1];
  1309. return { "pos": pos };
  1310. }
  1311. }
  1312. return isValid;
  1313. },
  1314. cardinality: 1
  1315. },
  1316. {
  1317. validator: function (chrs, buffer, pos, strict, opts) {
  1318. var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  1319. if (!strict && !isValid) {
  1320. var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear).toString().slice(0, 2);
  1321. isValid = opts.isInYearRange(yearPrefix + chrs[1], opts.yearrange.minyear, opts.yearrange.maxyear);
  1322. if (isValid) {
  1323. buffer[pos++] = yearPrefix[1];
  1324. return { "pos": pos };
  1325. }
  1326. yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear).toString().slice(0, 2);
  1327. if (opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) {
  1328. var dayMonthValue = buffer.join('').substr(0, 6);
  1329. if (dayMonthValue != opts.leapday)
  1330. isValid = true;
  1331. else {
  1332. var year = parseInt(chrs, 10);//detect leap year
  1333. if (year % 4 === 0)
  1334. if (year % 100 === 0)
  1335. if (year % 400 === 0)
  1336. isValid = true;
  1337. else isValid = false;
  1338. else isValid = true;
  1339. else isValid = false;
  1340. }
  1341. } else isValid = false;
  1342. if (isValid) {
  1343. buffer[pos - 1] = yearPrefix[0];
  1344. buffer[pos++] = yearPrefix[1];
  1345. buffer[pos++] = chrs[0];
  1346. return { "pos": pos };
  1347. }
  1348. }
  1349. return isValid;
  1350. }, cardinality: 2
  1351. },
  1352. {
  1353. validator: function (chrs, buffer, pos, strict, opts) {
  1354. return opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
  1355. }, cardinality: 3
  1356. }
  1357. ]
  1358. }
  1359. },
  1360. insertMode: false,
  1361. autoUnmask: false
  1362. },
  1363. 'mm/dd/yyyy': {
  1364. placeholder: "mm/dd/yyyy",
  1365. alias: "dd/mm/yyyy", //reuse functionality of dd/mm/yyyy alias
  1366. regex: {
  1367. val2pre: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[13-9]|1[012])" + escapedSeparator + "[0-3])|(02" + escapedSeparator + "[0-2])"); }, //daypre
  1368. val2: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[1-9]|1[012])" + escapedSeparator + "(0[1-9]|[12][0-9]))|((0[13-9]|1[012])" + escapedSeparator + "30)|((0[13578]|1[02])" + escapedSeparator + "31)"); }, //day
  1369. val1pre: new RegExp("[01]"), //monthpre
  1370. val1: new RegExp("0[1-9]|1[012]") //month
  1371. },
  1372. leapday: "02/29/",
  1373. onKeyUp: function (e, buffer, opts) {
  1374. var $input = $(this);
  1375. if (e.ctrlKey && e.keyCode == opts.keyCode.RIGHT) {
  1376. var today = new Date();
  1377. $input.val((today.getMonth() + 1).toString() + today.getDate().toString() + today.getFullYear().toString());
  1378. }
  1379. }
  1380. },
  1381. 'yyyy/mm/dd': {
  1382. mask: "y/1/2",
  1383. placeholder: "yyyy/mm/dd",
  1384. alias: "mm/dd/yyyy",
  1385. leapday: "/02/29",
  1386. onKeyUp: function (e, buffer, opts) {
  1387. var $input = $(this);
  1388. if (e.ctrlKey && e.keyCode == opts.keyCode.RIGHT) {
  1389. var today = new Date();
  1390. $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString());
  1391. }
  1392. },
  1393. definitions: {
  1394. '2': { //val2 ~ day or month
  1395. validator: function (chrs, buffer, pos, strict, opts) {
  1396. var frontValue = buffer.join('').substr(5, 3);
  1397. var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
  1398. if (!strict && !isValid) {
  1399. if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
  1400. isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0));
  1401. if (isValid) {
  1402. buffer[pos - 1] = "0";
  1403. return { "pos": pos, "c": chrs.charAt(0) };
  1404. }
  1405. }
  1406. }
  1407. //check leap yeap
  1408. if (isValid) {
  1409. var dayMonthValue = buffer.join('').substr(4, 4) + chrs;
  1410. if (dayMonthValue != opts.leapday)
  1411. return true;
  1412. else {
  1413. var year = parseInt(buffer.join('').substr(0, 4), 10); //detect leap year
  1414. if (year % 4 === 0)
  1415. if (year % 100 === 0)
  1416. if (year % 400 === 0)
  1417. return true;
  1418. else return false;
  1419. else return true;
  1420. else return false;
  1421. }
  1422. }
  1423. return isValid;
  1424. },
  1425. cardinality: 2,
  1426. prevalidator: [{
  1427. validator: function (chrs, buffer, pos, strict, opts) {
  1428. var frontValue = buffer.join('').substr(5, 3);
  1429. var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
  1430. if (!strict && !isValid) {
  1431. isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
  1432. if (isValid) {
  1433. buffer[pos] = "0";
  1434. pos++;
  1435. return { "pos": pos };
  1436. }
  1437. }
  1438. return isValid;
  1439. }, cardinality: 1
  1440. }]
  1441. }
  1442. }
  1443. },
  1444. 'dd.mm.yyyy': {
  1445. mask: "1.2.y",
  1446. placeholder: "dd.mm.yyyy",
  1447. leapday: "29.02.",
  1448. separator: '.',
  1449. alias: "dd/mm/yyyy"
  1450. },
  1451. 'dd-mm-yyyy': {
  1452. mask: "1-2-y",
  1453. placeholder: "dd-mm-yyyy",
  1454. leapday: "29-02-",
  1455. separator: '-',
  1456. alias: "dd/mm/yyyy"
  1457. },
  1458. 'mm.dd.yyyy': {
  1459. mask: "1.2.y",
  1460. placeholder: "mm.dd.yyyy",
  1461. leapday: "02.29.",
  1462. separator: '.',
  1463. alias: "mm/dd/yyyy"
  1464. },
  1465. 'mm-dd-yyyy': {
  1466. mask: "1-2-y",
  1467. placeholder: "mm-dd-yyyy",
  1468. leapday: "02-29-",
  1469. separator: '-',
  1470. alias: "mm/dd/yyyy"
  1471. },
  1472. 'yyyy.mm.dd': {
  1473. mask: "y.1.2",
  1474. placeholder: "yyyy.mm.dd",
  1475. leapday: ".02.29",
  1476. separator: '.',
  1477. alias: "yyyy/mm/dd"
  1478. },
  1479. 'yyyy-mm-dd': {
  1480. mask: "y-1-2",
  1481. placeholder: "yyyy-mm-dd",
  1482. leapday: "-02-29",
  1483. separator: '-',
  1484. alias: "yyyy/mm/dd"
  1485. },
  1486. 'datetime': {
  1487. mask: "1/2/y h:s",
  1488. placeholder: "dd/mm/yyyy hh:mm",
  1489. alias: "dd/mm/yyyy",
  1490. regex: {
  1491. hrspre: new RegExp("[012]"), //hours pre
  1492. hrs24: new RegExp("2[0-9]|1[3-9]"),
  1493. hrs: new RegExp("[01][0-9]|2[0-3]"), //hours
  1494. ampmpre: new RegExp("[apAP]"),
  1495. ampm: new RegExp("^[a|p|A|P][m|M]")
  1496. },
  1497. timeseparator: ':',
  1498. hourFormat: "24", // or 12
  1499. definitions: {
  1500. 'h': { //hours
  1501. validator: function (chrs, buffer, pos, strict, opts) {
  1502. var isValid = opts.regex.hrs.test(chrs);
  1503. if (!strict && !isValid) {
  1504. if (chrs.charAt(1) == opts.timeseparator || "-.:".indexOf(chrs.charAt(1)) != -1) {
  1505. isValid = opts.regex.hrs.test("0" + chrs.charAt(0));
  1506. if (isValid) {
  1507. buffer[pos - 1] = "0";
  1508. buffer[pos] = chrs.charAt(0);
  1509. pos++;
  1510. return { "pos": pos };
  1511. }
  1512. }
  1513. }
  1514. if (isValid && opts.hourFormat !== "24" && opts.regex.hrs24.test(chrs)) {
  1515. var tmp = parseInt(chrs, 10);
  1516. if (tmp == 24) {
  1517. buffer[pos + 5] = "a";
  1518. buffer[pos + 6] = "m";
  1519. } else {
  1520. buffer[pos + 5] = "p";
  1521. buffer[pos + 6] = "m";
  1522. }
  1523. tmp = tmp - 12;
  1524. if (tmp < 10) {
  1525. buffer[pos] = tmp.toString();
  1526. buffer[pos - 1] = "0";
  1527. } else {
  1528. buffer[pos] = tmp.toString().charAt(1);
  1529. buffer[pos - 1] = tmp.toString().charAt(0);
  1530. }
  1531. return { "pos": pos, "c": buffer[pos] };
  1532. }
  1533. return isValid;
  1534. },
  1535. cardinality: 2,
  1536. prevalidator: [{
  1537. validator: function (chrs, buffer, pos, strict, opts) {
  1538. var isValid = opts.regex.hrspre.test(chrs);
  1539. if (!strict && !isValid) {
  1540. isValid = opts.regex.hrs.test("0" + chrs);
  1541. if (isValid) {
  1542. buffer[pos] = "0";
  1543. pos++;
  1544. return { "pos": pos };
  1545. }
  1546. }
  1547. return isValid;
  1548. }, cardinality: 1
  1549. }]
  1550. },
  1551. 't': { //am/pm
  1552. validator: function (chrs, buffer, pos, strict, opts) {
  1553. var isValid = opts.regex.ampm.test(chrs);
  1554. if (!strict && !isValid) {
  1555. isValid = opts.regex.ampm.test(chrs + 'm');
  1556. if (isValid) {
  1557. buffer[pos - 1] = chrs.charAt(0);
  1558. buffer[pos] = "m";
  1559. pos++;
  1560. return pos;
  1561. }
  1562. }
  1563. return isValid;
  1564. },
  1565. casing: "lower",
  1566. cardinality: 2,
  1567. prevalidator: [{
  1568. validator: function (chrs, buffer, pos, strict, opts) {
  1569. var isValid = opts.regex.ampmpre.test(chrs);
  1570. if (isValid) {
  1571. isValid = opts.regex.ampm.test(chrs + "m");
  1572. if (isValid) {
  1573. buffer[pos] = chrs;
  1574. buffer[pos + 1] = 'm';
  1575. return pos;
  1576. }
  1577. }
  1578. return isValid;
  1579. }, cardinality: 1
  1580. }]
  1581. }
  1582. },
  1583. insertMode: false,
  1584. autoUnmask: false
  1585. },
  1586. 'datetime12': {
  1587. mask: "1/2/y h:s t",
  1588. placeholder: "dd/mm/yyyy hh:mm xm",
  1589. alias: "datetime",
  1590. hourFormat: "12"
  1591. },
  1592. 'hh:mm t': {
  1593. mask: "h:s t",
  1594. placeholder: "hh:mm xm",
  1595. alias: "datetime",
  1596. hourFormat: "12"
  1597. },
  1598. 'h:s t': {
  1599. mask: "h:s t",
  1600. placeholder: "hh:mm xm",
  1601. alias: "datetime",
  1602. hourFormat: "12"
  1603. },
  1604. 'hh:mm:ss': {
  1605. mask: "h:s:s",
  1606. autoUnmask: false
  1607. },
  1608. 'hh:mm': {
  1609. mask: "h:s",
  1610. autoUnmask: false
  1611. },
  1612. 'date': {
  1613. alias: "dd/mm/yyyy" // "mm/dd/yyyy"
  1614. }
  1615. });
  1616. })(jQuery);/*
  1617. Input Mask plugin extensions
  1618. http://github.com/RobinHerbots/jquery.inputmask
  1619. Copyright (c) 2010 - 2013 Robin Herbots
  1620. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  1621. Version: 1.3.14
  1622. Optional extensions on the jquery.inputmask base
  1623. */
  1624. (function ($) {
  1625. //number aliases
  1626. $.extend($.inputmask.defaults.aliases, {
  1627. 'decimal': {
  1628. mask: "~",
  1629. placeholder: "",
  1630. repeat: 10,
  1631. greedy: false,
  1632. numericInput: true,
  1633. digits: "*", //numer of digits
  1634. groupSeparator: ",", // | "."
  1635. radixPoint: ".",
  1636. groupSize: 3,
  1637. autoGroup: false,
  1638. getMaskLength: function (buffer, greedy, repeat, currentBuffer, opts) { //custom getMaskLength to take the groupSeparator into account
  1639. var calculatedLength = buffer.length;
  1640. if (!greedy && repeat > 1) {
  1641. calculatedLength += (buffer.length * (repeat - 1));
  1642. }
  1643. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  1644. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
  1645. var currentBufferStr = currentBuffer.join(''), strippedBufferStr = currentBufferStr.replace(new RegExp(escapedGroupSeparator, "g"), "").replace(new RegExp(escapedRadixPoint), ""),
  1646. groupOffset = currentBufferStr.length - strippedBufferStr.length;
  1647. return calculatedLength + groupOffset;
  1648. },
  1649. postFormat: function (buffer, pos, reformatOnly, opts) {
  1650. var cbuf = buffer.slice();
  1651. if (!reformatOnly) cbuf.splice(pos, 0, "?"); //set position indicator
  1652. var bufVal = cbuf.join('');
  1653. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  1654. bufVal = bufVal.replace(new RegExp("\\" + opts.groupSeparator, "g"), '');
  1655. var radixSplit = bufVal.split(opts.radixPoint);
  1656. bufVal = radixSplit[0];
  1657. var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
  1658. while (reg.test(bufVal)) {
  1659. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  1660. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  1661. }
  1662. if (radixSplit.length > 1)
  1663. bufVal += opts.radixPoint + radixSplit[1];
  1664. }
  1665. buffer.length = bufVal.length; //align the length
  1666. for (var i = 0, l = bufVal.length; i < l; i++) {
  1667. buffer[i] = bufVal.charAt(i);
  1668. }
  1669. var newPos = reformatOnly ? pos : $.inArray("?", buffer);
  1670. if (!reformatOnly) buffer.splice(newPos, 1);
  1671. return newPos;
  1672. },
  1673. regex: {
  1674. number: function (groupSeparator, groupSize, radixPoint, digits) {
  1675. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  1676. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, radixPoint);
  1677. var digitExpression = isNaN(digits) ? digits : '{0,' + digits + '}';
  1678. return new RegExp("^[\+-]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
  1679. }
  1680. },
  1681. onKeyDown: function (e, buffer, opts) {
  1682. var $input = $(this), input = this;
  1683. if (e.keyCode == opts.keyCode.TAB) {
  1684. var radixPosition = $.inArray(opts.radixPoint, buffer);
  1685. if (radixPosition != -1) {
  1686. for (var i = 1; i <= opts.digits && i < opts.getMaskLength($input.data('inputmask')['_buffer'], opts.greedy, opts.repeat, buffer, opts) ; i++) {
  1687. if (buffer[radixPosition + i] == undefined) buffer[radixPosition + i] = "0";
  1688. }
  1689. input._valueSet(buffer.join(''));
  1690. }
  1691. } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
  1692. opts.postFormat(buffer, 0, true, opts);
  1693. input._valueSet(buffer.join(''));
  1694. }
  1695. },
  1696. definitions: {
  1697. '~': { //real number
  1698. validator: function (chrs, buffer, pos, strict, opts) {
  1699. if (chrs == "") return false;
  1700. if (pos == 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs)) { //handle first char
  1701. buffer[0] = "";
  1702. return { "pos": 0 };
  1703. }
  1704. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  1705. cbuf.splice(pos + 1, 0, chrs);
  1706. var bufferStr = cbuf.join('');
  1707. if (opts.autoGroup && !strict) //strip groupseparator
  1708. bufferStr = bufferStr.replace(new RegExp("\\" + opts.groupSeparator, "g"), '');
  1709. var isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  1710. if (!isValid) {
  1711. //let's help the regex a bit
  1712. bufferStr += "0";
  1713. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  1714. if (!isValid) {
  1715. //make a valid group
  1716. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  1717. for (i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  1718. bufferStr += "0";
  1719. }
  1720. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  1721. if (!isValid && !strict) {
  1722. if (chrs == opts.radixPoint) {
  1723. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test("0" + bufferStr + "0");
  1724. if (isValid) {
  1725. buffer[pos] = "0";
  1726. pos++;
  1727. return { "pos": pos };
  1728. }
  1729. }
  1730. }
  1731. }
  1732. }
  1733. if (isValid != false && !strict && chrs != opts.radixPoint) {
  1734. var newPos = opts.postFormat(buffer, pos + 1, false, opts);
  1735. return { "pos": newPos };
  1736. }
  1737. return isValid;
  1738. },
  1739. cardinality: 1,
  1740. prevalidator: null
  1741. }
  1742. },
  1743. insertMode: true,
  1744. autoUnmask: false
  1745. },
  1746. 'non-negative-decimal': {
  1747. regex: {
  1748. number: function (groupSeparator, groupSize, radixPoint, digits) {
  1749. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  1750. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, radixPoint);
  1751. var digitExpression = isNaN(digits) ? digits : '{0,' + digits + '}'
  1752. return new RegExp("^[\+]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
  1753. }
  1754. },
  1755. alias: "decimal"
  1756. },
  1757. 'integer': {
  1758. regex: {
  1759. number: function (groupSeparator, groupSize) {
  1760. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  1761. return new RegExp("^[\+-]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)$");
  1762. }
  1763. },
  1764. alias: "decimal"
  1765. }
  1766. });
  1767. })(jQuery);