jquery.inputmask.js 59 KB

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