jquery.inputmask.bundle.js 92 KB

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