jquery.inputmask.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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: 0.0.0
  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) && (version <= 534);
  84. //}
  85. if (typeof fn == "string") {
  86. switch (fn) {
  87. case "mask":
  88. //resolve possible aliases given by options
  89. resolveAlias(opts.alias, options);
  90. //init buffer
  91. var _buffer = getMaskTemplate();
  92. var tests = getTestingChain();
  93. return this.each(function () {
  94. mask(this);
  95. });
  96. break;
  97. case "unmaskedvalue":
  98. var tests = this.data('inputmask')['tests'];
  99. var _buffer = this.data('inputmask')['_buffer'];
  100. opts.greedy = this.data('inputmask')['greedy'];
  101. opts.repeat = this.data('inputmask')['repeat'];
  102. opts.definitions = this.data('inputmask')['definitions'];
  103. return unmaskedvalue(this);
  104. break;
  105. case "remove":
  106. var tests, _buffer;
  107. return this.each(function () {
  108. var $input = $(this), input = this;
  109. setTimeout(function () {
  110. if ($input.data('inputmask')) {
  111. tests = $input.data('inputmask')['tests'];
  112. _buffer = $input.data('inputmask')['_buffer'];
  113. opts.greedy = $input.data('inputmask')['greedy'];
  114. opts.repeat = $input.data('inputmask')['repeat'];
  115. opts.definitions = $input.data('inputmask')['definitions'];
  116. //writeout the unmaskedvalue
  117. input._valueSet(unmaskedvalue($input, true));
  118. //clear data
  119. $input.removeData('inputmask');
  120. //unbind all events
  121. $input.unbind(".inputmask");
  122. $input.removeClass('focus.inputmask');
  123. //restore the value property
  124. var valueProperty;
  125. if (Object.getOwnPropertyDescriptor)
  126. valueProperty = Object.getOwnPropertyDescriptor(input, "value");
  127. if (valueProperty && valueProperty.get) {
  128. if (input._valueGet) {
  129. Object.defineProperty(input, "value", {
  130. get: input._valueGet,
  131. set: input._valueSet
  132. });
  133. }
  134. } else if (document.__lookupGetter__ && input.__lookupGetter__("value")) {
  135. if (input._valueGet) {
  136. input.__defineGetter__("value", input._valueGet);
  137. input.__defineSetter__("value", input._valueSet);
  138. }
  139. }
  140. delete input._valueGet;
  141. delete input._valueSet;
  142. }
  143. }, 0);
  144. });
  145. break;
  146. case "getemptymask": //return the default (empty) mask value, usefull for setting the default value in validation
  147. if (this.data('inputmask'))
  148. return this.data('inputmask')['_buffer'].join('');
  149. else return "";
  150. case "hasMaskedValue": //check wheter the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  151. return this.data('inputmask') ? !this.data('inputmask')['autoUnmask'] : false;
  152. case "isComplete":
  153. var tests = this.data('inputmask')['tests'];
  154. var _buffer = this.data('inputmask')['_buffer'];
  155. opts.greedy = this.data('inputmask')['greedy'];
  156. opts.repeat = this.data('inputmask')['repeat'];
  157. opts.definitions = this.data('inputmask')['definitions'];
  158. return isComplete(this[0]);
  159. default:
  160. //check if the fn is an alias
  161. if (!resolveAlias(fn, options)) {
  162. //maybe fn is a mask so we try
  163. //set mask
  164. opts.mask = fn;
  165. }
  166. //init buffer
  167. var _buffer = getMaskTemplate();
  168. var tests = getTestingChain();
  169. return this.each(function () {
  170. mask(this);
  171. });
  172. break;
  173. }
  174. } else if (typeof fn == "object") {
  175. opts = $.extend(true, {}, $.inputmask.defaults, fn);
  176. resolveAlias(opts.alias, fn); //resolve aliases
  177. //init buffer
  178. var _buffer = getMaskTemplate();
  179. var tests = getTestingChain();
  180. return this.each(function () {
  181. mask(this);
  182. });
  183. } else if (fn == undefined) {
  184. //look for data-inputmask atribute - the attribute should only contain optipns
  185. return this.each(function () {
  186. var attrOptions = $(this).attr("data-inputmask");
  187. if (attrOptions && attrOptions != "") {
  188. try {
  189. attrOptions = attrOptions.replace(new RegExp("'", "g"), '"');
  190. var options = $.parseJSON("{" + attrOptions + "}");
  191. opts = $.extend(true, {}, $.inputmask.defaults, options);
  192. resolveAlias(opts.alias, options);
  193. opts.alias = undefined;
  194. $(this).inputmask(opts);
  195. } catch (ex) { } //need a more relax parseJSON
  196. }
  197. });
  198. }
  199. //helper functions
  200. function isInputEventSupported(eventName) {
  201. var el = document.createElement('input'),
  202. eventName = 'on' + eventName,
  203. isSupported = (eventName in el);
  204. if (!isSupported) {
  205. el.setAttribute(eventName, 'return;');
  206. isSupported = typeof el[eventName] == 'function';
  207. }
  208. el = null;
  209. return isSupported;
  210. }
  211. function resolveAlias(aliasStr, options) {
  212. var aliasDefinition = opts.aliases[aliasStr];
  213. if (aliasDefinition) {
  214. if (aliasDefinition.alias) resolveAlias(aliasDefinition.alias); //alias is another alias
  215. $.extend(true, opts, aliasDefinition); //merge alias definition in the options
  216. $.extend(true, opts, options); //reapply extra given options
  217. return true;
  218. }
  219. return false;
  220. }
  221. function getMaskTemplate() {
  222. var escaped = false, outCount = 0, mask = opts.mask.toString();
  223. if (mask.length == 1 && opts.greedy == false) { opts.placeholder = ""; } //hide placeholder with single non-greedy mask
  224. var singleMask = $.map(mask.split(""), function (element, index) {
  225. var outElem = [];
  226. if (element == opts.escapeChar) {
  227. escaped = true;
  228. }
  229. else if ((element != opts.optionalmarker.start && element != opts.optionalmarker.end) || escaped) {
  230. var maskdef = opts.definitions[element];
  231. if (maskdef && !escaped) {
  232. for (var i = 0; i < maskdef.cardinality; i++) {
  233. outElem.push(getPlaceHolder(outCount + i));
  234. }
  235. } else {
  236. outElem.push(element);
  237. escaped = false;
  238. }
  239. outCount += outElem.length;
  240. return outElem;
  241. }
  242. });
  243. //allocate repetitions
  244. var repeatedMask = singleMask.slice();
  245. for (var i = 1; i < opts.repeat && opts.greedy; i++) {
  246. repeatedMask = repeatedMask.concat(singleMask.slice());
  247. }
  248. return repeatedMask;
  249. }
  250. //test definition => {fn: RegExp/function, cardinality: int, optionality: bool, newBlockMarker: bool, offset: int, casing: null/upper/lower, def: definitionSymbol}
  251. function getTestingChain() {
  252. var isOptional = false, escaped = false, mask = opts.mask.toString();
  253. var newBlockMarker = false; //indicates wheter the begin/ending of a block should be indicated
  254. return $.map(mask.split(""), function (element, index) {
  255. var outElem = [];
  256. if (element == opts.escapeChar) {
  257. escaped = true;
  258. } else if (element == opts.optionalmarker.start && !escaped) {
  259. isOptional = true;
  260. newBlockMarker = true;
  261. }
  262. else if (element == opts.optionalmarker.end && !escaped) {
  263. isOptional = false;
  264. newBlockMarker = true;
  265. }
  266. else {
  267. var maskdef = opts.definitions[element];
  268. if (maskdef && !escaped) {
  269. var prevalidators = maskdef["prevalidator"], prevalidatorsL = prevalidators ? prevalidators.length : 0;
  270. for (var i = 1; i < maskdef.cardinality; i++) {
  271. var prevalidator = prevalidatorsL >= i ? prevalidators[i - 1] : [], validator = prevalidator["validator"], cardinality = prevalidator["cardinality"];
  272. 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 });
  273. if (isOptional == true) //reset newBlockMarker
  274. newBlockMarker = false;
  275. }
  276. 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 });
  277. } else {
  278. outElem.push({ fn: null, cardinality: 0, optionality: isOptional, newBlockMarker: newBlockMarker, offset: 0, casing: null, def: element });
  279. escaped = false;
  280. }
  281. //reset newBlockMarker
  282. newBlockMarker = false;
  283. return outElem;
  284. }
  285. });
  286. }
  287. function isValid(pos, c, buffer, strict) { //strict true ~ no correction or autofill
  288. var result = false;
  289. if (pos >= 0 && pos < getMaskLength(buffer)) {
  290. var testPos = determineTestPosition(pos), loopend = c ? 1 : 0, chrs = '';
  291. for (var i = tests[testPos].cardinality; i > loopend; i--) {
  292. chrs += getBufferElement(buffer, testPos - (i - 1));
  293. }
  294. if (c) {
  295. chrs += c;
  296. }
  297. //return is false or a json object => { pos: ??, c: ??}
  298. result = tests[testPos].fn != null ? tests[testPos].fn.test(chrs, buffer, pos, strict, opts) : false;
  299. }
  300. setTimeout(function () { opts.onKeyValidation.call(this, result, opts); }, 0); //extra stuff to execute on keydown
  301. return result;
  302. }
  303. function isMask(pos) {
  304. var testPos = determineTestPosition(pos);
  305. var test = tests[testPos];
  306. return test != undefined ? test.fn : false;
  307. }
  308. function determineTestPosition(pos) {
  309. return pos % tests.length;
  310. }
  311. function getPlaceHolder(pos) {
  312. return opts.placeholder.charAt(pos % opts.placeholder.length);
  313. }
  314. function getMaskLength(currentBuffer) {
  315. return opts.getMaskLength(_buffer, opts.greedy, opts.repeat, currentBuffer, opts);
  316. }
  317. //pos: from position
  318. function seekNext(buffer, pos) {
  319. var maskL = getMaskLength(buffer);
  320. if (pos >= maskL) return maskL;
  321. var position = pos;
  322. while (++position < maskL && !isMask(position)) { };
  323. return position;
  324. }
  325. //pos: from position
  326. function seekPrevious(buffer, pos) {
  327. var position = pos;
  328. if (position <= 0) return 0;
  329. while (--position > 0 && !isMask(position)) { };
  330. return position;
  331. }
  332. function setBufferElement(buffer, position, element) {
  333. //position = prepareBuffer(buffer, position);
  334. var test = tests[determineTestPosition(position)];
  335. var elem = element;
  336. if (elem != undefined) {
  337. switch (test.casing) {
  338. case "upper":
  339. elem = element.toUpperCase();
  340. break;
  341. case "lower":
  342. elem = element.toLowerCase();
  343. break;
  344. }
  345. }
  346. buffer[position] = elem;
  347. }
  348. function getBufferElement(buffer, position, autoPrepare) {
  349. if (autoPrepare) position = prepareBuffer(buffer, position);
  350. return buffer[position];
  351. }
  352. //needed to handle the non-greedy mask repetitions
  353. function prepareBuffer(buffer, position, isRTL) {
  354. var j;
  355. if (isRTL) {
  356. while (position < 0 && buffer.length < getMaskLength(buffer)) {
  357. j = _buffer.length - 1;
  358. position = _buffer.length;
  359. while (_buffer[j] !== undefined) {
  360. buffer.unshift(_buffer[j--]);
  361. }
  362. }
  363. } else {
  364. while (buffer[position] == undefined && buffer.length < getMaskLength(buffer)) {
  365. j = 0;
  366. while (_buffer[j] !== undefined) { //add a new buffer
  367. buffer.push(_buffer[j++]);
  368. }
  369. }
  370. }
  371. return position;
  372. }
  373. function writeBuffer(input, buffer, caretPos) {
  374. input._valueSet(buffer.join(''));
  375. if (caretPos != undefined) {
  376. caret(input, caretPos);
  377. }
  378. };
  379. function clearBuffer(buffer, start, end) {
  380. for (var i = start, maskL = getMaskLength(buffer) ; i < end && i < maskL; i++) {
  381. setBufferElement(buffer, i, getBufferElement(_buffer.slice(), i));
  382. }
  383. };
  384. function setReTargetPlaceHolder(buffer, pos) {
  385. var testPos = determineTestPosition(pos);
  386. setBufferElement(buffer, pos, getBufferElement(_buffer, testPos));
  387. }
  388. function checkVal(input, buffer, clearInvalid, skipRadixHandling) {
  389. var isRTL = $(input).data('inputmask')['isRTL'],
  390. inputValue = truncateInput(input._valueGet(), isRTL).split('');
  391. var maskL = getMaskLength(buffer);
  392. if (isRTL) { //align inputValue for RTL/numeric input
  393. var inputValueRev = inputValue.reverse(); inputValueRev.length = maskL;
  394. for (var i = 0; i < maskL; i++) {
  395. var targetPosition = determineTestPosition(maskL - (i + 1));
  396. if (tests[targetPosition].fn == null && inputValueRev[i] != getBufferElement(_buffer, targetPosition)) {
  397. inputValueRev.splice(i, 0, getBufferElement(_buffer, targetPosition));
  398. inputValueRev.length = maskL;
  399. } else {
  400. inputValueRev[i] = inputValueRev[i] || getBufferElement(_buffer, targetPosition);
  401. }
  402. }
  403. inputValue = inputValueRev.reverse();
  404. }
  405. clearBuffer(buffer, 0, buffer.length);
  406. buffer.length = _buffer.length;
  407. var lastMatch = -1, checkPosition = -1, np, ivl = inputValue.length, rtlMatch = ivl == 0 ? maskL : -1;
  408. for (var i = 0; i < ivl; i++) {
  409. for (var pos = checkPosition + 1; pos < maskL; pos++) {
  410. if (isMask(pos)) {
  411. var c = inputValue[i];
  412. if ((np = isValid(pos, c, buffer, !clearInvalid)) !== false) {
  413. if (np !== true) {
  414. pos = np.pos != undefined ? np.pos : pos; //set new position from isValid
  415. c = np.c != undefined ? np.c : c; //set new char from isValid
  416. }
  417. setBufferElement(buffer, pos, c);
  418. lastMatch = checkPosition = pos;
  419. } else {
  420. setReTargetPlaceHolder(buffer, pos);
  421. if (c == getPlaceHolder(pos)) {
  422. checkPosition = pos;
  423. rtlMatch = pos;
  424. }
  425. }
  426. break;
  427. } else { //nonmask
  428. setReTargetPlaceHolder(buffer, pos);
  429. if (lastMatch == checkPosition) //once outsync the nonmask cannot be the lastmatch
  430. lastMatch = pos;
  431. checkPosition = pos;
  432. if (inputValue[i] == getBufferElement(buffer, pos))
  433. break;
  434. }
  435. }
  436. }
  437. //Truncate buffer when using non-greedy masks
  438. if (opts.greedy == false) {
  439. var newBuffer = truncateInput(buffer.join(''), isRTL).split('');
  440. while (buffer.length != newBuffer.length) { //map changes into the original buffer
  441. isRTL ? buffer.shift() : buffer.pop();
  442. }
  443. }
  444. if (clearInvalid) {
  445. writeBuffer(input, buffer);
  446. }
  447. 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);
  448. }
  449. function escapeRegex(str) {
  450. return $.inputmask.escapeRegex.call(this, str);
  451. }
  452. function truncateInput(inputValue, rtl) {
  453. return rtl ? inputValue.replace(new RegExp("^(" + escapeRegex(_buffer.join('')) + ")*"), "") : inputValue.replace(new RegExp("(" + escapeRegex(_buffer.join('')) + ")*$"), "");
  454. }
  455. function clearOptionalTail(input, buffer) {
  456. checkVal(input, buffer, false);
  457. var tmpBuffer = buffer.slice(), testPos, pos;
  458. if ($(input).data('inputmask')['isRTL']) {
  459. for (pos = 0; pos <= tmpBuffer.length - 1; pos++) {
  460. testPos = determineTestPosition(pos);
  461. if (tests[testPos].optionality) {
  462. if (!isMask(pos) || !isValid(pos, buffer[pos], buffer, true))
  463. tmpBuffer.splice(0, 1);
  464. else break;
  465. } else break;
  466. }
  467. } else {
  468. for (pos = tmpBuffer.length - 1; pos >= 0; pos--) {
  469. testPos = determineTestPosition(pos);
  470. if (tests[testPos].optionality) {
  471. if (!isMask(pos) || !isValid(pos, buffer[pos], buffer, true))
  472. tmpBuffer.pop();
  473. else break;
  474. } else break;
  475. }
  476. }
  477. writeBuffer(input, tmpBuffer);
  478. }
  479. //functionality fn
  480. function unmaskedvalue($input, skipDatepickerCheck) {
  481. var input = $input[0];
  482. if (tests && (skipDatepickerCheck === true || !$input.hasClass('hasDatepicker'))) {
  483. var buffer = _buffer.slice();
  484. checkVal(input, buffer);
  485. return $.map(buffer, function (element, index) {
  486. return isMask(index) && isValid(index, element, buffer, true) ? element : null;
  487. }).join('');
  488. }
  489. else {
  490. return input._valueGet();
  491. }
  492. }
  493. var caretSavePoint;
  494. function caret(input, begin, end) {
  495. var npt = input.jquery && input.length > 0 ? input[0] : input;
  496. if (typeof begin == 'number') {
  497. if (!$(input).is(':visible')) {
  498. return;
  499. }
  500. end = (typeof end == 'number') ? end : begin;
  501. if (opts.insertMode == false && begin == end) end++; //set visualization for insert/overwrite mode
  502. if (npt.setSelectionRange) {
  503. if (android) {
  504. setTimeout(function () {
  505. npt.selectionStart = begin;
  506. npt.selectionEnd = android ? begin : end;
  507. }, 10);
  508. caretSavePoint = { "begin": begin, "end": end };
  509. }
  510. else {
  511. npt.selectionStart = begin;
  512. npt.selectionEnd = end;
  513. }
  514. } else if (npt.createTextRange) {
  515. var range = npt.createTextRange();
  516. range.collapse(true);
  517. range.moveEnd('character', end);
  518. range.moveStart('character', begin);
  519. range.select();
  520. }
  521. } else {
  522. if (!$(input).is(':visible')) {
  523. return { "begin": 0, "end": 0 };
  524. }
  525. if (npt.setSelectionRange) {
  526. begin = npt.selectionStart;
  527. end = npt.selectionEnd;
  528. } else if (document.selection && document.selection.createRange) {
  529. var range = document.selection.createRange();
  530. begin = 0 - range.duplicate().moveStart('character', -100000);
  531. end = begin + range.text.length;
  532. }
  533. return { "begin": begin, "end": end };
  534. }
  535. };
  536. function isComplete(npt) {
  537. var complete = true, nptValue = npt._valueGet(), ml = nptValue.length;
  538. for (var i = 0; i < ml; i++) {
  539. if (isMask(i) && nptValue.charAt(i) == getPlaceHolder(i)) {
  540. complete = false;
  541. break;
  542. }
  543. }
  544. return complete;
  545. }
  546. function mask(el) {
  547. var $input = $(el);
  548. if (!$input.is(":input")) return;
  549. var buffer = _buffer.slice();
  550. //correct greedy setting if needed
  551. opts.greedy = opts.greedy ? opts.greedy : opts.repeat == 0;
  552. //handle maxlength attribute
  553. var maxLength = $input.prop('maxLength');
  554. if (getMaskLength(buffer) > maxLength && maxLength > -1) { //FF sets no defined max length to -1
  555. if (maxLength < _buffer.length) _buffer.length = maxLength;
  556. if (opts.greedy == false) {
  557. opts.repeat = Math.round(maxLength / _buffer.length);
  558. }
  559. $input.prop('maxLength', getMaskLength(buffer) * 2);
  560. }
  561. //store tests & original buffer in the input element - used to get the unmasked value
  562. $input.data('inputmask', {
  563. 'tests': tests,
  564. '_buffer': _buffer,
  565. 'greedy': opts.greedy,
  566. 'repeat': opts.repeat,
  567. 'autoUnmask': opts.autoUnmask,
  568. 'definitions': opts.definitions,
  569. 'isRTL': false
  570. });
  571. patchValueProperty(el);
  572. //init vars
  573. var undoBuffer = el._valueGet(),
  574. skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
  575. ignorable = false,
  576. lastPosition = -1,
  577. firstMaskPos = seekNext(buffer, -1),
  578. lastMaskPos = seekPrevious(buffer, getMaskLength(buffer)),
  579. isRTL = false;
  580. if (el.dir == "rtl" || opts.numericInput) {
  581. el.dir = "ltr"
  582. $input.css("text-align", "right");
  583. $input.removeAttr("dir");
  584. var inputData = $input.data('inputmask');
  585. inputData['isRTL'] = true;
  586. $input.data('inputmask', inputData);
  587. isRTL = true;
  588. }
  589. //unbind all events - to make sure that no other mask will interfere when re-masking
  590. $input.unbind(".inputmask");
  591. $input.removeClass('focus.inputmask');
  592. //bind events
  593. $input.bind("mouseenter.inputmask", function () {
  594. var $input = $(this), input = this;
  595. if (!$input.hasClass('focus.inputmask') && opts.showMaskOnHover) {
  596. var nptL = input._valueGet().length;
  597. if (nptL < buffer.length) {
  598. if (nptL == 0)
  599. buffer = _buffer.slice();
  600. writeBuffer(input, buffer);
  601. }
  602. }
  603. }).bind("blur.inputmask", function () {
  604. var $input = $(this), input = this, nptValue = input._valueGet();
  605. $input.removeClass('focus.inputmask');
  606. if (nptValue != undoBuffer) {
  607. $input.change();
  608. }
  609. if (opts.clearMaskOnLostFocus && nptValue != '') {
  610. if (nptValue == _buffer.join(''))
  611. input._valueSet('');
  612. else { //clearout optional tail of the mask
  613. clearOptionalTail(input, buffer);
  614. }
  615. }
  616. if (!isComplete(input)) {
  617. $input.trigger("incomplete");
  618. if (opts.clearIncomplete) {
  619. if (opts.clearMaskOnLostFocus)
  620. input._valueSet('');
  621. else {
  622. buffer = _buffer.slice();
  623. writeBuffer(input, buffer);
  624. }
  625. }
  626. }
  627. }).bind("focus.inputmask", function () {
  628. var $input = $(this), input = this, nptValue = input._valueGet();
  629. if (opts.showMaskOnFocus && !$input.hasClass('focus.inputmask') && (!opts.showMaskOnHover || (opts.showMaskOnHover && nptValue == ''))) {
  630. var nptL = nptValue.length;
  631. if (nptL < buffer.length) {
  632. if (nptL == 0)
  633. buffer = _buffer.slice();
  634. caret(input, checkVal(input, buffer, true));
  635. }
  636. }
  637. $input.addClass('focus.inputmask');
  638. undoBuffer = input._valueGet();
  639. }).bind("mouseleave.inputmask", function () {
  640. var $input = $(this), input = this;
  641. if (opts.clearMaskOnLostFocus) {
  642. if (!$input.hasClass('focus.inputmask')) {
  643. if (input._valueGet() == _buffer.join('') || input._valueGet() == '')
  644. input._valueSet('');
  645. else { //clearout optional tail of the mask
  646. clearOptionalTail(input, buffer);
  647. }
  648. }
  649. }
  650. }).bind("click.inputmask", function () {
  651. var input = this;
  652. setTimeout(function () {
  653. var selectedCaret = caret(input);
  654. if (selectedCaret.begin == selectedCaret.end) {
  655. var clickPosition = selectedCaret.begin;
  656. lastPosition = checkVal(input, buffer, false);
  657. determineInputDirection(input, selectedCaret);
  658. if (isRTL)
  659. caret(input, clickPosition > lastPosition && (isValid(clickPosition, buffer[clickPosition], buffer, true) !== false || !isMask(clickPosition)) ? clickPosition : lastPosition);
  660. else
  661. caret(input, clickPosition < lastPosition && (isValid(clickPosition, buffer[clickPosition], buffer, true) !== false || !isMask(clickPosition)) ? clickPosition : lastPosition);
  662. }
  663. }, 0);
  664. }).bind('dblclick.inputmask', function () {
  665. var input = this;
  666. setTimeout(function () {
  667. caret(input, 0, lastPosition);
  668. }, 0);
  669. }).bind("keydown.inputmask", keydownEvent
  670. ).bind("keypress.inputmask", keypressEvent
  671. ).bind("keyup.inputmask", keyupEvent
  672. ).bind(pasteEvent + ".inputmask dragdrop.inputmask drop.inputmask", function () {
  673. var input = this;
  674. setTimeout(function () {
  675. caret(input, checkVal(input, buffer, true));
  676. }, 0);
  677. }).bind('setvalue.inputmask', function () {
  678. var input = this;
  679. undoBuffer = input._valueGet();
  680. checkVal(input, buffer, true);
  681. if (input._valueGet() == _buffer.join(''))
  682. input._valueSet('');
  683. }).bind('complete.inputmask', opts.oncomplete)
  684. .bind('incomplete.inputmask', opts.onincomplete)
  685. .bind('cleared.inputmask', opts.oncleared);
  686. //apply mask
  687. lastPosition = checkVal(el, buffer, true);
  688. // Wrap document.activeElement in a try/catch block since IE9 throw "Unspecified error" if document.activeElement is undefined when we are in an IFrame.
  689. var activeElement;
  690. try {
  691. activeElement = document.activeElement;
  692. } catch (e) { }
  693. if (activeElement === el) { //position the caret when in focus
  694. $input.addClass('focus.inputmask');
  695. caret(el, lastPosition);
  696. } else if (opts.clearMaskOnLostFocus) {
  697. if (el._valueGet() == _buffer.join('')) {
  698. el._valueSet('');
  699. } else {
  700. clearOptionalTail(el, buffer);
  701. }
  702. }
  703. installEventRuler(el);
  704. //private functions
  705. function installEventRuler(npt) {
  706. var events = $._data(npt).events;
  707. $.each(events, function (eventType, eventHandlers) {
  708. $.each(eventHandlers, function (ndx, eventHandler) {
  709. if (eventHandler.namespace == "inputmask") {
  710. var handler = eventHandler.handler;
  711. eventHandler.handler = function () {
  712. if (this.readOnly || this.disabled)
  713. return false;
  714. return handler.apply(this, arguments);
  715. };
  716. }
  717. });
  718. });
  719. }
  720. function patchValueProperty(npt) {
  721. var valueProperty;
  722. if (Object.getOwnPropertyDescriptor)
  723. valueProperty = Object.getOwnPropertyDescriptor(npt, "value");
  724. if (valueProperty && valueProperty.get) {
  725. if (!npt._valueGet) {
  726. npt._valueGet = valueProperty.get;
  727. npt._valueSet = valueProperty.set;
  728. Object.defineProperty(npt, "value", {
  729. get: function () {
  730. var $self = $(this), inputData = $(this).data('inputmask');
  731. return inputData && inputData['autoUnmask'] ? $self.inputmask('unmaskedvalue') : this._valueGet() != inputData['_buffer'].join('') ? this._valueGet() : '';
  732. },
  733. set: function (value) {
  734. this._valueSet(value); $(this).triggerHandler('setvalue.inputmask');
  735. }
  736. });
  737. }
  738. } else if (document.__lookupGetter__ && npt.__lookupGetter__("value")) {
  739. if (!npt._valueGet) {
  740. npt._valueGet = npt.__lookupGetter__("value");
  741. npt._valueSet = npt.__lookupSetter__("value");
  742. npt.__defineGetter__("value", function () {
  743. var $self = $(this), inputData = $(this).data('inputmask');
  744. return inputData && inputData['autoUnmask'] ? $self.inputmask('unmaskedvalue') : this._valueGet() != inputData['_buffer'].join('') ? this._valueGet() : '';
  745. });
  746. npt.__defineSetter__("value", function (value) {
  747. this._valueSet(value); $(this).triggerHandler('setvalue.inputmask');
  748. });
  749. }
  750. } else {
  751. if (!npt._valueGet) {
  752. npt._valueGet = function () { return this.value; }
  753. npt._valueSet = function (value) { this.value = value; }
  754. }
  755. if ($.fn.val.inputmaskpatch != true) {
  756. $.fn.val = function () {
  757. if (arguments.length == 0) {
  758. var $self = $(this);
  759. if ($self.data('inputmask')) {
  760. if ($self.data('inputmask')['autoUnmask'])
  761. return $self.inputmask('unmaskedvalue');
  762. else {
  763. var result = $.inputmask.val.apply($self);
  764. return result != $self.data('inputmask')['_buffer'].join('') ? result : '';
  765. }
  766. } else return $.inputmask.val.apply($self);
  767. } else {
  768. var args = arguments;
  769. return this.each(function () {
  770. var $self = $(this);
  771. var result = $.inputmask.val.apply($self, args);
  772. if ($self.data('inputmask')) $self.triggerHandler('setvalue.inputmask');
  773. return result;
  774. });
  775. }
  776. };
  777. $.extend($.fn.val, {
  778. inputmaskpatch: true
  779. });
  780. }
  781. }
  782. }
  783. function determineInputDirection(input, pos) {
  784. //set input direction according the position to the radixPoint
  785. if (opts.numericInput && opts.radixPoint != "") {
  786. var nptStr = input._valueGet();
  787. var radixPosition = nptStr.indexOf(opts.radixPoint);
  788. isRTL = pos.begin <= radixPosition || pos.end <= radixPosition || radixPosition == -1;
  789. }
  790. }
  791. //shift chars to left from start to end and put c at end position if defined
  792. function shiftL(start, end, c) {
  793. while (!isMask(start) && start - 1 >= 0) start--;
  794. for (var i = start; i < end && i < getMaskLength(buffer) ; i++) {
  795. if (isMask(i)) {
  796. setReTargetPlaceHolder(buffer, i);
  797. var j = seekNext(buffer, i);
  798. var p = getBufferElement(buffer, j);
  799. if (p != getPlaceHolder(j)) {
  800. if (j < getMaskLength(buffer) && isValid(i, p, buffer, true) !== false && tests[determineTestPosition(i)].def == tests[determineTestPosition(j)].def) {
  801. setBufferElement(buffer, i, getBufferElement(buffer, j));
  802. setReTargetPlaceHolder(buffer, j); //cleanup next position
  803. } else {
  804. if (isMask(i))
  805. break;
  806. }
  807. } else if (c == undefined) break;
  808. } else {
  809. setReTargetPlaceHolder(buffer, i);
  810. }
  811. }
  812. if (c != undefined)
  813. setBufferElement(buffer, isRTL ? end : seekPrevious(buffer, end), c);
  814. buffer = truncateInput(buffer.join(''), isRTL).split('');
  815. if (buffer.length == 0) buffer = _buffer.slice();
  816. return start; //return the used start position
  817. }
  818. function shiftR(start, end, c, full) { //full => behave like a push right ~ do not stop on placeholders
  819. for (var i = start; i <= end && i < getMaskLength(buffer) ; i++) {
  820. if (isMask(i)) {
  821. var t = getBufferElement(buffer, i);
  822. setBufferElement(buffer, i, c);
  823. if (t != getPlaceHolder(i)) {
  824. var j = seekNext(buffer, i);
  825. if (j < getMaskLength(buffer)) {
  826. if (isValid(j, t, buffer, true) !== false && tests[determineTestPosition(i)].def == tests[determineTestPosition(j)].def)
  827. c = t;
  828. else {
  829. if (isMask(j))
  830. break;
  831. else c = t;
  832. }
  833. } else break;
  834. } else if (full !== true) break;
  835. } else
  836. setReTargetPlaceHolder(buffer, i);
  837. }
  838. var lengthBefore = buffer.length;
  839. buffer = truncateInput(buffer.join(''), isRTL).split('');
  840. if (buffer.length == 0) buffer = _buffer.slice();
  841. return end - (lengthBefore - buffer.length); //return new start position
  842. };
  843. function keydownEvent(e) {
  844. //Safari 5.1.x - modal dialog fires keypress twice workaround
  845. skipKeyPressEvent = false;
  846. var input = this, k = e.keyCode, pos = caret(input);
  847. determineInputDirection(input, pos);
  848. //backspace, delete, and escape get special treatment
  849. if (k == opts.keyCode.BACKSPACE || k == opts.keyCode.DELETE || (iphone && k == 127)) {//backspace/delete
  850. var maskL = getMaskLength(buffer);
  851. if (pos.begin == 0 && pos.end == maskL) { //remove full selection
  852. buffer = _buffer.slice();
  853. writeBuffer(input, buffer);
  854. caret(input, checkVal(input, buffer, false));
  855. } else if ((pos.end - pos.begin) > 1 || ((pos.end - pos.begin) == 1 && opts.insertMode)) { //partial selection
  856. clearBuffer(buffer, pos.begin, pos.end);
  857. writeBuffer(input, buffer, isRTL ? checkVal(input, buffer, false) : pos.begin);
  858. } else { //handle delete
  859. var beginPos = pos.begin;
  860. if (k == opts.keyCode.DELETE) {
  861. if (beginPos < firstMaskPos)
  862. beginPos = firstMaskPos;
  863. if (beginPos < maskL) {
  864. if (opts.numericInput && opts.radixPoint != "" && buffer[beginPos] == opts.radixPoint) {
  865. beginPos = (buffer.length - 1 == beginPos) /* radixPoint is latest? delete it */ ? beginPos : seekNext(buffer, beginPos);
  866. beginPos = shiftL(beginPos, maskL);
  867. } else {
  868. if (isRTL) {
  869. beginPos = shiftR(firstMaskPos, beginPos, getPlaceHolder(beginPos), true);
  870. beginPos = seekNext(buffer, beginPos);
  871. } else {
  872. beginPos = shiftL(beginPos, maskL);
  873. }
  874. }
  875. writeBuffer(input, buffer, beginPos);
  876. }
  877. } else if (k == opts.keyCode.BACKSPACE) { //handle backspace
  878. if (beginPos > firstMaskPos) {
  879. beginPos -= 1;
  880. if (opts.numericInput && opts.radixPoint != "" && buffer[beginPos] == opts.radixPoint) {
  881. beginPos = shiftR(firstMaskPos, (buffer.length - 1 == beginPos) /* radixPoint is latest? delete it */ ? beginPos : beginPos - 1, getPlaceHolder(beginPos), true);
  882. beginPos++;
  883. } else {
  884. if (isRTL) {
  885. beginPos = shiftR(firstMaskPos, beginPos, getPlaceHolder(beginPos), true);
  886. beginPos = buffer[beginPos + 1] == opts.radixPoint ? beginPos + 1 : seekNext(buffer, beginPos);
  887. } else {
  888. beginPos = shiftL(beginPos, maskL);
  889. }
  890. }
  891. writeBuffer(input, buffer, beginPos);
  892. }
  893. }
  894. }
  895. if (input._valueGet() == _buffer.join(''))
  896. $(input).trigger('cleared');
  897. e.preventDefault(); //stop default action but allow propagation
  898. } else if (k == opts.keyCode.END || k == opts.keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch
  899. setTimeout(function () {
  900. var caretPos = checkVal(input, buffer, false, true);
  901. if (!opts.insertMode && caretPos == getMaskLength(buffer) && !e.shiftKey) caretPos--;
  902. caret(input, e.shiftKey ? pos.begin : caretPos, caretPos);
  903. }, 0);
  904. } else if (k == opts.keyCode.HOME || k == opts.keyCode.PAGE_UP) {//Home or page_up
  905. caret(input, 0, e.shiftKey ? pos.begin : 0);
  906. }
  907. else if (k == opts.keyCode.ESCAPE) {//escape
  908. input._valueSet(undoBuffer);
  909. caret(input, 0, checkVal(input, buffer));
  910. } else if (k == opts.keyCode.INSERT) {//insert
  911. opts.insertMode = !opts.insertMode;
  912. caret(input, !opts.insertMode && pos.begin == getMaskLength(buffer) ? pos.begin - 1 : pos.begin);
  913. } else if (e.ctrlKey && k == 88) {
  914. setTimeout(function () {
  915. caret(input, checkVal(input, buffer, true));
  916. }, 0);
  917. } else if (!opts.insertMode) { //overwritemode
  918. if (k == opts.keyCode.RIGHT) {//right
  919. var caretPos = pos.begin == pos.end ? pos.end + 1 : pos.end;
  920. caretPos = caretPos < getMaskLength(buffer) ? caretPos : pos.end;
  921. caret(input, e.shiftKey ? pos.begin : caretPos, e.shiftKey ? caretPos + 1 : caretPos);
  922. } else if (k == opts.keyCode.LEFT) {//left
  923. var caretPos = pos.begin - 1;
  924. caretPos = caretPos > 0 ? caretPos : 0;
  925. caret(input, caretPos, e.shiftKey ? pos.end : caretPos);
  926. }
  927. }
  928. opts.onKeyDown.call(this, e, buffer, opts); //extra stuff to execute on keydown
  929. ignorable = $.inArray(k, opts.ignorables) != -1;
  930. }
  931. function keypressEvent(e) {
  932. //Safari 5.1.x - modal dialog fires keypress twice workaround
  933. if (skipKeyPressEvent) return false;
  934. skipKeyPressEvent = true;
  935. var input = this, $input = $(input);
  936. e = e || window.event;
  937. var k = e.which || e.charCode || e.keyCode,
  938. c = String.fromCharCode(k);
  939. if (opts.numericInput && c == opts.radixPoint) {
  940. var nptStr = input._valueGet();
  941. var radixPosition = nptStr.indexOf(opts.radixPoint);
  942. caret(input, seekNext(buffer, radixPosition != -1 ? radixPosition : getMaskLength(buffer)));
  943. }
  944. if (e.ctrlKey || e.altKey || e.metaKey || ignorable) {
  945. return true;
  946. } else {
  947. if (k) {
  948. //$input.trigger('input');
  949. var pos = caret(input), maskL = getMaskLength(buffer), writeOutBuffer = true;
  950. clearBuffer(buffer, pos.begin, pos.end);
  951. if (isRTL) {
  952. var p = seekPrevious(buffer, pos.end), np;
  953. if ((np = isValid(p == maskL || getBufferElement(buffer, p) == opts.radixPoint ? seekPrevious(buffer, p) : p, c, buffer, false)) !== false) {
  954. if (np !== true) {
  955. p = np.pos != undefined ? np.pos : p; //set new position from isValid
  956. c = np.c != undefined ? np.c : c; //set new char from isValid
  957. }
  958. maskL = getMaskLength(buffer); //update masklength to include possible groupSeparator offset
  959. var firstUnmaskedPosition = firstMaskPos;
  960. if (opts.insertMode == true) {
  961. if (opts.greedy == true) {
  962. var bfrClone = buffer.slice();
  963. while (getBufferElement(bfrClone, firstUnmaskedPosition, true) != getPlaceHolder(firstUnmaskedPosition) && firstUnmaskedPosition <= p) {
  964. firstUnmaskedPosition = firstUnmaskedPosition == maskL ? (maskL + 1) : seekNext(buffer, firstUnmaskedPosition);
  965. }
  966. }
  967. if (firstUnmaskedPosition <= p && (opts.greedy || buffer.length < maskL)) {
  968. if (buffer[firstMaskPos] != getPlaceHolder(firstMaskPos) && buffer.length < maskL) {
  969. var offset = prepareBuffer(buffer, -1, isRTL);
  970. if (pos.end != 0) p = p + offset;
  971. maskL = buffer.length;
  972. }
  973. shiftL(firstUnmaskedPosition, p, c);
  974. } else writeOutBuffer = false;
  975. } else setBufferElement(buffer, p, c);
  976. if (writeOutBuffer) {
  977. writeBuffer(input, buffer, opts.numericInput ? p + 1 : p);
  978. setTimeout(function () { //timeout needed for IE
  979. if (isComplete(input))
  980. $input.trigger("complete");
  981. }, 0);
  982. }
  983. }
  984. }
  985. else {
  986. var p = seekNext(buffer, pos.begin - 1), np;
  987. prepareBuffer(buffer, p, isRTL);
  988. if ((np = isValid(p, c, buffer, false)) !== false) {
  989. if (np !== true) {
  990. p = np.pos != undefined ? np.pos : p; //set new position from isValid
  991. c = np.c != undefined ? np.c : c; //set new char from isValid
  992. }
  993. if (opts.insertMode == true) {
  994. var lastUnmaskedPosition = getMaskLength(buffer);
  995. var bfrClone = buffer.slice();
  996. while (getBufferElement(bfrClone, lastUnmaskedPosition, true) != getPlaceHolder(lastUnmaskedPosition) && lastUnmaskedPosition >= p) {
  997. lastUnmaskedPosition = lastUnmaskedPosition == 0 ? -1 : seekPrevious(buffer, lastUnmaskedPosition);
  998. }
  999. if (lastUnmaskedPosition >= p)
  1000. shiftR(p, buffer.length, c);
  1001. else writeOutBuffer = false;
  1002. }
  1003. else setBufferElement(buffer, p, c);
  1004. if (writeOutBuffer) {
  1005. var next = seekNext(buffer, p);
  1006. writeBuffer(input, buffer, next);
  1007. setTimeout(function () { //timeout needed for IE
  1008. if (isComplete(input))
  1009. $input.trigger("complete");
  1010. }, 0);
  1011. }
  1012. }
  1013. }
  1014. if(android) {
  1015. console.log("restore " + caretSavePoint.begin + ' ' + caretSavePoint.end);
  1016. caret(input, caretSavePoint.begin, caretSavePoint.end);
  1017. }
  1018. e.preventDefault();
  1019. }
  1020. }
  1021. }
  1022. function keyupEvent(e) {
  1023. var $input = $(this), input = this;
  1024. var k = e.keyCode;
  1025. opts.onKeyUp.call(this, e, buffer, opts); //extra stuff to execute on keyup
  1026. if (k == opts.keyCode.TAB && $input.hasClass('focus.inputmask') && input._valueGet().length == 0 && opts.showMaskOnFocus) {
  1027. buffer = _buffer.slice();
  1028. writeBuffer(input, buffer);
  1029. if (!isRTL) caret(input, 0);
  1030. undoBuffer = input._valueGet();
  1031. }
  1032. }
  1033. }
  1034. return this; //return this to expose publics
  1035. };
  1036. }
  1037. })(jQuery);