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